I currently have a very strange problem with calling one REST service from inside another, and I can really use my hand in developing what I'm doing wrong.
So, firstly, a little context:
I have a webapp that calls the REST service to create a user account (for this explanation, the endpoint is localhost: 8080 / register). Earlier in a user journey, I called another service to create user credentials localhost:8090/signup, but I need to check a few things in the call / register, so inside the call that I call to another endpoint on 8090 to get this information ( localhost:8090/availability). In short, webapp calls localhost: 8080 / register, which in turn calls localhost:8090/availability.
When I call the accessibility endpoint directly, either from the REST client or from the webapp itself, everything works as expected, but for some strange reason, when I call it from the call to the register endpoint, I get HTTP415. Does anyone know what is going wrong?
The register controller is as follows:
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public UserModel createUser(@RequestBody UserModel userModel) throws InvalidSignupException {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Boolean> response = restTemplate.postForEntity("http://localhost:8090/availability",
userModel.getUsername(), Boolean.class);
System.out.println(response.getBody());
return userModel;
}
And the availability controller looks like this:
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public Boolean isUsernameAvailable(@RequestBody String username) {
return Boolean.TRUE;
}
Full disclosure - in practice, what I showed as the contents of createUser () actually calls several calls in the call stack, using the same class as me to call services from webapp (which works fine in this context), and I not really returning true to isUsernameAvailable (because that would be stupid), but this is the simplest version of the code that replicates the problem.
My current assumption is that I am doing what I'm going to pounce on myself when I see it, but I have looked at this code for too long to see it more.
Edit Vikdor Comment to resolve this issue for me. I changed the createUser method to:
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public UserModel createUser(@RequestBody UserModel userModel) throws InvalidSignupException {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
ResponseEntity<Boolean> response = restTemplate.postForEntity("http://localhost:8090/availability",
userModel.getUsername(), Boolean.class);
System.out.println(response.getBody());
return userModel;
}