First of all, even if this does not directly answer the question, your objects should never return null instead of empty collections - you can find the arguments in Effective Java 2nd Edition , paragraph 43 / p.201
So, if the situation where no country has been found is normal, it should be handled by the client JS code, which will check the counter and display the corresponding message.
If something went wrong, you can throw an exception (as Biju pointed out +1) - I believe this is a service that should throw an exception, because it knows the reason why this happened, and do not return null anyway .
I would like to add that in Spring 3.2 (in pre Spring 3.2 the return body of the response is complex ) you can set @ExceptionHandler , which will return JSON and set the HTTP status code, which can be later processed by the client. I think that returning a custom JSON response with some error code is most optimal here.
@RequestMapping("/test") @ResponseBody public List<Country> getListOfCountries() { //assuming that your service throws new NoCountriesFoundException(); //when something goes wrong return countryService.listAll(); } @ExceptionHandler(NoCountriesFoundException.class) ResponseEntity<String> test() { return new ResponseEntity<String>( "We are sorry, our server does not know any countries yet.", HttpStatus.I_AM_A_TEAPOT ); }
Then, in the JS code, you can do some processing depending on the status code returned.
In addition, to avoid declaring the same @ExceptionHandler in different controllers, in Spring 3.2 you can put @ExceptionHandler inside the annotated @ControllerAdvice class.
See http://static.springsource.org/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-exceptionhandlers and http://www.springsource.org/node/3738 for 3.2 specific things for more details.