1

I am using Jersey to implement a REST service. I want to display JSON on the browser but instead i get XML.

@Path("/todos")
public class TodosResource {

// Allows to insert contextual objects into the class, 
// e.g. ServletContext, Request, Response, UriInfo
@Context
UriInfo uriInfo;
@Context
Request request;


// Return the list of todos to the user in the browser
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
  public List<Todo> getTodosBrowser() {
  List<Todo> todos = new ArrayList<Todo>();
  todos.addAll(TodoDao.instance.getModel().values());
  return todos; 
}

I Appreciate your help.

user2165029
  • 11
  • 1
  • 4

4 Answers4

4

Well, your code states that it produces XML and JSON. Depending on your browsers settings, it might request XML instead of JSON.

First, check that your code actually can generate JSON. Remove the MediaType.APPLICATION_XML from your @Produces annotation and test again.

If that works, you will need to tell your browser to request JSON. Add Accept: application/json to your request header.

How this is done depends on your client application. In JavaScript, this is done by adding something like

httpRequest.setRequestHeader('Accept', 'application/json'); 

depending on the framework you use. You can also test with curl on the command line

curl -H "Accept: application/json" http://yourhost/context/todos

If you type the URL into a browser, it is most likely sending the following accept header

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

because a browser wants to display something to the User (styled html). This is why the MediaType.APPLICATION_XML has precedence over your MediaType.APPLICATION_JSON.

phisch
  • 4,571
  • 2
  • 34
  • 52
  • 1
    +1 yes I don't think any browser sends `Accept: application/json` with requests. I assume the OP is typing the URL into the browser directly rather than using a REST tool or cURL. Most browsers have a `JSON` plugin that either adds the header or provides a way to add headers, for example the [Advanced REST Client](https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo/reviews?hl=en-US) for Chrome – andyb Mar 13 '13 at 11:20
0

Add jersey-json.jar

Add POJOMappingFeature to web.xml as initparam, then it will automatically convert the java list to json format.

<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>
Kris
  • 1,882
  • 1
  • 20
  • 23
0

1) Install browser extension e.g. Postman (Chrome) or Poster (FireFox) 2) Add header Accept:application/json in this extension

Marek Gregor
  • 3,691
  • 2
  • 26
  • 28
0

As I can see your method is mapped with GET. So you have to change @Produces({ MediaType.APPLICATION_JSON }). This will generate data in JSON form only and to see this in your browser you can plugin Advance Rest API (app) support for your Chrome browser. That will definitely work. All the best

 @GET
 @Produces({MediaType.APPLICATION_JSON})
 public List<Todo> getTodosBrowser() {
   List<Todo> todos = new ArrayList<Todo>();
   todos.addAll(TodoDao.instance.getModel().values());
   return todos; 
 }

 Web.xml-
 <?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"             xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
   <display-name>homepage_todaysdeal_products</display-name>
      <welcome-file-list>
        <welcome-file>default.jsp</welcome-file>
       </welcome-file-list>
     <servlet>
       <servlet-name>RESTService</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>"Mention Your Package Name here for GET/POST"</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>RESTService</servlet-name>
     <url-pattern>/rest/*</url-pattern>
     </servlet-mapping>
   </web-app>
Jeet
  • 61
  • 1
  • 1
  • 9