当我将RestTemplate与String ResponseType一起使用时,它运行正常。
ResponseEntity<String> response = restTemplate.postForEntity(uri,
request, String.class);
返回Json字符串
{"code":0,"message":null,"data":"{\"companyId\":\"1\",\"tinyIdList\":[\"2\"],\"carId\":\"3\"}"}
虽然我使用Java POJO来保存响应,但Java Object的属性都为空
ResponseEntity<queryTinyListResponse> responseEntity = restTemplate.postForEntity(uri, request, queryTinyListResponse.class);
结果:
body:queryTinyListResponse{data=Data{tinyIdList=[], carId='null', companyId='null'}, code=0, msg='null'}
queryTinyListResponse.class:
@JsonIgnoreProperties(ignoreUnknown = true)
public class queryTinyListResponse implements Serializable {
public class Data{
private String companyId;
private List<String> tinyIdList=new ArrayList<String>();
private String carId;
//getter setter getCompanyId() etc..
}
public Data getData() {
return data;
}
//getter setter
private Data data;
private int code;
private String msg;
}
答案 0 :(得分:0)
您可以在application.properties/yml中使用以下配置:spring.jackson.default-property-inclusion=non_null
,然后我尝试重现类似的配置,并在您的响应字符串中找到
{"code":0,"message":null,"data":"{\"companyId\":\"1\",\"tinyIdList\":[\"2\"],\"carId\":\"3\"}"}
一个jeson语法错误,应该为{\"code\":0,\"message\":null,\"data\":{\"companyId\":\"1\",\"tinyIdList\":[\"2\"],\"carId\":\"3\"}}
,否则数据字段将被视为字符串而不是数据对象。
如果您想尝试一下自己,可以尝试以下代码:
package com.example.jacksondemo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.RequestEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class JacksonDemoApplication {
public static void main(String[] args) {
SpringApplication.run(JacksonDemoApplication.class, args);
}
}
@lombok.Data
@JsonIgnoreProperties(ignoreUnknown = true)
class QueryTinyListResponse implements Serializable {
//getter setter
private Data data;
private int code;
private String msg;
}
@lombok.Data
class Data implements Serializable {
private String companyId;
private List<String> tinyIdList=new ArrayList<String>();
private String carId;
//getter setter getCompanyId() etc..
}
@RestController
class QueryTinyListResponseController {
@PostMapping
public String query(){
return "{\"code\":0,\"message\":null,\"data\":{\"companyId\":\"1\",\"tinyIdList\":[\"2\"],\"carId\":\"3\"}}";
}
}
@RestController
class QueryTinyListResponseControllerClient {
@GetMapping("/client")
public QueryTinyListResponse query(){
RestTemplate restTemplate = new RestTemplate();
return restTemplate.postForEntity("http://localhost:8080/", RequestEntity.EMPTY, QueryTinyListResponse.class).getBody();
}
}
application.properties
spring.jackson.default-property-inclusion=non_null
答案 1 :(得分:0)
我找到了这个问题的答案。我应该使用static
内部类here
找到了!!解决方案实际上是很奇怪的。问题来自内部类。旧的结构是这样的:
public class MetaData{
...
public class Data {
....
public class Item{
...
}
}
}
内部类必须为static
,因为Jackson不能直接实例化内部类。
public class MetaData{
...
public static class Data {
....
public static class Item{
...
}
}
}
一切正常。希望能有所帮助!