RESTFul validation using Spring Part 3

Custom Error Class:

package com.cloudsdp.core.exception;
public class Errors {
    private String objectName;
    private String fieldName;
    private String errorMessage;
    /**
      * @return the objectName
      */
    public String getObjectName() {
        return objectName;
    }
    /**
      * @param objectName the objectName to set
      */
    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }
    /**
      * @return the fieldName
      */
    public String getFieldName() {
        return fieldName;
    }
    /**
      * @param fieldName the fieldName to set
      */
    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }
    /**
      * @return the errorMessage
      */
    public String getErrorMessage() {
        return errorMessage;
    }
    /**
      * @param errorMessage the errorMessage to set
      */
    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
 }


package com.core.exception;
import java.util.List;
public class CloudSdpMessageException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    private List<Errors> formattedErrors;
    /**
     * Constructor.
     */
    public CloudSdpMessageException() {
        super();
    }
    /**
     * Constructor.
     * @param msg message string
     */
    public CloudSdpMessageException(String msg) {
        super(msg);
    }
    /**
     * Constructor.
     * @param e {@link Throwable}
     */
    public CloudSdpMessageException(Throwable e) {
        super(e);
    }
    /**
     * Constructor.
     * @param msg message string
     * @param e {@link Throwable}
     */
    public CloudSdpMessageException(String msg, Throwable e) {
        super(msg, e);
    }
    /**
     * Set list of Errors objects.
     *
     * @param fe the formatted errors
     */
    public void setFormattedErrors(
      List<Errors> fe) {
        this.formattedErrors = fe;
    }
    /**
     * Get list of Errors objects.
     *
     * @return  Collection of IIPObjectErrors
     */
    public List<Errors> getFormattedErrors() {
        return formattedErrors;
    }
}

Conversion of application exception to JSON (There are other approaches to convert then to JSON/XML format routing to custom exception interceptor method)

package com.core;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
import com.cloudsdp.core.exception.CloudSdpMessageException;


@Controller
public abstract class GenericController {
       private MappingJacksonJsonView  jsonView = new MappingJacksonJsonView();
       
@ExceptionHandler(CloudSdpMessageException.class)
       public ModelAndView handleMessageException(CloudSdpMessageException ex) {
              return new ModelAndView(jsonView, "errors", ex.getFormattedErrors());
       }
}

Invoke @ExceptionHandler: Application layer controller need to extend GenericController as below.

@RequestMapping("/user/**")
public class UserController extends GenericController {
       // Write service method here. Define annotation for validation.

       /**
         * Service to register user within system.
         * @param user User
         * @return User
         */
       @RequestMapping(value = "/user/register", method = RequestMethod.POST)
       @Transactional(propagation=Propagation.REQUIRED)
       @ResponseBody()
       @Validators(validators="myapplication.registeration.user")
       public User registerUser(@RequestBody User user) { 
              // Logic
       }
  }

Hope this helps. Please comment your input/suggestions.

4 Responses to RESTFul validation using Spring Part 3

  1. Rajeshkumar Reddy says:

    You have put up a som much effort, along with that you could have put up the dependency. you have used so many libraries it is pain to find all those dependencies.

  2. Rajeshkumar Reddy says:

    thanks dude

  3. Rajeshkumar Reddy says:

    i have make work. soon will upload the code to github.

  4. I did this post long back in 2012, hence missed your comment. Please upload/share extended work you have done or let me know if you need any help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: