Comunidade

Ask a Question
Back to All

Obtenho sucesso na resposta porém o servidor me devolve null.

Sou desenvolvedor e estou consumindo a api usando Webclient - spring boot + java. Obtenho a resposta 200 ok. Porém no console eu obtenho:

MelhorEnvioResponse(id=null, name=null, price=null, customPrice=null, discount=null, currency=null, deliveryTime=null, deliveryRange=null, customDeliveryTime=null, customDeliveryRange=null, packages=null, dimensions=null)

Estou usando o token do sandbox;

Não estou usando refresh_token;

Segue os principais parâmetros...

@Service
public class MelhorEnvioService {
    private final WebClient webClient;

    public MelhorEnvioService() {
        this.webClient = WebClient.builder()
                .baseUrl(ApiTokenIntegracao.URL_MELHOR_ENVIO_SAND_BOX + "api/v2/me/shipment")
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
                .defaultHeader(HttpHeaders.USER_AGENT, "[email protected]")
                .build();
    }

    public Mono<MelhorEnvioResponse> calculateShipping() {
        MelhorEnvioRequest request = new MelhorEnvioRequest();

        try {
            return webClient.post()
                    .uri("/calculate")
                    .header(HttpHeaders.AUTHORIZATION, "Bearer " + ApiTokenIntegracao.TOKEN_MELHOR_ENVIO)
                    .body(BodyInserters.fromValue(request))
                    .exchange()
                    .flatMap(response -> response.bodyToMono(MelhorEnvioResponse.class))
                    .doOnSuccess(response -> System.out.println(response.toString()));

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class MelhorEnvioRequest implements Serializable {

    private From from;
    private To to;
    private List<ProductDto> products;
    private Options options;

}
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class MelhorEnvioResponse implements Serializable {
    private Integer id;
    private String name;
    private String price;
    @JsonProperty(value = "custom_price")
    private String customPrice;
    private String discount;
    private String currency;
    @JsonProperty(value = "delivery_time")
    private Integer deliveryTime;
    @JsonProperty(value = "delivery_range")
    private DeliveryRange deliveryRange;
    private Integer customDeliveryTime;
    private CustomDeliveryRange customDeliveryRange;
    private Packages packages;
    private Dimensions dimensions;

}
@RestController
@RequestMapping(value = "/shipment")
public class CalculateShipping {

    @Autowired
    private MelhorEnvioService service;

    @PostMapping("/consult")
    public Mono<ResponseEntity<MelhorEnvioResponse>> calculateShipping() {
        return service.calculateShipping()
                .map(response -> ResponseEntity.ok(response))
                .onErrorResume(error -> Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build()));
    }

}

Desde já agradeço a atenção;

Estou à disposição.