Oi Rodrigo, conforme solicitado seguem os dados:
Existe alguma forma de enviar os dados de acesso da API somente para você?
Como esta API não é publica não posso publicar aqui os dados de conexão.
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.6.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>br.com.mycompany.authorizationapi</groupId>
<artifactId>authorizationapi</artifactId>
<version>1</version>
<name>authorizationapi</name>
<description>testesteste</description>
<properties>
<java.version>1.8</java.version>
<log4j2.version>2.17.0</log4j2.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</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>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</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>
</project>
WebClient
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration
public class WebClientConfiguration {
@Value("${authorizationapi.base_url}")
private String baseUrl;
@Bean
public WebClient createWebClient() {
return WebClient.builder().baseUrl(baseUrl)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
}
Service
public Mono<AuthorizationModelDto> recuperaToken() {
String uri = "?grant_type=" + grantType + "&" + "client_id=" + clientId + "&client_secret=" + clientSecret
+ "&username=" + userName + "&password=" + password;
Mono<AuthorizationModelDto> sxx = webClientConfiguration.createWebClient().post()
.uri(uri)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(null)
.retrieve()
.bodyToMono(AuthorizationModelDto.class);
sxx.subscribe(response -> retornarDados(response));
return sxx;
}
private void retornarDados(AuthorizationModelDto teste) {
System.out.println("########################################");
System.out.println("ENTREI NO MÉTODO!"+teste.getAcessToken());
System.out.println("ENTREI NO MÉTODO2!"+teste.getTokenType());
System.out.println("ENTREI NO MÉTODO3!"+teste.getInstanceUrl());
System.out.println("ENTREI NO MÉTODO4!"+teste.getIssuedAt());
System.out.println("ENTREI NO MÉTODO5!"+teste.getSignature());
System.out.println("ENTREI NO MÉTODO6!"+teste.getId());
System.out.println("########################################");
}
Controller
@PostMapping
public Mono<AuthorizationModelDto> retornarToken() {
Mono<AuthorizationModelDto> retorno = authorizatorionService.recuperaToken1();
System.out.println(retorno);
return retorno;
}