java2db

Example for (media type) text plain in RESTful web service ( Jersey )



If any client sends request to a  java RESTful web service for plain text result (the response from the server in the form of a plain text ), the service must return a plain text (text/plain) response to the client after processing the requested resource.

Add @Produces(MediaType.TEXT_PLAIN) annotation to use text plain in RESTful web service .

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

 

create a servletJar jersey files version might change according to the latest releases.

 

Process to use (text/palin MediaType) text plain in RESTful web service :

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

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

Create web.xml under WEB-INF folder .

Add the required  jar files to lib folder.

 

Example for (media type) text plain in RESTful web service ( Jersey )

 

 

 

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

Copy the below java code in CustomerResourceClass.java and Customer.java classes.

 

 

Example for (media type) text plain in RESTful web service ( Jersey ) CustomerResourceClass.java

 

 

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

 

 

 

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

Copy the below code in web.xml file .

Example for (media type) text plain in RESTful web service ( Jersey ) web.xml

 

 

 

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

Deploy the dynamic web project in server and start the server . To test the application we used Eclipse server . If you want , use other server like tomcat , WebSphere , Jetty , Jboss or Wildfly.

 

 

Example for (media type) text plain in RESTful web service ( Jersey ) Step 5 :

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

http://localhost:9090/RESTful_PlainText_Response/api/customerInfo/plain/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_PlainText_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  plain          -: @Path(“/plain/{customerNo : (\\w+)?}”)  added in the top of  CustomerResourceClass.java class  method .

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

 

 

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

Finally we will get the below text plain response from the RESTful_PlainText_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) Json in RESTful web service ( Jersey )
Example for (media type) text html 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]



^ <