java2db

Example java RESTful web service for xml response (JAX-RS , Jersey)



Any client may request text,json,html or  xml response from java RESTful web service according to the requirement. In this specific post , we given example for xml response from the RESTful web service.

For that, we need to add @Produces(MediaType.APPLICATION_XML)  annotation  at the top of the service method .

 

 

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).

 

Java RESTful web service for xml response (JAX-RS , Jersey)Jar files version might change according to the latest releases.

 

 

Steps to create Java RESTful web service for xml response :

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

Create a web project in Eclipse IDE (or NetBeans , MyEclipse , RAD ) . Create a package, java classes under src folder .

Create web.xml in WEB-INF folder .
Java RESTful web service for xml response (JAX-RS , Jersey)

 

 

Java RESTful web service for xml response (JAX-RS , Jersey)   Step 2:

Add all jar files to the lib folder.

 

 

Java RESTful web service for xml response (JAX-RS , Jersey)   Step 3: 

Copy the below code to java classes and web.xml .

 

 

CustomerResourceClass.java

Customer.java

web.xml

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

Start the server  and deploy the dynamic web project . If you want , can use other server like tomcat , WebSphere , Jetty , Jboss or Wildfly.

 

 

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

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

http://localhost:9090/RESTful_Xml_Response/api/customerInfo/xml/1

Content from the URL as follows.

Java RESTful web service for text plain request and response  http://     -: Protocol.

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

Java RESTful web service for text plain request and response  9090         -: Server port .

Java RESTful web service for text plain request and response  RESTful_Xml_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 i the top of CustomerResourceClass.java class.

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

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

 

 

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

Finally you will get the below xml response from the server.




Related Posts :
How to create a java client to call RESTful web service JAX-RS (Jersey )
Example for (media type) Json in RESTful web service ( 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]



^ <