Web Services HTTP客户端错误异常$错误请求:400服务返回时请求错误[]

nle07wnf  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(133)

I'm running a service and it works perfectly when it returns some entidade. But when it doesn't find any, I get the following error:
org.springframework.web.client.HttpClientErrorException$BadRequest: 400 BAD REQUEST: [{ ***"entidades": []***, "saida": "" rua bandeirantes, 1578 - bairro: cent... (19970 bytes)] at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:186) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125) at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:818) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:776) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:710) at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:467) at br.mp.mpf.cened01.cargaIaRJ.RJServico.executarServico(RJServico.java:953) at br.mp.mpf.cened01.cargaIaRJ.RJServico.executarServicoByPagina(RJServico.java:857) at br.mp.mpf.cened01.cargaIaRJ.RJServico.executarComPaginacao(RJServico.java:752) at br.mp.mpf.cened01.cargaIaRJ.RJApplication.lambda$0(RJApplication.java:52) at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) at br.mp.mpf.cened01.cargaIaRJ.RJApplication.main(RJApplication.java:29) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

private RetornoServicoDto executarServico(String sentenca) 
            throws Exception {
        RetornoServicoDto retornoServico = new RetornoServicoDto();

        final String sentencaOri = sentenca;
        RestTemplate restTemplate = new RestTemplate();
       final Iterator<HttpMessageConverter<?>> iterator = restTemplate.getMessageConverters().iterator();
       while (iterator.hasNext()) {
        final HttpMessageConverter<?> converter = iterator.next();
          if (converter instanceof StringHttpMessageConverter) {
            iterator.remove();
          }       
       }
       restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

        try {         
            ResponseEntity<RetornoServicoDto> responseEntity = 
                    restTemplate.postForEntity(Constantes.SERVICO_URL,
                            sentencaOri, RetornoServicoDto.class);

            retornoServico = responseEntity.getBody();
            
        } catch (Exception e) {
            e.printStackTrace();
        }

        return retornoServico;
    }  

@Data
public class RetornoServicoDto {    
    private List<EntidadeDto> entidades; 
    private String saida;
    private String status;  
} 

@Data
public class EntidadeDto {
    private String entidade;
    private int id;
    private PosicaoDto posicao;
    private String texto;
}

What to do to work even when the entities list returns []?

j9per5c4

j9per5c41#

根据我从日志中看到的内容,您的响应对象以“[{...”开头,因此您的服务器端可能返回RetornoServicoDto列表,而不是您请求的单个对象,因为您的问题是一个错误请求,如果能看到处理请求的服务端以便更好地理解,那将是非常棒的。

相关问题