Posts

Showing posts from July, 2019

How to create a JAX-RS Jersey client application for consuming a REST service

Image
In this blog post, I will be demonstrating how you can create a JAX-RS client application using Jersey JAX-RS client implementation. What is JAX-RS and Jersey Client API? JAX-RS is the Java specification for Restful web services. It is just a specification, it does not provide an implementation. Jersey is the reference implementation of JAX-RS specification. There are other implementations of JAX-RS like RESTEasy, etc  The first release of JAX-RS was JAX-RS 1.0. It did not provide a client API. So vendors like Jersey, RESTEasy provided their own client APIs. JAX-RS 2.0 (which is the latest release of JAX-RS) provides a client API. The latest release of Jersey (2.25.1) implements this client API. So in addition to its proprietary client API, Jersey also implements the JAX-RS client API.  While implementing a REST client that uses Jersey, it is better to use the JAX-RS specific client implementation and not Jersey’s client API. So even if the JAX-RS vendor is changed from Jersey to somet

Important Spring MVC Annotations Explained

In this blog post, I will be explaining some of the important Spring MVC Annotations used in Spring applications. Controller You can use the Controller annotation to designate a Controller class in an MVC application. Normally in such scenarios, the code maps each method in the controller to a URL specified via the  RequestMapping annotation. When the UI requests a particular URL, the code invokes the appropriate method. This executes the business logic and then redirects the user to the appropriate JSP page. Edit RestController Spring 4.0 introduced the  RestController annotation. So the  RestController annotation is a specialization of the Controller annotation that can be used to designate a REST service. It combines the behavior of the  Controller and  ResponseBody annotations. So when a method in a class is designated with this annotation, it returns the REST response directly to the client instead of redirecting the user to an HTML or JSP page ResponseBody The  Response

Create a REST service in Java using JAX-RS and Eclipse

Image
In this article, I will be demonstrating creating a REST service in Java using JAX-RS and Eclipse. I will be using Maven and Eclipse. Create a Project and And Maven Support Step 1 – Follow the steps in  this  article to create a web project in Eclipse with Maven Support. You should see a project as follows: Step 2 – Add Maven dependencies for JAX-RS and Jersey as follows: <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-bundle</artifactId> <version>1.19.4</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20170516</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId>

How to deploy an application on Tomcat server in Eclipse

Image
In this blog post, I will be demonstrating how to deploy an application to Tomcat in Eclipse. In order to deploy a Maven application in Tomcat in Eclipse, you can follow the same steps. Step 1 – Setup Tomcat in Eclipse if not already setup. Refer to this blog post for detailed steps Step 2 – Right-click on “Tomcat Server” at the bottom and click Add/Remove: Step 3 – Select Your project name and click “Add”: Step 4 – Click on Finish. Step 5 – Start Tomcat by right-clicking and clicking on “Start”. If the server starts successfully, you should see a message similar to the following in the console:   Further Reading Fundamentals of Apache Tomcat Beginning with Eclipse IDE Apache Tomcat Beginners to Advanced Java Programming in Eclipse

Java 8 LongConsumer example

In this blog post, I will be explaining how the Java 8 functional interface LongConsumer works. To know more about functional interfaces, you can refer this blog post. Edit The LongConsumer interface provides a method called accept . It accepts a single parameter of long data type. It does not return anything, it returns a void. So it operates via side effects i.e. it modifies the parameter passed in.The LongConsumer interface is a specialization of the Consumer  interface. While the Consumer interface accepts any data type, the LongConsumer interface accepts a long value. To see an example of the Consumer interface, refer to  this  blog post. LongConsumer Example Consider the following code snippet: public class LongConsumerDemo { public static void main(String args[]) { LongConsumer decrementBy5 = (num) -> System.out.println("Input:"+num+", Incremented Value:"+(num-5)); decrementBy5.accept(12); decrementBy5.accept(23); }}   Here, the Lon

Java 8 Stream API map method with examples

In this blog post, I will be demonstrating the Java 8 Stream API map method. In order to understand the Stream API in detail, refer to this  blog post. Edit Introduction The map method in the Stream API can be applied on an existing Stream. It applies some logic to each element in the input stream and produces an output stream. For example, if there is an integer stream, you can you the map operation to multiply each element by 5. Or if you have a Stream of String values, you can use the map operation to convert each element in the Stream to uppercase. The map operation accepts a Function instance which is an in-built functional interface. Refer this blog post for a detailed Function example. Code Sample with Integer Consider the following code sample: public class MapIntegerDemo { public static void main(String[] args) { List<Integer> input = Arrays.asList(5, 3, 11, 15, 9, 2, 5, 11); System.out.println("Input list:"); input.forEach(num -> System.ou

Java Super Keyword explained

What is the Super Keyword? Super keyword is used to access members of the superclass from the subclass. It can be used to access both instance fields and methods. Code Sample Consider the following code: public class Base { int a; int b; public void baseMethod() { System.out.println("In base class method”); }}public class Sub extends Base { int c; public void subMethod() { super.a=4; super.b=9; super.baseMethod(); System.out.println("In sub class method"); } } There are 2 classes, Base and Sub Sub is a sub-class of Base ; it has a method called subMethod The base fields a,b are accessed via the super keyword in subMethod The base method baseMethod is also accessed via the super keyword When this code is executed, it will print the following output: In base class methodIn sub class method Points to Remember The super keyword is a bit redundant for instance fields and methods since as long as the instance fields and method of the sup

Spring RequestMapping, GetMappin, PostMapping annotations

In this blog post, I will be explaining the RequestMapping, GetMapping and PostMapping annotations in Spring. In order to know more about other Spring annotations, you can refer to this article. In order to learn about the Spring framework from scratch, I recommend that you enroll in this Spring masterclass . You can use the RequestMapping, GetMapping and PostMapping annotations to map a URL to a particular Controller method. However, there are some differences between them. Let’s check them out. Edit RequestMapping The RequestMapping annotation can be used to map both GET and POST requests. Consider the following code: @RequestMapping(value="/person/{personId}",method=RequestMethod.GET) public Person getPersonWithId(@PathVariable Integer personId){ } Here, the RequestMapping annotation is used on the getPersonWithId method. It maps to the /person/{personId} URL. The method attribute is also specified. This indicates that this method maps to an HTTP GET request

Java TreeSet and HashSet similarities and differences

Java TreeSet and HashSet are implementations of the Set interface. Often you may have wondered, whether to choose HashSet or TreeSet and which is faster. In this article, I will be comparing the HashSet and TreeSet classes in Java. HashSet and TreeSet Similarities Duplicates not allowed Since both HashSet and TreeSet implement the Set interface, they do not allow duplicates. So consider the following code: Set<String> hashSet = new HashSet<String>(); hashSet.add("Mango"); hashSet.add("Mango"); Set<String> treeSet = new TreeSet<String>(); treeSet.add("Mango"); treeSet.add("Mango"); System.out.println("Number of elements in Hashset:"+hashSet.size()); System.out.println("Number of elements in Treeset:"+treeSet.size()); When you execute this code, it will print the following output: Number of elements in Hashset:1Number of elements in Treeset:1  Not Thread-Safe Both HashSet and

Java 8 IntConsumer Interface

In this blog post, I will be explaining how the Java 8 functional interface IntConsumer works. To know more about functional interfaces, you can refer this blog post. Edit The IntConsumer interface provides a method called accept. It accepts a single parameter of int data type. It does not return anything, it returns a void. So it operates via side effects i.e. it modifies the parameter passed in. The  IntConsumer interface is a specialization of the Consumer interface. While the Consumer interface accepts any data type, the  IntConsumer interface accepts an Integer value. To see an example of the Consumer interface, refer to  this  blog post. IntConsumer Example Consider the following code snippet: public class IntConsumerDemo { public static void main(String args[]) { IntConsumer incrementby5 = (num) -> System.out.println("Input:"+num+", Incremented Value:"+(num+5)); incrementby5.accept(12); incrementby5.accept(23); }}   Here, the IntConsumer.