【问题标题】:Getting exception org.springframework.web.client.HttpClientErrorException: 400 Bad Request when using restTemplate.postForEntity使用 restTemplate.postForEntity 时获取异常 org.springframework.web.client.HttpClientErrorException: 400 Bad Request
【发布时间】:2021-10-19 10:27:33
【问题描述】:
public String getOrderdata(final String poNumber) throws Exception {
        String apiResponse = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String url = ApiHost + ApiPath;
            JSONObject requestJson = new JSONObject();
            requestJson.put("orderNumber", poNumber);
            HttpEntity<String> request = new HttpEntity<>(requestJson.toString(), getHttpHeaders());
            log.info("request is " + request);
            log.info("Api url={}", url);
            ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
            log.info("Inside ServiceImpl, response is {}", response);
            if (response != null && response.hasBody() && response.getStatusCode() == HttpStatus.OK) {
                apiResponse = objectMapper.readValue(response.getBody(), String.class);
            } else if (response != null && response.getStatusCode() == HttpStatus.BAD_REQUEST) {
                log.info("event=getOrderdata: Received bad request exception from Api and Response is {} : ", response);
            } else {
                log.error("event=getOrderdata: Response is null= {} : ", response);
                throw new RuntimeException("Error While fetching details from api");
            }
        } catch (HttpClientErrorException e) {
            log.info("event=getOrderdata: HttpClientErrorException error=", e);
        } catch (Exception exe) {
            log.error("event=getOrderdata: error=", exe);
            //throw exe;
        }
        return apiResponse;
    }

    public HttpHeaders getHttpHeaders() {
        HttpHeaders headers = new HttpHeaders();
        try {
            val token = Client.getAccessToken(roleArn, scope);
            headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + token);
            headers.setContentType(MediaType.APPLICATION_JSON);
            return headers;
        } catch (Exception ex) {
            throw new RuntimeException("Error trying to generate token", ex);
        }
    }

我正在使用 springboot restTemplate.postForEntity()。我能够通过邮递员并尝试为此编写java代码。我正在使用 post call 打 api。对于此端点令牌和以下 json 正文是必需的。我能够生成令牌,并且下面的请求是通过上面的 java 代码形成的,但是在行号 restTemplate.postForEntity() 我收到 HttpClientErrorException: 400 Bad request。我正在记录响应字符串,但由于上述异常,它没有打印。请帮帮我。

JsonBody: { “订单号”:“PO0012345” }

形成的请求是:

 request is <{"orderNumber":"PO0012345"},{Authorization=[Bearer eyExampleToken], Content-Type=[application/json]}>

请找到具有邮递员请求正文的附件。点击这个链接打开它 enter image description here

【问题讨论】:

  • 在不知道邮递员发送的内容的情况下,您希望我们神奇地知道发生了什么问题?
  • 去你的邮递员那里,以 CURL 的形式复制请求,然后粘贴到这里。邮递员很可能会在您缺少的请求中添加额外信息。
  • 您确定请求是正确的,因为错误代码 400 指向错误请求。
  • 我附上了请求正文,您可以单击它打开图像并帮助我@M。 Deinum
  • 该图像不完整且无法读取。只需将请求正文作为文本添加到您的问题中。您在 postman 中发布的一件事(正如我所见)与您在 Java 中构建的完全不同。

标签: java spring-boot rest resttemplate


【解决方案1】:

在这一行放一个断点:

HttpEntity<String> request = new HttpEntity<>(requestJson.toString(), getHttpHeaders());
        

检查值requestJson.toString()。它与您通过 Postman 成功发送的相同吗?比较它们。我认为任何人都很难猜测为什么服务器会以 400 响应。您还可以将 Postman 设置的标头与您在 Java 代码中设置的标头进行比较。

【讨论】:

  • 这不是答案,而是评论。
  • 我知道,但是在评论中很难粘贴这样的长行。
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 1970-01-01
  • 2021-03-05
  • 2020-11-28
相关资源
最近更新 更多