Spring Boot 404 Bad Request错误Sping Boot for Open API

iih3973s  于 2024-01-06  发布在  Spring
关注(0)|答案(1)|浏览(137)
  1. @Service
  2. public class AirService {
  3. @Value("${api.key}")
  4. private String apiKey;
  5. @Value("${api.url}")
  6. private String apiUrl;
  7. @Autowired
  8. private RestTemplate restTemplate;
  9. @Autowired
  10. private HttpHeaders httpHeaders;
  11. Logger logger = LoggerFactory.getLogger(AirService.class);
  12. public double fetchTotalPrice() {
  13. logger.info("Fetching total price from Air France API...");
  14. httpHeaders.set("Content-Type", "application/hal+json");
  15. httpHeaders.set("API-Key", "apiKey");
  16. httpHeaders.set("AFKL-TRAVEL-Host", "KL");
  17. HttpEntity<String> entity = new HttpEntity<>(httpHeaders);
  18. logger.info("Sending request to API: {}", apiUrl);
  19. logger.info("Request body: {}", httpHeaders);
  20. try {
  21. ResponseEntity<FlightResponse> responseEntity = restTemplate.exchange(apiUrl, HttpMethod.POST, entity, FlightResponse.class);
  22. logger.info("Received response from API: {}", responseEntity);
  23. logger.info("Response status code: {}", responseEntity.getStatusCode());
  24. if (responseEntity.getStatusCode().is2xxSuccessful()) {
  25. FlightResponse ApiResponse = responseEntity.getBody();
  26. double totalPrice = airFranceApiResponse
  27. .getDestinationCities()
  28. .get(0)
  29. .getFlightProducts()
  30. .get(0)
  31. .getPrice()
  32. .getTotalPrice();
  33. logger.info("Total price retrieved: {}", totalPrice);
  34. return totalPrice;
  35. } else {
  36. logger.error("Error fetching total price: {}", responseEntity.getStatusCodeValue());
  37. return 0;
  38. }
  39. } catch (Exception e) {
  40. logger.error("Unexpected error occurred", e);
  41. return 0;
  42. }
  43. }
  44. }

字符串
错误信息:

  1. org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : "{"errors":[{"code":2000,"name":"OFA/TECHNICAL/CLIENT_ERROR","description":"Unreadable message"}]}" I am trying to fetching data from open Api, I am sure the headers.I use same request for Postman and it works but I couldn't implement truley spring boot.

xytpbqjk

xytpbqjk1#

我得到了相同的错误消息,因为我忘记了在从API客户端移动到代码(Bruno -> Bun/Svelte/Wretch)时包含请求体。
我不知道Java或Sping Boot ,但我猜是请求主体丢失或格式错误。你试过使用Content-Type: application/json吗?AFKLM API使用application/hal+json响应,但我不知道任何端点需要它。

相关问题