1

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]
Pavindu
  • 2,684
  • 6
  • 44
  • 77

2 Answers2

0

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:

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 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);
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
0

I guess you are not converting your Object to 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

Hitesh Kumar
  • 45
  • 1
  • 14