2

I am creating a small validation application using Spring MVC. I am very new to Spring MVC and would like to ensure that what I want is possible.

I have simplified my problem.

I have setup a controller that will be called when a URL is executed. localhost/validate/{SOME TEXT}

The {SOME TEXT} value with be sent to all my validation classes I created. I currently have 4 classes which does the validation and returns another Object data about what happened during the validation

The 4 validation classes are:

CreditCardValidator
AddressValidator
ZipcodeValidator
AccountNumberValidator

I have a main controller bean that when called I want the string to be passed to each class and the object returned from each to be stored and then finally all results are sent back in a response.

Normally, I would do this without Spring by creating an interface that each validation class implements. Then iteration through the list of classes and execute a method.

The problem doing it that way is that whenever I need to add a new validation class I'll need to register it so the request can use it. This involved modifying existing classes.

Since I am using Spring quick heavily in this application I am wondering if this is possible to do via Spring and annotated classes.

I was thinking of creating a custom annotation that each validation class has and then using spring component-scan to get the classes. This would allow me to create new validations without modifying existing code.

Below is the what I am trying to do.

@Controller
public class StringValidationController {

    @RequestMapping(value = "/validate/{text:.+}", method = RequestMethod.GET)
    public ModelAndView index(@PathVariable("text") String text) {

        ModelAndView model = new ModelAndView();
        model.setViewName("index");
        model.addObject("result", getListOfValidatedData());

        return model;

    }

    public List getListOfValidatedData(){
        //Scan for IValidator annotation
        //call each concrete class and pass in text
        // get object with has validation information in it
    }

}
Decrypter
  • 2,784
  • 12
  • 38
  • 57
  • Maybe this will help: http://www.javacodegeeks.com/2013/07/spring-mvc-custom-validation-annotations.html – yname Dec 06 '14 at 06:40

0 Answers0