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?
{
"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.
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.
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
mapobject but never sent it, change your code and populate theheadersobject instead of themapnad send it with the request:Alternatively you can also use the
HttpHeadersto set the headers: