How to dynamically choose output parameter type in Spring MVC REST controller methods

I have a Spring MVC application which uses two REST methods to get a Person from the server, or to allow someone to remotely register. Both methods accept JSON and return it as well. The conversion to from JSON is handled in the DispatcherServlet of the application.

The two methods are:

// Gets a Person via REST.
@RequestMapping(value="/person/{personId}", method=RequestMethod.GET)
public @ResponseBody Person getPerson(@PathVariable("personId") String personId) { + ".getPerson() method called."); 

    return personService.get(Integer.parseInt(personId));        

// Allows a new Person to register via REST.
@RequestMapping(value="/register", method=RequestMethod.POST)    
public @ResponseBody Person postRegister(@RequestBody Person person, 
                                         BindingResult bindingResult) { + ".postRegister() method called."); 

    // Validate Person.
    personValidator.validate(person, bindingResult);
    if (bindingResult.hasErrors()) {

        /* NOTE: Messages object to be returned here. */

            List<ObjectError> errors = bindingResult.getAllErrors();            
            for (ObjectError error : errors) { 
                System.out.println(messageSource.getMessage(error.getCode(), null, null));            
            return person;

    else {                       
        return person;        

These work perfectly well if there are no validation errors. But in the postRegister method, if there's a validation error(s), I want to return to the client a simple Messages object as follows:

public class Messages {

private List<String> messages;

public Messages() { 
    messages = new ArrayList<String>();

Which can then be converted to JSON.

So my problem becomes an issue of handling the return types from the methods. For a successful operation, I want each method to return a Person, but in the case of a failure, each method should return a Messages object.

Other than having both methods return a generic Object, is there a simple way that this can be achieved?


You could throw an exception in case the BindingResult has errors and then have a dedicated @ExceptionHandler for that method to return the messages instead:

public Messages handleIOException(ValidationException ex) {

    // Build up messages from BindingResult contained in the Exception
    return messages;

Need Your Help

Are rename() and unlink() asynchronous functions?

php ajax asynchronous filesystems

I have strong reason to believe that both functions rename() and unlink() are asynchronous, which, from my understanding, means that when the functions are called, the code below them are continued

Xcode 4.5 - iOS development

iphone xcode ios6 iphone-5

In the release notes of Xcode 4.5 it is written

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.