Java HttpMessageTreadable例外:JSON解析错误:无法反序列化ArrayList的实例超出起始对象标记
我有一个终点Java HttpMessageTreadable例外:JSON解析错误:无法反序列化ArrayList的实例超出起始对象标记,java,json,rest,jackson,spring-rest,Java,Json,Rest,Jackson,Spring Rest,我有一个终点 @GetMapping(value = "/accounts/{accountId}/placementInfo", headers = "version=1") @ResponseStatus(HttpStatus.OK) public List<PlacementDetail> findPlacementDetailByPlacementInfoAtTime(@PathVariable("accountId") Long accountId,
@GetMapping(value = "/accounts/{accountId}/placementInfo", headers = "version=1")
@ResponseStatus(HttpStatus.OK)
public List<PlacementDetail> findPlacementDetailByPlacementInfoAtTime(@PathVariable("accountId") Long accountId,
@RequestParam(value = "searchDate", required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate searchDate) {}
当我看到SO时,人们谈论发送对象和失败,因为创建的JSON格式错误,但这里是一个get api,我不理解问题的根源 这通常是由于您的
RestTemplate
上缺少错误处理程序造成的。服务器响应错误,客户端尝试将其反序列化到列表
。为了解决这个问题,您应该正确处理HTTP错误代码
请参阅下面的代码片段
@Configuration
public class ClientConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
.errorHandler(new ClientErrorHandler())
.build();
}
public class ClientErrorHandler implements ResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
// check if HTTP status signals an error response
return !HttpStatus.OK.equals(httpResponse.getStatusCode());
}
@Override
public void handleError(ClientHttpResponse httpResponse) throws IOException {
// handle exception case as you see fit
throw new RuntimeException("Error while making request");
}
}
}
它期望
GET
返回一个PlacementDetail
数组,它正在接收一个对象(即以{
开头的json)。使用curl或类似方法检查返回的json。当api返回一个对象的列表时,它可以正常工作!这是预期的行为吗?如何?尝试了解api何时返回列表它将如何获取数组?抱歉,这是由某人编写的,我正在尝试了解像list
这样的容器类型通常被序列化为json数组eserialiser需要一个json数组,它将从中创建一个列表
。当postman发出请求时,api会给出正确的响应!当响应包含列表中的一个对象时,它也不会导致问题。您是否正在测试来自postman的完全相同的请求(即邮递员请求使用searchDate
完成,您从code执行的请求也使用searchDate
完成)?
private String placementUriBuilder(long accountId, LocalDate searchDate) throws IOException {
String resourceUri = ACCOUNT_RESOURCE_URI_START + accountId + PLACEMENT_DETAIL_RESOURCE_URI_END;
String url;
if(searchDate != null) {
url = UriComponentsBuilder.fromUri(serverUri.getUri()).path(resourceUri).queryParam("searchDate", searchDate.format(DateTimeFormatter.ISO_DATE)).build().toUriString();
} else {
url = UriComponentsBuilder.fromUri(serverUri.getUri()).path(resourceUri).build().toUriString();
}
return url;
}
@Configuration
public class ClientConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
.errorHandler(new ClientErrorHandler())
.build();
}
public class ClientErrorHandler implements ResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
// check if HTTP status signals an error response
return !HttpStatus.OK.equals(httpResponse.getStatusCode());
}
@Override
public void handleError(ClientHttpResponse httpResponse) throws IOException {
// handle exception case as you see fit
throw new RuntimeException("Error while making request");
}
}
}