使用Jersey通过REST发送String []

时间:2013-10-17 12:21:57

标签: jersey jackson jax-rs jboss7.x

我正在尝试使用Jersey实现REST Web服务。我正在使用JBoss AS 7.1.1 Final作为我的网络服务器。

每当我尝试使用String []作为我方法签名的一部分时,问题就出现了:

@Override
@POST
@Path("/getVersion")
public ControllerVersion getVersion(String deviceID, String[] macAddresses)
{
    System.out.println("Received "+deviceID+", and macAddress[0] "+macAddresses[0]);
    return new ControllerVersion();
}

在服务器端抛出的异常如下:

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]

我确保通过在我的web.xml中添加以下内容来启用POJOMapping:

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

我正在使用以下罐子:

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

请注意,我还从JBoss的standalone.xml文件中禁用了jaxrs子系统。

我使用泽西岛的这种尝试主要是因为我在使用CXF-RS和杰克逊时遇到了问题。您可以在URL后面查看我的其他帖子以获取更多信息: Parsing String[] Request Parameter through REST with Apache CXF

非常感谢您的帮助。

更新 我在我的客户端使用Apache的HttpPost进行调用,并将我的请求参数设置如下:

StringEntity params = new StringEntity(“{\”deviceID \“:\”testDevice \“,\”macAddresses \“:[1,2,3]}”);

1 个答案:

答案 0 :(得分:1)

使用@POST而不是@GET的原因是什么? 除非您将大量MAC地址传递给呼叫,否则呼叫似乎更适合GET。

如果可能,根据您的要求,使用以下内容应该有效:

@GET
@Path("device/{deviceId}/version")
public ControllerVersion getVersion(@PathParam("deviceId") String deviceId, @QueryParam("mac") List<String> macAddresses) {
   ...
}

调用端点的uri如下所示:

/device/12345/version?mac=[Some mac address]&mac=[Another mac address].

您可以使用Apache的HttpGet实例调用它。 Jersey会自动将重复的密钥转换为字符串值列表。

我也会避免在你的路径中使用动词。 “device / {id} / version”或“/ version / {id}”会更加RESTful。另外,如果您要返回json,请不要忘记添加@Produces(MediaType.APPLICATION_JSON)

更新:我能够重现这个问题。根据您的POST要求,以下工作:

@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();
}

添加了POJO类:

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;
    }
}

我的第一个假设是Jackson无法将您的请求映射到通用对象,因为它没有您在json中指定的属性,因此您的例外。一旦定义了映射到值的契约,就可以进行反序列化。