Posts

Showing posts with the label java

two way webservice communication REST

Image
Clash Royale CLAN TAG #URR8PPP two way webservice communication REST G'day folks, So I have an application in mind with a client-server architecture where multiple clients are connected to a web service. The webservice needs to be able to call or send messages to all or some of the clients. I have previously done the same using SOAP based services in WCF but now I am working with java and wish to avoid SOAP. I have been experimenting with Server Sent Events in reactive framework in Spring framework but have been mostly unlucky. Is there a way to implement two way communication without explicitly exposing a webservice or such on the client? thanks a bunch :) 1 Answer 1 Try WebSockets for two-way communication and simple Restful for request-response communication. Here is a simple guide: http://www.baeldung.com/java-websockets By clicking "Post Your...

jaxb unmarshal xml element to object with some fields

Image
Clash Royale CLAN TAG #URR8PPP jaxb unmarshal xml element to object with some fields I have some classes: class Location { private Long id; private String roomTitle; protected Specialization specialization; } public class Specialization { private Long id; private boolean archived; private String title; } and xml: <location id="10"> <roomTitle>string</roomTitle> <specializationId>10</specializationId> </location> Can I unmarshal specializationId to Location.specialization.id ? By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Gradle sets incorrect (unexpected?) classpath on running task

Image
Clash Royale CLAN TAG #URR8PPP Gradle sets incorrect (unexpected?) classpath on running task I have my project structured in this way: projectRoot +-src +-main | +-java | | +-package/java files go here | +-resources | +-config | +-files go here +-test +-java | +-package/java files go here +-resources +-config | +-files go here +-features +-files go here When this project is built, it produces the following output structure: projectRoot +-out +-production | +-classes | | +-package/classes in here | +-resources | +-config | +-files in here +-test +-classes | +-package/classes in here +-resources +-config | +-files in here +-features +-files in here This is all...

Read from YAML File

Image
Clash Royale CLAN TAG #URR8PPP Read from YAML File i have a yaml file to my chatbot i want read answer from yaml file exemple Person see hello Chatbot see Hello person how to do with code java to read answer from yaml file By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Checking if a String is contained in an Enum Set in O(1)

Image
Clash Royale CLAN TAG #URR8PPP Checking if a String is contained in an Enum Set in O(1) Let's say I have an enum like the following: public enum Utils { UTIL_1, UTIL_2, ... UTIL_n } Now, I have Set<Utils> myUtils , which contains a bunch of these enum entries. Some other module (on which I don't have any control) gives me one such util name (say " UTIL_i ") as a String. I need to check if it is contained in the set myUtils . Is there any way to perform this operation in O(1)? I understand, I could change the set to be a set of Strings, and then do a .contains() on that, but I want to keep it as a last resort. Set<Utils> myUtils UTIL_i myUtils Update : Following the suggestions in the answer, I tried a little experiment. I created a small code snippet to generate a java file which is an enum of a fixed size. I tried with size 1000, 2500, 5000. An enum of size 10000 showed me the error The code for the static initializer is exceeding the 65535 bytes lim...

How to return positive and negative numbers when reading a file

Image
Clash Royale CLAN TAG #URR8PPP How to return positive and negative numbers when reading a file I am writing a code that reads a file line by line and return only the following lines, as an example: int int0 = (-953); int int1 = (-411); int int2 = 5471; int int3 = 823; After that I would like to return only the numbers both positive and negative. To do that, I wrote the following: String str = line.replaceAll("\D+",""); System.out.println(str); The result of running this code is: 0953 1411 25471 3823 The output that I seek is: -953 -411 5471 823 How can I do that? does the number can be float ? – YCF_L 1 hour ago @Thomas Look at the variable naming 0, 1, 2, 3, then look at the start of each output number ;) – Glains ...

Java Enum why static line executes only once?

Image
Clash Royale CLAN TAG #URR8PPP Java Enum why static line executes only once? The result is: 1 3 1 3 1 3 2 The constructor runs for A,B and for C (3 times). But if you use static keyword it runs only once. What is the reason of this? And why does this line executes last? enum Enums { A, B, C; { System.out.println(1); } static { System.out.println(2); } private Enums() { System.out.println(3); } } public class MainClass { public static void main(String args) { Enum en = Enums.C; } } By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

JFrame size is too small

Image
Clash Royale CLAN TAG #URR8PPP JFrame size is too small i have created a JFrame in netbeans. But when i run the program, the Jframe size is too small. here is my code. import javax.swing.JFrame; public class Window { private static void demo() { JFrame frame =new JFrame(); frame.setVisible(true); } public static void main(String args) { javax.swing.SwingUtilities.invokeLater(new Runnable(){ public void run() { demo(); } }); } } Swing JComponents by default accepting only PreferredSize, – mKorbel Oct 27 '12 at 12:35 "frame size is too small" Put components in it, then call pack() . The frame will become the smallest size it needs to be, in order to display the compo...

error while generating signed apk init

Image
Clash Royale CLAN TAG #URR8PPP error while generating signed apk init Hey i am trying to generate signed apk to upload to the store and i keep getting this error: Error:FAILURE: Build failed with an exception. What went wrong: Execution failed for task ':android:lintVitalRelease'. Could not resolve all files for configuration ':android:lintClassPath'. Could not resolve com.android.tools.external.com-intellij:intellij-core:26.1.0. Required by: project :android > com.android.tools.lint:lint-gradle:26.1.0 project :android > com.android.tools.lint:lint-gradle:26.1.0 > com.android.tools.lint:lint:26.1.0 > com.android.tools.lint:lint-checks:26.1.0 project :android > com.android.tools.lint:lint-gradle:26.1.0 > com.android.tools.lint:lint:26.1.0 > com.android.tools.lint:lint-kotlin:26.1.0 project :android > com.android.tools.lint:lint-gradle:26.1.0 > com.android.tools.lint:lint:26.1.0 > com.android.tools.lint:lint-c...

What does it mean that Kotlin based apps will get a performance boost on Android P

Image
Clash Royale CLAN TAG #URR8PPP What does it mean that Kotlin based apps will get a performance boost on Android P Over the last few of months in a bunch of places I've seen the information that Android P will give the performance boost to Kotlin based apps (e.g. here and here). On the official Android blog, Dave Burke in "Previewing Android P" post described it in a few words: Kotlin is a first-class language on Android, and if you haven't tried it yet, you should! We've made an enduring commitment to Kotlin in Android and continue to expand support including optimizing the performance of Kotlin code. In P you'll see the first results of this work -- we've improved several compiler optimizations, especially those that target loops, to extract better performance. We're also continuing to work in partnership with JetBrains to optimize Kotlin's generated code. You can get all of the latest Kotlin performance improvements just by keeping Android Studio...

How to format text correctly using printf() (Java)

Image
Clash Royale CLAN TAG #URR8PPP How to format text correctly using printf() (Java) Hello I want to print something so they are aligned. for (int i = 0; i < temp.size(); i++) { //creatureT += "[" + temp.get(i).getCreatureType() + "]"; creatureS = "t" + temp.get(i).getName(); creatureT = " [" + temp.get(i).getCreatureType() + "]"; System.out.printf(creatureS + "%15a",creatureT + "n"); } and the output is Lily [Animal] Mary [NPC] Peter [Animal] Squash [PC] I just want the [Animal], [NPC], and [PC] to be aligned like Lily [Animal] Mary [NPC] Peter [Animal] Squash [PC] Say I know that no name will be greater then 15 characters. surely some of your past responses helped you. Why not accept some of them? It would show that you appreciate the time and effort put into trying to help...

Asking for user input twice in a while loop

Image
Clash Royale CLAN TAG #URR8PPP Asking for user input twice in a while loop I'm currently doing a java project. The project involves a database about NHL Statistics and about accessing bits of information from the database through user input. I'm using a while loop to ask for user input in multiple circumstances, such as asking for the number of penalties received, the number of goals scored, amount of points etc. I am having difficulty with one specific part, it asks the user input for which statistic they want (points, goals, assists,penalties, assists, player, club). After that, if the user types player as their choice, then the system is supposed to ask the user which players statistics they wish to see. The problem is that since I'm using a while loop after the user inputs the first time, the loop is executed and the program ends before the player can input the second time. Any help would be appreciated, here is my code: import java.util.Scanner; import nhlstats.NHLS...

Selecting Multiple Classes from an ID selection

Image
Clash Royale CLAN TAG #URR8PPP Selecting Multiple Classes from an ID selection I have the following XML <div id="Master25" class="open-pane" role="tabpanel"> <a href="/option1" title="option1" class="top-element "> <div class="top-element-description"> <div class="top-element-option-title">Spain</div> <div class="top-element-option-description">Test1</div> </div> <span class="top-element-price">23&euro;</span> </a> <a href="/option2" class="top-element "> <div class="top-element-description"> <div class="top-element-option-title">Greece</div> <div class="top-element-option-description">Test2</div> </div> <span class="top-element-price">25&euro;...

jackson JsonDeserializer stackoverflow

Image
Clash Royale CLAN TAG #URR8PPP jackson JsonDeserializer stackoverflow I am trying to extract attributes from a json sting in Java and I am writing a JsonDeseriizer to do it. My attempt is shown below: public class testJacksonSimple { @JsonDeserialize(using = ItemDeserializerJson.class) public static class Item { public int id; public String itemName; public Item(){}; public Item(int id, String itemName) { this.id = id; this.itemName = itemName; } public void setId(int id) { this.id = id; } public int getId() { return id; } } public static class ItemDeserializerJson extends JsonDeserializer<Item> { ObjectReader reader = new ObjectMapper().reader(); @Override public Item deserialize(JsonParser jp, DeserializationContext context) throws IOException { if(jp.currentToken() != JsonToken.VALUE_NULL) { ...

Spring-WS: consume service

Image
Clash Royale CLAN TAG #URR8PPP Spring-WS: consume service I try to follow the example on spring site to consume a SOAP web service (https://spring.io/guides/gs/consuming-web-service/) : The application works, it consume correctly the services, but on the pom.xml file there is an error. The part of pom that give the error is : <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.14.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <schemaLanguage>WSDL</schemaLanguage> <generatePackage>home.wsdl</generatePackage> <schemas> <schema> <url>http://localhost:8080/ws/countries.wsdl</url> ...

Android Spinner to determine Marker Colour

Image
Clash Royale CLAN TAG #URR8PPP Android Spinner to determine Marker Colour I have a spinner which I have created for a project in Android studio. The spinner takes the following values from an array in my strings.xml file. <string-array name="activity_types"> <item>Games</item> <item>Learn</item> <item>Fitness</item> <item>Make</item> <item>Explore</item> <item>Find</item> </string-array> I would like to create a function which places a marker of a particular colour onto my map in MainActivity i.e. selecting Games, will place an orange marker on the map, when 'Games' is selected and the 'Place' button (below) is pressed. However, I am not sure how to go about this? place_btn = (Button) findViewById(R.id.place_btn); place_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //what would handle the...

Using @MockBean to mock an ItemReader with @BeforeStep Throws Exception to many methods with @Beforestep anotation. How do I solve it?

Image
Clash Royale CLAN TAG #URR8PPP Using @MockBean to mock an ItemReader with @BeforeStep Throws Exception to many methods with @Beforestep anotation. How do I solve it? I have my own Implementation of an ItemReader with two methods. public class Reader implements ItemReader<Integer> { private final Logger logger = LoggerFactory.getLogger(getClass()); private Iterator<Integer> iterator; @Override public Integer read() throws UnexpectedInputException, ParseException, NonTransientResourceException { if(iterator.hasNext()){ return iterator.next(); } return null; } @BeforeStep public void init(StepExecution stepExecution){ List<Integer> integerList = (List<Integer>)stepExecution.getJobExecution().getExecutionContext().get(CKEY_ERROREVENT_IDS); this.iterator = integerList.iterator(); } } When I try to run this within a spring-batch context and mocking the ItemReader with @MockBean the ap...