java - HttpMessageNotReadable 异常 : JSON parse error: cannot deserialize instance of ArrayList out of start object token

标签 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,
                                                                        @RequestParam(value = "searchDate", required = false)
                                                                        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate searchDate) {}

我正在使用休息模板发送请求

placementResponseEntity = restTemplate.exchange(placementUriBuilder(accountId, searchDate), HttpMethod.GET,
          apiRequestEntity,new ParameterizedTypeReference<List<PlacementDetail>>() {});

使用辅助方法

 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;
      }

当我查看 SO 时,人们谈论发送对象并失败,因为创建的 JSON 格式错误,但这里这是一个 get api,我不明白问题的根源。

最佳答案

这通常是由于 RestTemplate 上缺少错误处理程序引起的。您的服务器响应错误,您的客户端尝试将其反序列化为 List<PlacementDetail> 。为了解决这个问题,您应该正确处理 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");
        }

    }

}

关于java - HttpMessageNotReadable 异常 : JSON parse error: cannot deserialize instance of ArrayList out of start object token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52884828/

相关文章:

java - 带有JDBC池的tomcat上下文异常

java - JAXB 将 XSD 转换为 Java 类

delphi - JSON 解析器的要求

java - 具有 Java 配置的 Spring Boot 自定义身份验证提供程序不起作用

java - 如何删除 "fullscreenmode"中的这个栏?

java - 如何简化注释

json - 如何在 Mattermost 中将机器人的直接消息发送给用户

javascript - 将输入值插入 JSON 数组

django - 如何使用 django rest 框架禁用 HTML 错误页面的返回?

休息 API : Obtaining Payment ID after redirect back to merchant site