In this tutorial we are going to see how to consume JSON type input using RESTful web service and Jackson
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder

4. Create package called com.javatutorialscorner.jaxrs.jsonservice under RESTful-WebService
5. Create Java class JSONService1 under com.javatutorialscorner.jaxrs.jsonservice package
JSONService1.java
01.
package
com.javatutorialscorner.jaxrs.jsonservice;
02.
03.
import
javax.ws.rs.Consumes;
04.
import
javax.ws.rs.POST;
05.
import
javax.ws.rs.Path;
06.
import
javax.ws.rs.core.MediaType;
07.
import
javax.ws.rs.core.Response;
08.
09.
10.
@Path
(
"consumejson"
)
11.
public
class
JSONService1 {
12.
13.
@POST
14.
@Path
(
"post"
)
15.
@Consumes
(MediaType.APPLICATION_JSON)
16.
public
Response setStudentDetail(Student student){
17.
String response =
"Data Received Successfuly : "
+student.getName();
18.
return
Response.status(
201
).entity(response).build();
19.
}
20.
}
In above program Jackson used to convert JSON to object. Jersey uses the @Consumes(MediaType.APPLICATION_JSON) annotation will convert JSON into object automatically.For this conversion jersey-json.jar is required.
6. Create Java class Student under com.javatutorialscorner.jaxrs.jsonservice package
Student.java
01.
package
com.javatutorialscorner.jaxrs.jsonservice;
02.
03.
public
class
Student {
04.
05.
String name;
06.
int
id;
07.
String department;
08.
int
year;
09.
10.
public
String getName() {
11.
return
name;
12.
}
13.
14.
public
void
setName(String name) {
15.
this
.name = name;
16.
}
17.
18.
public
int
getId() {
19.
return
id;
20.
}
21.
22.
public
void
setId(
int
id) {
23.
this
.id = id;
24.
}
25.
26.
public
String getDepartment() {
27.
return
department;
28.
}
29.
30.
public
void
setDepartment(String department) {
31.
this
.department = department;
32.
}
33.
34.
public
int
getYear() {
35.
return
year;
36.
}
37.
38.
public
void
setYear(
int
year) {
39.
this
.year = year;
40.
}
41.
42.
}
Jersey Jackson convert JSON input to this class type object.
7.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.
web.xml
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id
=
"WebApp_ID"
version
=
"3.0"
>
03.
<
display-name
>JAX-RS-Path</
display-name
>
04.
<
servlet
>
05.
<
servlet-name
>jersey-serlvet</
servlet-name
>
06.
<
servlet-class
>
07.
com.sun.jersey.spi.container.servlet.ServletContainer
08.
</
servlet-class
>
09.
<
init-param
>
10.
<
param-name
>com.sun.jersey.config.property.packages</
param-name
>
11.
<
param-value
>com.javatutorialscorner.jaxrs.jsonservice</
param-value
>
12.
</
init-param
>
13.
<
init-param
>
14.
<
param-name
>com.sun.jersey.api.json.POJOMappingFeature</
param-name
>
15.
<
param-value
>true</
param-value
>
16.
</
init-param
>
17.
<
load-on-startup
>1</
load-on-startup
>
18.
</
servlet
>
19.
20.
<
servlet-mapping
>
21.
<
servlet-name
>jersey-serlvet</
servlet-name
>
22.
<
url-pattern
>/rest/*</
url-pattern
>
23.
</
servlet-mapping
>
24.
</
web-app
>
The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL
1.
<
init-param
>
2.
<
param-name
>com.sun.jersey.api.json.POJOMappingFeature</
param-name
>
3.
<
param-value
>true</
param-value
>
4.
</
init-param
>
In this web.xml above lines are additionally added for Jersey to Support JSON-Object mapping.
you can call the service by using from client class.
Create Client
Create package called com.javatutorialscorner.jaxrs.client under RESTful-WebService project
Create Client Java Class RESTfulClient under com.javatutorialscorner.jaxrs.client package
RESTfulClient .java
01.
package
com.javatutorialscorner.jaxrs.client;
02.
03.
import
com.sun.jersey.api.client.Client;
04.
import
com.sun.jersey.api.client.ClientResponse;
05.
import
com.sun.jersey.api.client.WebResource;
06.
import
com.sun.jersey.api.client.config.ClientConfig;
07.
import
com.sun.jersey.api.client.config.DefaultClientConfig;
08.
09.
public
class
RESTfulClient {
10.
11.
/**
12.
* @param args
13.
*/
14.
public
static
void
main(String[] args) {
15.
// TODO Auto-generated method stub
16.
try
{
17.
String baseURI =
"http://localhost:8080/RESTful-WebService/rest/consumejson/post"
;
18.
ClientConfig config =
new
DefaultClientConfig();
19.
Client client = Client.create(config);
20.
WebResource service = client.resource(baseURI);
21.
String json =
"{\"name\":\"Ram\",\"id\":33,\"department\":\"EEE\",\"year\":2011}"
;
22.
ClientResponse response = service.type(
"application/json"
).post(
23.
ClientResponse.
class
, json);
24.
System.out.println(
"output for JSON Service "
);
25.
26.
if
(response.getStatus() !=
201
) {
27.
throw
new
Exception(
"Error code : "
+ response.getStatus());
28.
}
29.
30.
System.out.println(
"output "
+ response.getEntity(String.
class
));
31.
}
catch
(Exception e) {
32.
e.printStackTrace();
33.
}
34.
}
35.
36.
}
Now you can run the client and see the following output in console
output for JSON Service
output Data Received Successfuly : Ram
0 comments:
Post a Comment