[英]Java deserializing part of the JSON into object and the rest into JsonObject
I was looking everywhere how to deserialize part of the JSON into an object and the rest into JsonObject. 我到处寻找如何将部分JSON反序列化为对象,其余部分反序列化为JsonObject。
for example: 例如:
{
"id" : "123",
"message" : {"subject" : "test sub" , "body" : "test body"}
}
I want to deserialize this JSON into this class: 我想将此JSON反序列化为此类:
public class className {
private String id;
private transient JsonObject message;
// getters and setters
}
The problem is that after the deserialization I get empty object {}
inside "message". 问题是在反序列化后我在“message”中得到空对象
{}
。
Does anyone have any idea about it? 有没有人对它有任何想法?
====================================================== ================================================== ====
EDIT: 编辑:
A little more info, I am using Spring MVC, the JSON is being sent by POST message into my controller. 更多信息,我使用Spring MVC,JSON正通过POST消息发送到我的控制器。
The controller function looks like this: 控制器功能如下所示:
public @ResponseBody String publish(@RequestBody final className input, final HttpServletRequest request,
final HttpServletResponse response) {
//input.message = {}
}
That is because Spring tries to parse your object with Jackson library while your object holds Gson objects (which can't be parsed with Jackson). 这是因为Spring试图用Jackson库解析你的对象,而你的对象持有Gson对象(不能用Jackson解析)。
Please refer Configure Gson in Spring . 请参考Spring中的Configure Gson 。
@Configuration
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter < ? >> converters) {
GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
converters.add(gsonHttpMessageConverter);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.