0

I'm having trouble getting a Jersey RESTful Service working.

I'm getting the following error

    The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
    org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:806)
    org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:602)
    org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:399)
    com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalJAXBElementFromJSON(BaseJSONUnmarshaller.java:111)
    com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalFromJSON(BaseJSONUnmarshaller.java:100)
    com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider.readFrom(JSONRootElementProvider.java:129)
    com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:111)
    com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488)
    com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123)
    com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:46)
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153)
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:183)
    com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
* Closing connection #0

The service is:

@Path("/agents")
public class AgentService {

    @POST 
    @Consumes({MediaType.APPLICATION_JSON + ";charset=utf-8"})
    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public String createAgent(AgentTO input) {
        try{
//          Agent agent = input.createAgent()
//          PersistenceService.getInstance().save(agent);
            return input.getName();
        } catch (Exception se) {
            se.printStackTrace();
        }
        return null;
    }

And here is the object that I want to recieve:

@XmlRootElement
public class AgentTO {

    private String name;
    private String phone;

    public AgentTO(){}

    public AgentTO(String phone, String name){
        this.phone = phone;
        this.name = name;               
    }

I'm doing the POST like this:

curl -H "Content-Type: application/json" -v -X POST -d '{"phone":"123","name":"asd2"}' http://127.0.0.1:8080/project/agents

I've made Resources before that are just like this one, but this doesn't seems to want to work :P

Could you help me with this?

Regards, Martin

mcarreiro
  • 13
  • 3
  • Are you sure the exception is coming from there, you're posting json, but it's trying to unmarshal XML? – Sotirios Delimanolis Mar 28 '13 at 18:44
  • I have another resource, and I'm only getting that error when doing the POST mentioned in above. I've tried with out the CONTENT-JSON header, i'm getting "The server refused this request because the request entity is in a format not supported by the requested resource for the requested method" – mcarreiro Mar 28 '13 at 18:45
  • The exception `org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:806)` you showed comes from the XML unmarshaller, makes no sense for the json you are posting. – Sotirios Delimanolis Mar 28 '13 at 18:46
  • You say it's going through another code? But the thing is when I change any of the Annotations, for example to @PUT, it tells me that the method doesn't accept PUT – mcarreiro Mar 28 '13 at 19:13
  • I'm under the impression that it is wrongly trying to parse the JSON request body as XML to populate a `AgentTO` instance. – Sotirios Delimanolis Mar 28 '13 at 19:16
  • Is there a way to debug that? Or to know if that's what is happening? – mcarreiro Mar 28 '13 at 19:24
  • If you're in Eclipse, Ctrl+Shift+T to find the class `SAXUnmarshaller` or maybe your jersey Servlet, point a breakpoint at the entry point for your app (eg, servlet service() method or the called method on `SAXUnmarshaller`) and run in debug mode. – Sotirios Delimanolis Mar 28 '13 at 19:27

1 Answers1

0

You can use Gson to unmarshall the JSON from the body request. Please check here.

Community
  • 1
  • 1
Octavian Ionel
  • 423
  • 6
  • 17