我必须在 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;
}
}
我也尝试过requestEntity
,和request
,但在所有情况下我都收到了错误的请求。当我尝试从 POSTman 调用它时,我没有收到错误消息。RestTemplate.postForEntity
RestTemplate.exchange
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]