2

我向服务器发送一个 HTTP 请求,内容为 JSON 格式,如下所示。

客户

String json = null;

            objectMapper = new ObjectMapper();
            objectMapper.SerializationConfig(Feature.WRAP_ROOT_VALUE. true);
            objectMapper.setPropertyNamingStrategy(new LongNameShortNameNamingStrategy());
            objectMapper.setAnnotationIntrospector(new SerializingAnnotionIntrospector());
            json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);;

            System.out.println("CONVERTED JSON CONTENT" + json);

                HttpEntity entity = new ByteArrayEntity(json.getBytes("UTF-8"));
                ( (HttpPut) httpUriRequest).setEntity(entity);
                System.out.println("ENTITY" + entity);

这会将我的内容转换为 JSON 并添加根元素。我使用命名策略将我的属性名称转换为一种形式,并类似地用于AnnottaionIntrospector将 rootName 转换为相同的形式。

{
  "ae" : {
    "api" : "THA",
    "aei" : "",
    "rr" : false
  }

Annotation 中的根元素AE被转换为aeinside AnnotationIntrospector,类似地使用PropertyNamingStrategy.

服务器

@PUT
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public void handlePutRequest(Object resource) {

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
        objectMapper.setPropertyNamingStrategy(new LongNameShortNameNamingStrategy());
        objectMapper.setAnnotationIntrospector(new DeserializingAnnotionIntrospector());

    objectMapper.convertValue(resource, AE.class);

服务器上接收到的资源是LinkedHasMap客户端发送的 JSON。

 CONTENT: {ae={api=THA, aei=, rr=false}}

可以看到修改后的根名称存在。

但是,objectMapper.convertValue(resource, AE.class);没有将其AE正确转换为 POJO 并给出错误:

resteasy-servlet threw exception: org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalArgumentException: Root name 'ae' does not match expected ('AE') for type [simple type, class cdot.onem2m.resource.xsd.AE]
 at [Source: N/A; line: -1, column: -1]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:340) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:214) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:540) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:]

我已经申请UNWRAP_ROOT_VALUE了,但它仍然给出错误。我想UNWRAP_ROOT_VALUE只适用于 Json 字符串?

如果从客户那里我不包装根值并发送 json 之类的,

{api=THA, aei=, rr=false}

没有根元素,我可以成功地将资源转换为 POJO(使用objectMapper.convertValue(resource, AE.class))。

但是打包后就不行了。对此有什么建议吗??

4

0 回答 0