Olá pessoal, resolvi criar uma api para fixar os conteudos passados no curso, primeiramente estou fazendo uma api vai mostrar a cotação do dolár diário, porém ao executar o programa é lançaco um erro interno (ERRO 500), verifiquei a URL que estou passando no postman e os dados são retornados, porem ao chamar pelo programa é gerado este erro e não consegui identificar, abaixo as classes que utilizei: Classe DTO
public class DolarDiarioDto {
private String cotacaoCompra;
private String cotacaoVenda;
private String dataHoraCotacao;
/* getters e setters*/
Classe Service
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import br.com.cotacaoapi.model.dto.DolarDiarioDto;
import br.com.cotacaoapi.util.BaseUrlConstante;
@Service
public class CotacaoService {
public DolarDiarioDto retornarValorDiario() {
RestTemplate restTemplate = new RestTemplate();
String uri = BaseUrlConstante.BASE_URL+ BaseUrlConstante.DOLAR_DIARIO+"'07-02-2021'";
System.out.println("### URL ####\n");
System.out.println(uri);
DolarDiarioDto retorno = restTemplate.getForObject("https://olinda.bcb.gov.br/olinda/servico/PTAX/versao/v1/odata/CotacaoDolarDia(dataCotacao=@dataCotacao)?%40dataCotacao='07-02-2021'", DolarDiarioDto.class);
return retorno;
}
}
Controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import br.com.cotacaoapi.model.dto.DolarDiarioDto;
import br.com.cotacaoapi.service.CotacaoService;
@RestController
@RequestMapping("/cotacao")
public class CotacaoApiController {
@Autowired
private CotacaoService cotacaoService;
@GetMapping("/dolardiario")
public ResponseEntity<DolarDiarioDto> exemplo(){
DolarDiarioDto dados = cotacaoService.retornarValorDiario();
return ResponseEntity.ok(dados);
}
}
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>br.com.cotacaoapi</groupId>
<artifactId>cotacaoapi</artifactId>
<version>0.0.1</version>
<name>cotacaoapi</name>
<description>API para cotação Dólar</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>