stack man
stack man

Reputation: 2413

Java - RestTemplate syntax error?

So I've got this controller method, which is obviously returning a ResponseEntity:

@RequestMapping(method=RequestMethod.POST, value="sendEmail")
    public ResponseEntity sendPasswordResetEmail (@RequestParam("name")     final String name,
                                       @RequestParam("password") final String password,
                                       @RequestParam("email")    final String email)
{
            final boolean success = notificationService.sendPasswordResetEmail(name, password, email);
            return success ?
                    new ResponseEntity(HttpStatus.OK) : new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}

My problem is that when calling the following, it is not letting me set ResponseEntity.class as the third parameter, which does not makes sense because that is the expected returning type:

ResponseEntity<String> auth = restTemplate.postForEntity(url, entity, ResponseEntity.class);

Any hint?

UPDATE:

How is it possible that the code compiles and runs and I get this when consuming the endpoint?

{
  "timestamp": 1444927133682,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "java.lang.NoClassDefFoundError",
  "message": "org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: com/api/model/postmark/ResetPassword",
  "path": "/v1/api/notify/sendPasswordResetEmail"
}

Upvotes: 0

Views: 729

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280138

The signature of the postForEntity method you are attempting to use is

public <T> ResponseEntity<T> postForEntity(URI url,
                                       Object request,
                                       Class<T> responseType)
                                throws RestClientException

In other words, it's always going to return a ResponseEntity. What you are providing as an argument is the type of the response body, which will tell the RestTemplate how to deserialize the content.

Simply use

ResponseEntity<String> auth = restTemplate.postForEntity(url, entity, String.class);

Upvotes: 1

Related Questions