2

I send an HTTP request to the server with content body in JSON form as follows.

CLIENT

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

This converts my content into JSON and adds root element too. I use a naming strategy to convert my property names in a form and similarly use AnnottaionIntrospector to convert rootName into same form.

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

The root element in Annotation was AE which was converted to ae inside AnnotationIntrospector, similarly keys are shortened using PropertyNamingStrategy.

SERVER

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

The resource which is recieved on the server is LinkedHasMap of the JSON sent from client.

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

As one can see the modified root name is present.

However, objectMapper.convertValue(resource, AE.class); is not converting it into AE POJO correctly and gives the error:

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:]

I have applied UNWRAP_ROOT_VALUE, but still it's giving error. I guess UNWRAP_ROOT_VALUE works only on a Json string ??

If from cliemt I don't wrap root value and send json like,

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

without root element I can successfully convert resource to POJO (using objectMapper.convertValue(resource, AE.class)).

But it's not working after wrapping. Any suggestions for this??

Siddharth Trikha
  • 2,648
  • 8
  • 57
  • 101
  • Have you tried to just use a String parameter in your resource method, instead of Object? – Paul Samsotha Apr 26 '16 at 11:37
  • @peeskillet: You mean recieve content in method like `public void handlePutRequest(String resource) {` ??? – Siddharth Trikha Apr 26 '16 at 11:41
  • 1
    See also [here](http://stackoverflow.com/a/28310779/2587435), if you want to configure the unwrap globally. This way you can just accept the `AE` parameter, instead of explicitly deserializing it in every method – Paul Samsotha Apr 26 '16 at 11:49
  • @peeskillet: Using that gives a JSONstring, but the exception is there: `"ae" : { "api" : "THA", "aei" : "", "rr" : false } org.codehaus.jackson.map.JsonMappingException: Root name 'ae' does not match expected ('AE') for type [simple type, class cdot.onem2m.resource.xsd.AE]` – Siddharth Trikha Apr 26 '16 at 11:50
  • Try adding `@JsonRootName("ae")` onto the class, see what happens – Paul Samsotha Apr 26 '16 at 11:52
  • I cannot edit my POJO. It's a generated one. My POJO has `@XmlRootElement(name = "AE")`. Thus I use `JaxbAnnotationIntrospector`, to serialize root element from `AE` to `ae`. Now either I can revert it back or unwrap root. Any ideas? – Siddharth Trikha Apr 26 '16 at 11:55
  • Not sure. It's out of the scope of my knowledge :-( – Paul Samsotha Apr 26 '16 at 12:00

0 Answers0