Jackson not consuming the JSON root element

核能气质少年 提交于 2019-12-03 15:27:08
Sam Berry

There are two approaches I can think of. If this is a common occurrence in your application, I would recommend enabling unwrapping on your ObjectMapper. If this is a one-off situation, a wrapper object is not a bad option.

A. Enable Unwrapping

@JsonRootName will only apply if unwrapping is enabled on the ObjectMapper. You can accomplish this with a deserialization feature. Note that this will unwrap all requests:

public CustomObjectMapper() {
   super();
   enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
}

If you do not already have a custom ObjectMapper registered then you will need to add a provider to register your custom config with Jersey. This answer explains how do accomplish that.

B. Create a Wrapper

If you do not want to unwrap globally, you can create a simple wrapper object and omit the @JsonRootName annotation:

public class SampleObjectWrapper {
   public SampleObject sampleObject;
}

Then update your resource method signature to accept the wrapper:

public Response submitJsonRequest(SampleObjectWrapper sampleObjectWrapper, @Context HttpHeaders headers)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!