Spring MVC - calling a holiday service from another holiday service

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 {

    // a load of business logic that validates the user model

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<Boolean> response = restTemplate.postForEntity("http://localhost:8090/availability",
            userModel.getUsername(), Boolean.class);
    System.out.println(response.getBody());

    // a load more business logic

    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) {

    // a load of business logic that returns a boolean
    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 {

    // a load of business logic that validates the user model

    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());

    // a load more business logic

    return userModel;
}
+4
2

A HTTP415 . , isUsernameAvailable JSON, , .

Content-Type: application/json HTTP-, :

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
restTemplate.put(uRL, entity);
+1

RestTemplate.exchange, :

ResponseEntity responseEntity = RestTemplate.exchange(endPointURL, HttpMethod.GET/POST/PUT/DELETE, HttpEntity/headers, uriVariables)

endpointURL - URL- SOAP, REST.

HTTPMethod - , GET, PUT, POST, DELETE ..

HTTPEntity. //{a}. , -Valye HTTP.

uriVariables - (Object... urivariables), String.class, Integer.class

connectTimeout, isSSLDisabled, responseCached restTemplate.

0

Source: https://habr.com/ru/post/1622034/


All Articles