Search code examples
javajsonrestjerseyglassfish-3

Hot to edit JSON object output with Jersey & Netbeans?


I've got a problem with my RESTful webservice - again :( Summary: When I call this URL:

http://localhost:8080/iOSWebServices/resources/credits/1

I get this JSON output:

{"id":1,"lastname":null,"firstname":"lalsdaksldlasds"}

but I want this JSON output:

{"creditRequest":{"id":1,"lastname":null,"firstname":"lalsdaksldlasds"}}

Long version: I am using netbeans, glassfish 3 and jersey 1.9.1 to provide some simple test webservices. the problem is, that all json outputs are missing the class name in the output...

For the example above: CreditRequest.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "creditRequest")
@XmlType(name = "creditRequestType")
@XmlAccessorType(XmlAccessType.FIELD)
public class CreditRequest {

    private Long id;
    private String firstname;
    private String lastname;

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
}

CreditRequest.java

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("credits")
@RequestScoped
public class CreditsResource {

    @Context
    private UriInfo context;

    /** Creates a new instance of CreditsResource */
    public CreditsResource() {
    }

    //OUTPUT AS ABOVE!
    @GET
    @Path("{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getCreditById(@PathParam("id") long id) {
        System.out.println("getCreditById");
        CreditRequest creditRequest = new CreditRequest();
        creditRequest.setId(new Long(1));
        creditRequest.setFirstname("lalsdaksldlasds");
        return Response.ok().entity(creditRequest).build();        
    }
}

The jersey user guide explains, how is SHOULD work - a quote...

Example 5.14. Keep XML root tag equivalent in JSON mapped JSON notation
    JSONConfiguration.mapped().rootUnwrapping(false).build()
and get the following JSON for our Contact bean:
Example 5.15. XML root tag equivalent kept in JSON using mapped notation
    {"contact":{ "id":"2" 
     ,"name":"Bob"
     ,"addresses":{"street":"Long Street 1"
                        ,"town":"Short Village"}}}

To do this I wrote a simple class implementing implements ContextResolver (also in the user guide)

JAXBContextResolver.java //Doesn't seem to work

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {

    private JAXBContext context;
    private Class[] types = {CreditRequest .class}; //FIXED

    public JAXBContextResolver() throws Exception {

        System.out.println("init");
        this.context = new JSONJAXBContext(
                JSONConfiguration.mapped().rootUnwrapping(false).build(), types);
    }

    @Override
    public JAXBContext getContext(Class<?> objectType) {
        System.out.println("get");
        for (Class type : types) {
            if (type == objectType) {
                return context;
            }
        }
        return null;
    }
}

When I start the webservice, the output defined in the JAXBContextResolver's constructor will be written "init" - but "get" won't....

In this project netbeans controlls the resources of my webservices - and again is initializes JAXBContextResolver when the server started - but there is still no rootUnwrapping :(

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
       <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Any ideas? I really don't know why there are no changes in my JSON output :(


Solution

  • I had the same problem last week. I found the solution in the web.xml file. After I removed the following lines:

    <init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
    </init-param>