【问题标题】:Spring RestTemplate exchange DELETE with body jdk 1.8Spring RestTemplate 与 body jdk 1.8 交换 DELETE
【发布时间】:2016-09-08 20:04:03
【问题描述】:

我正在尝试通过交换方法使用 Spring RestTemplate 使用正文执行 HTTP DELETE 请求,但我总是收到 400 错误请求,例如 this question。使用 JavaScript 和其他工具,可以使用 DELETE 进行此 API 调用。 我知道 java see here。我将 spring-web-4.2.6.RELEASE 与 jdk 1.8 一起使用,所以我认为必须有办法。

我的代码:

public DealResponse closePosition(DealCloseRequest dealCloseRequest) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        //Object to JSON in String
        String jsonInString = mapper.writeValueAsString(dealCloseRequest);

        HttpEntity<String> entity = new HttpEntity<String>(jsonInString, this.headers);
        //execute request
        ResponseEntity<DealResponse> response = restTemplate.exchange("https://" + this.domain + "/gateway/deal/positions/otc", HttpMethod.DELETE, entity, DealResponse.class);
        //return filled DealResponse object
        return response.getBody();
    } catch (JsonProcessingException e) {
        this.logger.warn("could not close Position because: "+e);
        return null;
    }
}

错误信息:

org.springframework.web.client.HttpClientErrorException: 400 Bad Request

有谁知道用 spring restTemplate 做到这一点的方法吗?

【问题讨论】:

    标签: java spring rest resttemplate spring-web


    【解决方案1】:

    带有正文的 HTTP DELETE 请求适用于 Spring 4.2 版本的 rest 模板。您发送到服务的请求正文可能存在一些问题。如果它正在形成正确的 json 有效负载,您能否检查“jsonInString”。检查标题以及“application/json”类型。您可以通过发送 DELETE 请求来使用 Postman 验证您的服务。

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 2017-02-28
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 2019-03-20
      • 1970-01-01
      相关资源
      最近更新 更多