1

I'm trying to send custom error message with Http status codes in RestClient call

microservice 1 code:

try{
   String method = request.getMethod();
   RestTemplate restTemplate = new RestTemplate();
   ResponseEntity<byte[]> responseEntity = restTemplate.exchange("/someUrl", method, entity, byte[].class);
} catch (HttpServerErrorException | HttpClientErrorException e) {
   String errorString = e.getResponseBodyAsString();
   if(Strings.isNullOrEmpty(errorString)) {
      errorString = e.getMessage();
   }
   return new ResponseEntity<>(errorString, new HttpHeaders(), e.getStatusCode());
}

microservice 2 code:

@RequestMapping(value="/someUrl", method= { RequestMethod.GET, RequestMethod.POST})
public ResponseEntity<?> executeSoapServer(@RequestHeader HttpHeaders headers, HttpServletRequest request, HttpServletResponse resp){
  JsonObject obj = new JsonObject();
  obj.addProperty("errorMessage", "test error message")
  return new ResponseEntity<>(obj.toString(), new HttpHeaders(), HttpStatus.UNAUTHORIZED);
}

From microservice 1 GET call response: response status code - 401

{
  "errorMessage": "test error message"
}

From microservice 1 POST call response: response status code - 401

401 null

For the GET method its working as expected, but for the POST/PUT/DELETE not working for microservice to microservice.

When I directly hit microservice 2 from postman with POST method its working, but when I hit microservice2 from microservice1 with POST method not working.

When I hit microservice 2 controller from microservice 1 below are the response headers:

{
Access-Control-Allow-Credentials=[true], 
Access-Control-Allow-Methods=[GET,POST,PUT,PATCH,DELETE,HEAD], 
X-Application-Context=[eportal:local:8082], 
ep-flowid=[c35c3098-a7a5-712a-8e38-4d1ad99fccd0], 
Access-Control-Expose-Headers=[ep-flowid], 
X-Content-Type-Options=[nosniff], 
X-XSS-Protection=[1; mode=block], 
Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], 
Pragma=[no-cache], 
Expires=[0], 
X-Frame-Options=[DENY], 
Content-Type=[application/json;charset=UTF-8], 
Content-Length=[29], 
Date=[Mon, 01 Jun 2020 13:59:59 GMT]
}

When I hit microservice 2 controller from postman below are the response headers:

enter image description here

Giri
  • 31
  • 7

0 Answers0