简体   繁体   English

BadRequest 400,同时尝试使用POST RestTemplate调用外部API

[英]BadRequest 400 while trying to call a external API using POST RestTemplate

I have to call an external API in Java Spring in which I have to pass a JSON object as follows( XXX and YYY are parameters ).How can I pass this JSON object using the following classes? 我必须在Java Spring中调用一个外部API,在该API中必须按如下方式传递JSON对象(XXX和YYY是参数)。如何使用以下类传递此JSON对象?

{
  "codecConfigId": "XXXXXXXX", 
  "inputStreams": [{
    "inputId": "Input_Teste_1024_1",
    "inputPath": "YYYYYYYYYYYYY",
    "selectionMode": "AUTO"
  }]
}

What I tried 我尝试了什么


StreamsForCodecs streams = new StreamsForCodecs();
streams.setCodecConfigId(codecConfigId);
streams.setInputStream(path.toString());

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("Content-Type", "application/json");
map.put("x-api-key", "XXXXXXX");
map.put("Accept", "application/json");

The StreamsForCodecs is a class that has another class linked to that because I need to pass the above JSON, so I created those 2 classes. StreamsForCodecs是一个与另一个类链接在一起的类,因为我需要传递上述JSON,因此我创建了这两个类。

public class StreamsForCodecs implements Serializable {

    private static final long serialVersionUID = 1L;
    private String codecConfigId;
    ArrayList<InputStreamsCodec> inputStreams = new ArrayList<InputStreamsCodec>();

...
    // Setter Methods

    public void setCodecConfigId(String codecConfigId) {
        this.codecConfigId = codecConfigId;
    }

    public void setInputStream(String path) {
        InputStreamsCodec inputStreamsCodec = new InputStreamsCodec();
        inputStreamsCodec.setInputPath(path);
        inputStreams.add(inputStreamsCodec);
    }

}

class InputStreamsCodec implements Serializable {

    private static final long serialVersionUID = 1L;
    private String inputPath;
...
Some GetAndSetters

    public void setInputPath(String inputPath) {
        this.inputPath = inputPath;
    }
}

I also tried requestEntity , request , RestTemplate.postForEntity and RestTemplate.exchange ,but in all the scenarios I got the bad request.When I try to call this from POSTman, I didn't get the error. 我也尝试了requestEntityrequestRestTemplate.postForEntityRestTemplate.exchange ,但是在所有情况下我都遇到了错误的请求。当我尝试从POSTman调用它时,我没有收到错误。

HttpEntity<StreamsForCodecs> requestEntity = new HttpEntity(streams, headers);
request = new HttpEntity(requestEntity, headers);
ResponseEntity<StreamsForCodecsResponse> response= new RestTemplate().exchange(url, HttpMethod.POST, requestEntity, StreamsForCodecsResponse.class);
//ResponseEntity<StreamsForCodecsResponse> response= new //RestTemplate().postForEntity(url, request, StreamsForCodecsResponse.class);
System.out.println(response.getBody());

Here is s part of the stack trace of the exception. 这是异常的堆栈跟踪的一部分。

org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:79) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:778) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:579) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at com.carlosdv93.controller.CreateStreamsForCodecs.createStreamsForCodecsPOST(CreateStreamsForCodecs.java:42) ~[classes/:na]

You are not sending the correct http headers, you put all the correct headers in the map object but never sent it, change your code and populate the headers object instead of the map nad send it with the request: 您没有发送正确的http标头,而是将所有正确的标头放置在map对象中,但从未发送过,更改代码并填充headers对象,而不是map nad发送请求:

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Content-Type", "application/json");
headers.add("x-api-key", "XXXXXXX");
headers.add("Accept", "application/json");

Alternatively you can also use the HttpHeaders to set the headers: 另外,您也可以使用HttpHeaders设置标头:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType. APPLICATION_JSON);
headers.set("x-api-key", "XXXXXXX");
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<StreamsForCodecs> requestEntity = new HttpEntity<>(streams, headers);
ResponseEntity<StreamsForCodecsResponse> response = new RestTemplate().exchange(url, HttpMethod.POST, requestEntity, StreamsForCodecsResponse.class);

I guess you are not converting your Object to JSON. 我想您不是将Object转换为JSON。

    JSONObject requestJson = new JSONObject(streams).toString();
    HttpEntity<StreamsForCodecs> requestEntity = new HttpEntity<>(requestJson , headers);

Hope this will help Use HttpHeaders and HttPEntity as mentioned by fullstack guy 希望这将有助于使用fullHeader提到的HttpHeaders和HttPEntity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM