当前位置: 首页 > 知识库问答 >
问题:

调用WS REST时“由于输入结束,没有内容可映射到对象”

周昊乾
2023-03-14

考虑以下方法:

@POST
@Path("/device")
@Consumes("application/json")
@Produces("application/json")
public String putDevice(String jsonObject, @Context HttpHeaders headers, Admin admin)
{
    ObjectMapper mapper = new ObjectMapper();

    try
    {
        return updateDevice(mapper.readTree(jsonObject), Utils.extractUserInfo(headers), admin.getServer(), admin.getPort());
    }
    catch (JsonProcessingException e)
    {
        return e.getMessage();
    }
    catch (IOException e)
    {
        return e.getMessage();
    }
}

当我从Postman调用WS时,我得到以下异常:

08-Feb-2019 14:23:44.138 GRAVE [http-nio-8080-exec-7] com.sun.jersey.spi.container.ContainerResponse.mapMappableContainerException MappableContainerException MappableContainerException 中包含的异常无法映射到响应,重新抛出到 HTTP 容器 java.io.EOFException:由于输入结束,没有要映射到对象的内容 org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2766) 在 org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2682) atorg.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1308) at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419) at com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.readFrom(JacksonProviderProxy.java:139) at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:490) at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123) at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:86) at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153) at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:183) at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302) at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) atcom.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473) atcom.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409) at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) atorg.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)

值得一提的是,当我删除参数jsonObject并像这样编辑方法时,异常会消失:

@POST
@Path("/device")
@Consumes("application/json")
@Produces("application/json")
public Response putDevice(@Context HttpHeaders headers, Admin admin)
{
    String output = admin.toString();
    return Response.status(200).entity(output).build();
}

任何建议都将不胜感激。

共有1个答案

毕衡
2023-03-14

感谢Arnaud发布的链接,我了解到您只能有一个不带注释的参数,并且该参数将作为请求体进行处理。所以我做了必要的修改,下面是这个方法现在的样子:

@POST
@Path("/device")
@Consumes("application/json")
@Produces("application/json")
public String putDevice(String jsonString, @Context HttpHeaders headers)
{
    ObjectMapper mapper = new ObjectMapper();
    try
    {
    JSONObject jsonObject = new JSONObject(jsonString);
    return updateDevice(mapper.readTree(jsonString),
                        Utils.extractUserInfo(headers),
                        jsonObject.getString("server"),
                        jsonObject.getInt("port"));
}
    catch (JSONException e)
    {
        return e.getMessage();
    }
    catch (JsonProcessingException e)
    {
        return e.getMessage();
    }
    catch (IOException e)
    {
        return e.getMessage();
    }
}

我希望有一天它能帮助其他人解决同样的问题。

 类似资料: