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??