在Java资源中获取POST JSON时出错
我很难让Jersey RESTful服务正常工作 我得到以下错误在Java资源中获取POST JSON时出错,java,rest,jersey,marshalling,unmarshalling,Java,Rest,Jersey,Marshalling,Unmarshalling,我很难让Jersey RESTful服务正常工作 我得到以下错误 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(SAXU
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
这项服务是:
@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;
}
这是我想要接收的对象:
@XmlRootElement
public class AgentTO {
private String name;
private String phone;
public AgentTO(){}
public AgentTO(String phone, String name){
this.phone = phone;
this.name = name;
}
我是这样写的:
curl -H "Content-Type: application/json" -v -X POST -d '{"phone":"123","name":"asd2"}' http://127.0.0.1:8080/project/agents
我以前也做过类似的资源,但这似乎不起作用:p
你能帮我做这个吗
问候,,
Martin您可以使用Gson从body请求中解组JSON。
请检查。您确定异常来自于此,您正在发布json,但它正在尝试解组XML?我有另一个资源,我只是在执行上面提到的帖子时才遇到该错误。我尝试了CONTENT-JSON头,得到了“服务器拒绝了此请求,因为请求实体的格式不受请求方法的请求资源支持”异常
org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:806)
您显示的来自XML解组器,对于您发布的json毫无意义。您说它正在通过另一个代码?但问题是,当我更改任何注释(例如@PUT)时,它告诉我该方法不接受PUT。我的印象是,它错误地试图将JSON请求体解析为XML来填充AgentTO
实例。