问题
I'm trying to implement a REST webservice with Jersey. I'm using JBoss AS 7.1.1 Final as my webserver.
The problem is happening whenever I attempt to use a String[] as part of my method's signature:
@Override
@POST
@Path("/getVersion")
public ControllerVersion getVersion(String deviceID, String[] macAddresses)
{
System.out.println("Received "+deviceID+", and macAddress[0] "+macAddresses[0]);
return new ControllerVersion();
}
The exception that's being thrown on the server side is as follows:
15:15:09,487 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/DCPController2].[JerseyRest]] (http--0.0.0.0-8080-1) Servlet.service() for servlet JerseyRest threw exception: java.io.EOFException: No content to map to Object due to end of input
at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2775) [jackson-all-1.9.9.jar:1.9.9]
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2691) [jackson-all-1.9.9.jar:1.9.9]
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315) [jackson-all-1.9.9.jar:1.9.9]
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419) [jackson-all-1.9.9.jar:1.9.9]
at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.readFrom(JacksonProviderProxy.java:139) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:46) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:183) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1511) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1442) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538) [jersey-bundle-1.17.1.jar:1.17.1]
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716) [jersey-bundle-1.17.1.jar:1.17.1]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_45]
I made sure to enable POJOMapping by adding the following to my web.xml:
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
I'm using the following jars:
asm-3.3.1.jar
jackson-all-1.9.9.jar
jersey-bundle-1.17.1.jar
jersey-json-1.7.jar
log4j-1.2.16.jar
Note that I have also disabled the jaxrs subsystem from my standalone.xml file in JBoss.
This attempt of mine to use Jersey is mainly 'cause I was having problems with using CXF-RS and Jackson. You can check my other posts for more information following the URL: Parsing String[] Request Parameter through REST with Apache CXF
Your help is greatly appreciated.
UPDATE: I'm making the call using Apache's HttpPost in my client , and putting my request parameters as follows:
StringEntity params = new StringEntity("{\"deviceID\":\"testDevice\",\"macAddresses\":[1,2,3]}");
回答1:
What is your reason behind using @POST rather than @GET? The call appears to be more suited for GET unless you are passing a large list of mac addresses to the call.
If possible given your requirements, using the following should work:
@GET
@Path("device/{deviceId}/version")
public ControllerVersion getVersion(@PathParam("deviceId") String deviceId, @QueryParam("mac") List<String> macAddresses) {
...
}
The uri to call the endpoint would look like the following:
/device/12345/version?mac=[Some mac address]&mac=[Another mac address].
Which you could call using an instance Apache's HttpGet. Jersey will automatically convert the duplicate keys into a list of string values.
I would also avoid the use of verbs in your paths. "device/{id}/version" or "/version/{id}" would be more RESTful. Additionally, if you are returning json don't forget to add @Produces(MediaType.APPLICATION_JSON)
UPDATE: I was able to reproduce the problem. And based on your POST requirements, the following works:
@POST
@Path("/version")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ControllerVersion getVersion(Device device)
{
System.out.println("Received "+device.getDeviceID()+", and macAddress[0] "+ device.getMacAddresses()[0]);
return new ControllerVersion();
}
Added POJO class:
public class Device
{
@JsonProperty
String deviceID;
@JsonProperty
String[] macAddresses;
public String getDeviceID()
{
return deviceID;
}
public void setDeviceID(String deviceID)
{
this.deviceID = deviceID;
}
public String[] getMacAddresses()
{
return macAddresses;
}
public void setMacAddresses(String[] macAddresses)
{
this.macAddresses = macAddresses;
}
}
My first assumption is that Jackson cannot map your request to a generic object as it doesn't have the properties you've specified in the json, hence your exception. Once you define a contract that maps to the values, the deserialization can take place.
来源:https://stackoverflow.com/questions/19426630/sending-string-through-rest-with-jersey