简体   繁体   English

Spring Resttemplate将对象解析为字符串

[英]Spring Resttemplate parses Object as String

I have the following model: 我有以下模型:

public class MyObj implements Serializable {

        private Long id;

        private Object body;
        ..........

        public Object getBody() {
            return body;
        }

        public void setBody(Object body) {
            this.body = body;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        ............

}

The body field contains a JSON object. 正文字段包含一个JSON对象。

When I am trying to send MyObj as a request to a POST endpoint as following: 当我尝试将MyObj作为请求发送到POST端点时,如下所示:

restTemplate().postForObject("url",myObj,String.class);

the following request will be sent: 将发送以下请求:

{
                "id": 6,
                "body": "{\"Status: \":\"1\",\"Date: \":\"2017-9-12 11:3:51.328\",\"Source: \":\"xxx\", .....}",
                ......
}

which is not the result I want since body seems to be parsed as String while I want it to be parsed like this: 这不是我想要的结果,因为body似乎被解析为String,而我希望被解析为:

{
                "id": 6,
                "body": {"Status": "1", "Date": "2017-10-3 16:39:58.591", "Source": "xxx"},
                ......
}

Any help appreciated. 任何帮助表示赞赏。

Solution was the following : 解决方法如下:

@JsonRawValue
private Object body;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM