REST, for "Representational State Transfer," is a lightweight architecture for network applications that use HTTP requests to read and write data
Java API for RESTful Web Services (JAX-RS), is a set if APIs to developer REST service. JAX-RS is part of the Java EE6, and make developers to develop REST web application easily.
In this examples we use Jersey popular JAX-RS implementation. Example shows how to develop a simple REST webservice using Jersey Client APIs. We will use Jersey Client APIs to create a RESTful Java client to perform "GET" and "POST" to the REST service.
Jersey uses Jackson to convert object to/from JSON. In this example we will convert object to JSON.
1. Jersey jars
pom.xml
2. GET Request
In JAX-RS, you can use @Path to bind URI pattern to a Java method.It support complex URI matching with regular expression.
Annotate the method with @Produces(MediaType.APPLICATION_JSON). Jersey will use Jackson to handle the JSON conversion automatically.
Here we are converting Track object to JSON format and return back to user. To make Jersey support this JSON conversion we have to use jersey-json.jar in Maven pom.xml as shown above.
To integrate JSON with Jersey we have to add few parameters to web.xml.
web.xml
In web.xml, declares "com.sun.jersey.api.json.POJOMappingFeature" as "init-param" in Jersey mapped servlet. It will make Jersey support JSON/object mapping.
Access Above REST service in two ways as shown below:
1) We can send "GET" request by hitting the url on the browser http://localhost:8080/JavaRest2/json/song/get
2) Jersey client to send "GET" request and print out the returned JSON data.
{"title":"Satyam Sivam Sundaram","singer":"Latha Mangeshkar"}
3. POST Request
@Consumes(MediaType.APPLICATION_JSON) will convert JSON data to object.
Jersey client to send a “POST” request, with json data and print out the returned output.
Java API for RESTful Web Services (JAX-RS), is a set if APIs to developer REST service. JAX-RS is part of the Java EE6, and make developers to develop REST web application easily.
In this examples we use Jersey popular JAX-RS implementation. Example shows how to develop a simple REST webservice using Jersey Client APIs. We will use Jersey Client APIs to create a RESTful Java client to perform "GET" and "POST" to the REST service.
Jersey uses Jackson to convert object to/from JSON. In this example we will convert object to JSON.
1. Jersey jars
pom.xml
2. GET Request
In JAX-RS, you can use @Path to bind URI pattern to a Java method.It support complex URI matching with regular expression.
Annotate the method with @Produces(MediaType.APPLICATION_JSON). Jersey will use Jackson to handle the JSON conversion automatically.
Here we are converting Track object to JSON format and return back to user. To make Jersey support this JSON conversion we have to use jersey-json.jar in Maven pom.xml as shown above.
To integrate JSON with Jersey we have to add few parameters to web.xml.
web.xml
In web.xml, declares "com.sun.jersey.api.json.POJOMappingFeature" as "init-param" in Jersey mapped servlet. It will make Jersey support JSON/object mapping.
Access Above REST service in two ways as shown below:
1) We can send "GET" request by hitting the url on the browser http://localhost:8080/JavaRest2/json/song/get
2) Jersey client to send "GET" request and print out the returned JSON data.
Output from Server:
3. POST Request
Jersey client to send a “POST” request, with json data and print out the returned output.
Output from Server ....
Track saved Track [title=Friday, singer=Rebacca]
Access Source Code: