java2db

Example for (media type) Json in RESTful web service ( Jersey )



Json : Json (JavaScript Object Notation) is an ideal text format data-interchange language . It is independent for  C, C++, C#, Java, JavaScript, Perl, Python, and many others programming languages. Read more.

 

 

The annotation @Produces(MediaType_JSON) will take Json request and produce Json response to the requested client.To use Json in RESTful web service (jersey ) add all Json jar files to libraries .

 

 

Example for (media type) Json in RESTful web service ( Jersey ) Basic requirements :

Softwares :
Java (Download).
GlassFish server (Download).
Eclipse IDE (Download).
Jar files:
jersey-client-1.18.jar (Download).
jersey-core-1.18.jar (Download).
jersey-json-1.18.jar (Download).
jersey-server-1.18.jar (Download).
jersey-servlet-1.18.jar (Download).
servlet-api-3.0.jar (Download).
asm-3.1.jar (Download).
jackson-core-asl-1.9.2.jar (Download).
jackson-jaxrs-1.9.2.jar (Download).
jackson-mapper-asl-1.9.2.jar (Download).
jackson-xc-1.9.2.jarjettison-1.1.jar (Download).
jsr311-api-1.1.1.jar (Download).

 

 

 

Example for (media type) Json in RESTful web service ( Jersey )Jar files version may change according to the latest releases.

Example for (media type) Json in RESTful web service ( Jersey ) Process to use Json in RESTful web service :

Example for (media type) Json in RESTful web service ( Jersey ) Step 1 :

Create a dynamic web project in any java supported IDE like Eclipse , NetBeans , MyEclipse or  RAD  . Create a new package jsonResponse and create  java classes in it .

Create web.xml file under WEB-INF folder .

Copy or add above listed  jar files to the lib folder.

 

See the complete directory structure below :

 

 

Example for (media type) Json in RESTful web service ( Jersey ) Step 2 : 

Copy the below code to  CustomerResourceClass.java  , Customer .java and  web.xml files.

Java RESTful web service for text plain request and response  CustomerResourceClass.java

 

 

 

Example for (media type) Json in RESTful web service ( Jersey ) Customer .java

 

 

 

Java RESTful web service for text plain request and response  web.xml

 

 

 

Java RESTful web service for text plain request and response   Step 3 :

Start the server  and deploy the web project.We used Glassfish server . If you want , use other servers like tomcat , WebSphere , Jetty , Jboss or Wildfly.

 

 

 

Example for (media type) Json in RESTful web service ( Jersey ) Step 4 :

To test the application , enter the URI in the client address bar and hit enter button .

 

 

http://localhost:9090/RESTful_Json_Response/api/customerInfo/json/1

Java RESTful web service for text plain request and response  http://     -: Hiper text transfer protocol

Java RESTful web service for text plain request and response  localhost  -: Host or IP  ( If working with remote PC , use it’s IP )

Java RESTful web service for text plain request and response  9090         -:  Eclipse server port (Enter the server port that running in  your computer).

Java RESTful web service for text plain request and response  RESTful_Json_Response -: Project name

Java RESTful web service for text plain request and response  api              -: configured in web.xml  <url-pattern> tag  (target)

Java RESTful web service for text plain request and response  customerInfo -:  @Path(“/customerInfo”)  added at the top of CustomerResourceClass.java .

Java RESTful web service for text plain request and response  plain          -: @Path(“/plain/{customerNo : (\\w+)?}”)  added at the top of getCustomerNameJson   method .

Java RESTful web service for text plain request and response  1                  -:  Parameter to the getCustomerNameJson  method .

Java RESTful web service for text plain request and response  Step 5 :

Finally you will get the below text plain response from the RESTful_Json_Response project.




Related Posts :
How to create a java client to call RESTful web service JAX-RS (Jersey )
Example java RESTful web service for xml response (JAX-RS , Jersey)
Example for (media type) text html in RESTful web service ( Jersey )
Example for (media type) text plain in RESTful web service ( Jersey )
Java RESTful web service example in maven using JAX-RS
RESTful web service example in java using JAX-RS
What is RESTful web service (Introduction to RESTful web services)
   [will not be published]



^ <