Problem to request via restTemplate

Asked

Viewed 26 times

-1

Hello I am developing a . jar that consults the dollar’s quotation value, through the request for an endpoint provided by the central bank. When I request via Postman json comes correctly, but by java using restTemplate the following error occurs:

java.lang.NoSuchMethodError: org.springframework.util.MimeType.<init>(Lorg/springframework/util/MimeType;)V
    at org.springframework.http.MediaType.<init>(MediaType.java:511) ~[spring-web-5.3.3.jar:5.3.3]
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:623) ~[spring-web-5.3.3.jar:5.3.3]
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:967) ~[spring-web-5.3.3.jar:5.3.3]
    at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:136) ~[spring-web-5.3.3.jar:5.3.3]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:93) ~[spring-web-5.3.3.jar:5.3.3]

Service class

@Override
    public Map<String,Object> consultaCotacao() {
        boolean ok = false;
        Gson g = new Gson();
        Map<String, Object> response = new HashMap();

        String filter = setFiltroRequisicao();

        HttpHeaders header = new HttpHeaders();
        header.set("Content-Type", MediaType.APPLICATION_JSON_VALUE);

        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<String> request = new HttpEntity<>(header);

        ParameterizedTypeReference<CotacaoMoeda> typeRef = new ParameterizedTypeReference<CotacaoMoeda>() {
        };

        String endpoint = ConstantsURL.CONSULTA_COTACAO_COMPRA + filter;

        try{
            ResponseEntity<CotacaoMoeda> responseEntity = restTemplate.exchange(endpoint,
                    HttpMethod.GET, request, typeRef);

            CotacaoMoeda cotacaoMoeda = responseEntity.getBody();

            response.put("cotacaoMoeda",cotacaoMoeda);
            response.put("HttpStatus", responseEntity.getStatusCode());

        }catch(Exception e){
            response.put("cotacaoMoeda",null);
            response.put("HttpStatus", 500);
            e.printStackTrace();
        }

Class Quotationcurrency

@Data
public class CotacaoMoeda {

    @JsonDeserialize(contentAs = Value.class)
    private Value value;

}

Value class

@Data
public class Value {

   private BigDecimal cotacaoCompra;

}

Json return

{
    "@odata.context": "https://was-p.bcnet.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata$metadata#_CotacaoDolarDia(cotacaoCompra)",
    "value": [
        {
            "cotacaoCompra": 5.36200
        }
    ]
}

Note: I tried to put in the return of restTemplate as List , but it did not work. Tbm I tried to put the value attribute as a list in the class

1 answer

0

I solved the problem by changing the version of the spring-web dependency to 5.2.9.RELEASE from the project. Soon after he started returning the Responseentity object correctly


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.