RESTful web services JAXRS json example using jersey

This post is in continuation with web service tutorial (Part -8).

In previous post, we have created a very simple Restful web services(JAXRS) using jersey which returns xml. In this post, we will see Restful web services(JAXRS) using jersey which will return json as response.

Here are steps to create a simple Restful web services(JAXRS)  using jersey which will return json.
1) Create a dynamic web project using maven in eclipse named “JAXRSJsonExample”

2) We need to add jersey jars utility in the classpath.

Jersey internally uses Jackson for Json Handling, so it will be used to marshal pojo objects to JSON.

Now create pom.xml as follows:
pom.xml

Application configuration:

3) create web.xml as below:

Create bean class

4) Create a bean name “Country.java” in org.arpit.java2blog.bean.

Create CountryRestService

5) Create a controller named “CountryRestService.java”

@Path(/your_path_at_class_level) : Sets the path to base URL + /your_path_at_class_level. The base URL is based on your application name, the servlet and the URL pattern from the web.xml” configuration file.

@Path(/your_path_at_method_level): Sets path to base URL + /your_path_at_class_level+ /your_path_at_method_level

@Produces(MediaType.APPLICATION_JSON[, more-types ]): @Produces defines which MIME type is delivered by a method annotated with @GET. In the example text (“text/json”) is produced.

6) It ‘s time to do maven build.

Right click on project -> Run as -> Maven build

7) Provide goals as clean install (given below) and click on run

Run the application

8) Right click on project -> run as -> run on server
Select apache tomcat and click on finish
9) Test your REST service under: “http://localhost:8080/JAXRSJsonExample/rest/countries”.

You will get following output:

10) Now pass country id as parameter to url.
“http://localhost:8080/JAXRSJsonExample/rest/countries/3”.

click to begin
20KB .zip

Project structure:


We are done with Restful web services json example using jersey. If you are still facing any issue, please comment.

If you getting 404 error with above steps, you may need to follow below steps:


1) If you are getting this warning into your Tomcat startup console log, then it can cause the issue
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.j2ee.server: JAXRSJsonExample’ did not find a matching property.
This particular warning basically means that the element in Tomcat’s server.xml contains an unknown attribute source and that Tomcat doesn’t know what to do with this attribute and therefore will ignore it.
To resolve this in eclipse,
Remove the project from the server from the Server View. Right click on server -> add and remove
then remove project from server configuration.
Then run the project under the same server. Warning should be removed now
Or if warning still remains then
  • Go to server view
  • Double click on your tomcat server. It will open the server configuration.
  • Under server options check ‘Publish module contents to separate XML files’ checkbox.
  • Restart your server. This time your page will come without any issues.
2) Try to update Maven project.
Right click on project ->Maven-> update project
then

This should solve you issues.

7 Comments

  1. Krupaharan November 13, 2015
    • arpitmandliya November 13, 2015
    • Guest December 3, 2015
  2. anynonymums November 18, 2015
  3. Sruthi July 11, 2016
    • arpitmandliya July 11, 2016
  4. Sruthi July 12, 2016

Add Comment