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). |
![]() |
Process to use (text/palin MediaType) text plain in RESTful web service :
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.
Step 2 :
Copy the below java code in CustomerResourceClass.java and Customer.java classes.
CustomerResourceClass.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
/* Author : java2db.com */ package plainTextResponse; import java.util.ArrayList; import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import plainTextResponse.Customer; @Path("/customerInfo") public class CustomerResourceClass { // This method is called if TEXT_PLAIN is request @Path("/plain/{customerNo : (\\w+)?}") @GET @Produces(MediaType.TEXT_PLAIN) public Response getCustomerNamePlain(@PathParam("customerNo") String customerNo) { String result = ""; List<Customer> customerList = getCustomerList(customerNo); for(Customer customer:customerList){ result = result+customer.getCustomerName(); } return Response.status(200).entity(result).build(); } private List<Customer> getCustomerList(String customerNo){ List<Customer> customerList = new ArrayList<Customer>(); if(customerNo == null || customerNo.equals("1") || customerNo.equals("")){ Customer customer1 = new Customer(); customer1.setCustomerName("Avatar"); customer1.setEmail("avatar@gmail.com"); customer1.setId("6075"); customer1.setPhone("0162995136"); customer1.setSalary(40456); customerList.add(customer1); } if(customerNo == null || customerNo.equals("2") || customerNo.equals("") ){ Customer customer2 = new Customer(); customer2.setCustomerName("zimbo"); customer2.setEmail("zimbo@gmail.com"); customer2.setId("4572"); customer2.setPhone("012234656"); customer2.setSalary(69000); customerList.add(customer2); } return customerList; } } |
Customer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* Author : java2db.com */ package restWebResource; public class Customer { String id; String customerName; String email; String phone; double salary; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } |
Step 3 :
Copy the below code in web.xml file .
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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"> <display-name>RestWS</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>jersey-rest-ws</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>plainTextResponse</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-rest-ws</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> </web-app> |
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.
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
http:// -: Hiper text transfer protocol
localhost -: Host or IP ( If working with remote PC , use it’s IP )
9090 -: Eclipse server port (Enter the server port that running in your computer).
RESTful_PlainText_Response -: Project name
api -: configured in web.xml <url-pattern> tag (target)
customerInfo -: @Path(“/customerInfo”) added i the top of CustomerResourceClass.java class.
plain -: @Path(“/plain/{customerNo : (\\w+)?}”) added in the top of CustomerResourceClass.java class method .
1 -: Parameter to the getCustomerNamePlain method .
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) |