简体   繁体   中英

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.

When I am trying to send MyObj as a request to a POST endpoint as following:

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:

{
                "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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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