2

How can I change/update the following REST call from Spring MVC to return a error if the user did not enter of the the two names I was coding for.. something like a not found?

@RequestMapping(value = "/{name}", method = RequestMethod.GET)
@ResponseBody
public User getName(@PathVariable String name, ModelMap model)
{
    logger.debug("I am in the controller and got user name: " + name);

    /*
        Simulate a successful lookup for two users. This
        is where your real lookup code would go.
     */

    if ("name2".equals(name))
    {
        return new User("real name 2", name);
    }

    if ("name1".equals(name))
    {
        return new User("real name 1", name);
    }

    return null;
}
1

2 Answers 2

10

Define a new exception class, e.g. ResourceNotFoundException and throw an instance of this from your annotated controller method getName.

Then also define an annotated exception handler method in your Controller class to handle that exception, and return a 404 Not Found status code, potentially logging it.

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public void handleResourceNotFoundException(ResourceNotFoundException ex)
{
    LOG.warn("user requested a resource which didn't exist", ex);
}

Or even returning some error message, using @ResponseBody annotation:

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND)
@ResponseBody
public String handleResourceNotFoundException(ResourceNotFoundException ex)
{
    return ex.getMessage();
}
6
  • but how do I sent it back to the client?? do you have a example of this anywhere Jun 7, 2013 at 14:22
  • spring takes care of that for you. read the docs at: static.springsource.org/spring/docs/3.1.x/…
    – kevin847
    Jun 7, 2013 at 14:23
  • I dont see it on the page.. am I missing something.. how to I tell the client that something is NOT right with the call? Jun 7, 2013 at 14:48
  • if I do this then the client will get back a java.lang.NullPointerException.. I want to learn how to pass the error back to the client Jun 7, 2013 at 15:27
  • The client of a REST service will be some piece of software - and as such will be totally happy with a HTTP status 404 and needs nothing more. Jun 7, 2013 at 15:37
2

You could create an object specifically for returning error responses. That way, you can say whatever you want. For example:

@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<ResponseStatus> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex){

    ResponseStatus responseStatus = new ResponseStatus("400", "Bad Request. " + ex);
    responseStatus.setResponseStatusTime(timestampService.createTimestamp());
    HttpStatus status = HttpStatus.BAD_REQUEST;

    ResponseEntity<ResponseStatus> response = new ResponseEntity<ResponseStatus>(responseStatus, status);

    return response;
}

In this example you can see that there is a ResponseStatus object. This object contains a field for a status code, status message, date and time. You may not need date and time but I find it useful for when someone sends me an error they have seen, because then it is easy to track down exactly where it happened in our server logs.

1
  • ResponseStatus is your own class, right? Because Spring has a ResponseStatus annotation.
    – Marvo
    Sep 11, 2015 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.