RestTemplate 将 JSON 数组映射到对象列表
RestTemplate Map JSON array to list of objects
我在使用 RestTemplate 将下面 JSON 响应输出中的环境 LetakTandaTanganResponseDto 映射到 LetakTandaTanganResponseDto 对象列表时遇到问题。
我收到 URL 的回复,看起来像:
{
"code": "00",
"desc": "SUCCESS",
"body": [
{
"id": 71,
"createdate": null,
"lx": "104",
"ly": "632",
"page": 1,
"prfKe": 0,
"rx": "260",
"ry": "632",
"ttdKe": 1,
"formatItemId": null,
"formatPdfId": 10
},
{
"id": 72,
"createdate": null,
"lx": "485",
"ly": "629",
"page": 1,
"prfKe": 0,
"rx": "692",
"ry": "629",
"ttdKe": 2,
"formatItemId": null,
"formatPdfId": 10
},
{
"id": 73,
"createdate": null,
"lx": "585",
"ly": "729",
"page": 1,
"prfKe": 0,
"rx": "792",
"ry": "729",
"ttdKe": 3,
"formatItemId": null,
"formatPdfId": 10
}
]
}
RestTemplate代码。
ResponseEntity<List<LetakTandaTanganResponseDto>> letakTTDResponse = restTemplate.exchange(uri, HttpMethod.GET, entity, new ParameterizedTypeReference<List<LetakTandaTanganResponseDto>>() {
});
if (letakTTDResponse != null && letakTTDResponse.hasBody()) {
listletakTTD = letakTTDResponse.getBody();
}
return listletakTTD;
如果我碰巧使用了自定义值对象,比如:
@Data
@ToString
public class LetakTandaTanganResponseDto {
public String code;
public String desc;
public Body body;
@Data
@ToString
public class Body {
public Long id;
public Integer ttd_ke;
public Integer page;
public String lx;
public String ly;
public String rx;
public String ry;
public String createdate;
public Integer prfKe;
public Long formatItemId;
public Long formatPdfId ;
}
}
请问如何将上面提到的JSON列表结构映射到一个对象?
尝试根据 json 响应
更改您的响应 class
public class LetakTandaTanganResponseDto {
public String code;
public String desc;
public List<Body> body;
// ...
}
你的回答是
ResponseEntity<LetakTandaTanganResponseDto> letakTTDResponse = restTemplate.exchange(uri, HttpMethod.GET, entity, new ParameterizedTypeReference<LetakTandaTanganResponseDto>() {
});
if (letakTTDResponse != null && letakTTDResponse.hasBody()) {
listletakTTD.setBody( letakTTDResponse.getBody());
}
return listletakTTD;
我在使用 RestTemplate 将下面 JSON 响应输出中的环境 LetakTandaTanganResponseDto 映射到 LetakTandaTanganResponseDto 对象列表时遇到问题。 我收到 URL 的回复,看起来像:
{
"code": "00",
"desc": "SUCCESS",
"body": [
{
"id": 71,
"createdate": null,
"lx": "104",
"ly": "632",
"page": 1,
"prfKe": 0,
"rx": "260",
"ry": "632",
"ttdKe": 1,
"formatItemId": null,
"formatPdfId": 10
},
{
"id": 72,
"createdate": null,
"lx": "485",
"ly": "629",
"page": 1,
"prfKe": 0,
"rx": "692",
"ry": "629",
"ttdKe": 2,
"formatItemId": null,
"formatPdfId": 10
},
{
"id": 73,
"createdate": null,
"lx": "585",
"ly": "729",
"page": 1,
"prfKe": 0,
"rx": "792",
"ry": "729",
"ttdKe": 3,
"formatItemId": null,
"formatPdfId": 10
}
]
}
RestTemplate代码。
ResponseEntity<List<LetakTandaTanganResponseDto>> letakTTDResponse = restTemplate.exchange(uri, HttpMethod.GET, entity, new ParameterizedTypeReference<List<LetakTandaTanganResponseDto>>() {
});
if (letakTTDResponse != null && letakTTDResponse.hasBody()) {
listletakTTD = letakTTDResponse.getBody();
}
return listletakTTD;
如果我碰巧使用了自定义值对象,比如:
@Data
@ToString
public class LetakTandaTanganResponseDto {
public String code;
public String desc;
public Body body;
@Data
@ToString
public class Body {
public Long id;
public Integer ttd_ke;
public Integer page;
public String lx;
public String ly;
public String rx;
public String ry;
public String createdate;
public Integer prfKe;
public Long formatItemId;
public Long formatPdfId ;
}
}
请问如何将上面提到的JSON列表结构映射到一个对象?
尝试根据 json 响应
更改您的响应 classpublic class LetakTandaTanganResponseDto {
public String code;
public String desc;
public List<Body> body;
// ...
}
你的回答是
ResponseEntity<LetakTandaTanganResponseDto> letakTTDResponse = restTemplate.exchange(uri, HttpMethod.GET, entity, new ParameterizedTypeReference<LetakTandaTanganResponseDto>() {
});
if (letakTTDResponse != null && letakTTDResponse.hasBody()) {
listletakTTD.setBody( letakTTDResponse.getBody());
}
return listletakTTD;