localhostproduction-environmentinternal-server-errorspring-resttemplate

Internal server error in server works in localhost


Below code is throwing 500 Internal server error only in server but works in local host. Any input is appreciated.

Controller endpoint:

@ApiOperation(value = "This API gets info from external service")
    @PostMapping(value = "/ref-data/info", headers= "Accept=application/json")
    public ResponseEntity<String> getMasterMaterialInfo(@RequestBody RequestDTO requestDTO) {
        return service.getInfo(requestDTO);
    }

Service method:

public ResponseEntity<String> getInfo(RequestDTO requestDTO) {
        HttpEntity<RequestDTO> request = new HttpEntity<>(requestDTO);
        String requestUrl =  refDataUrl+"?apikey="+apikey;
        return restTemplate.exchange(requestUrl, HttpMethod.POST, request, String.class);
    }

Server Log:

enter image description here Thanks


Solution

  • This issue is because of the broken response entity pipeline. Solution is to read the content of the response entity and assign it to the new instance of the response entity that instead of returning the response as it is..

    Change in service layer:

    public String getInfo(RequestDTO requestDTO) {
            HttpEntity<RequestDTO> request = new HttpEntity<>(requestDTO);
            String requestUrl =  refDataUrl+"?apikey="+apikey;
            ResponseEntity<String> responseEntity =  restTemplate.exchange(requestUrl, HttpMethod.POST, request, String.class);
            return responseEntity.getBody()
        }