尝试使用 POST RestTemplate 调用外部 API 时出现错误请求 400



我必须在Java Spring中调用一个外部API,其中我必须传递一个JSON对象,如下所示(XXX和YYY是参数(。如何使用以下类传递此 JSON 对象?

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

我尝试了什么


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");

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;
}
}

我也尝试了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());

下面是异常堆栈跟踪的一部分。

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]

您没有发送正确的 http 标头,您将所有正确的标头放在map对象中但从未发送过,更改代码并填充headers对象而不是mapnad 发送请求:

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");

或者,您也可以使用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);

我猜你没有将你的对象转换为 JSON。

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

希望这会有所帮助 使用全栈家伙提到的 HttpHeaders 和 HttPEntity

相关内容

  • 没有找到相关文章

最新更新