Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC 3.2 - JSR 303 Validation is not working. JSON POST Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC 3.2 - JSR 303 Validation is not working. JSON POST

    I have the following Bean...
    Code:
    @Entity
    @Table(name="employees")
    public class Person {
    
    private Integer person_id;
    private String name;
    private String name2;
    private String email;
    private double phone;
    private String desc;
    
    @Id
    @Max(value=500)
    @Column (name="id")
    public Integer getPerson_id() {
        return person_id;
    }
    
    public void setPerson_id(Integer person_id) {
        this.person_id = person_id;
    }
    
    @NotNull
    @NotEmpty
    @Column (name="fn")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    @Column(name="ln")
    public String getName2() {
        return name2;
    }
    public void setName2(String name2) {
        this.name2 = name2;
    }
    
    @Email
    @Column (name="em", unique = true)
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    
    @Column(name="phone")
    public double getPhone() {
        return phone;
    }
    public void setPhone(double phone) {
        this.phone = phone;
    }
    
    @Column (name="de")
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
     }
    My POST Controller method:


    Code:
    @RequestMapping(value="/persons", method = RequestMethod.POST, 
                                     headers = "Accept=*/*", 
                                    consumes = "application/json")
    
    public String getAllPersons( @javax.validation.Valid @RequestBody Person p){
        System.out.println("The OBject:" + p);
        personService.savePerson(p); // The validation is kicking in this method.
        return "redirect:persons.htm";
    }
    My Config file:

    Code:
    		<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>
    		<context:component-scan base-package="com.springmvcsample"/>
    		<context:annotation-config/>
    		<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        		<!-- Turn off working out content type based on URL file extension, should fall back to looking at the Accept headers -->
        		<property name="favorPathExtension" value="false" />
    		</bean>
    I have Hibernate Validator jar in my classpath along with Spring validation API jar.

    The binding is working fine. But validation is not applying. Note that I am posting JSON. Not form.
    However, when I save the bean (Hibernate), the validation kicks in.

    What am I missing here?

  • #2
    Anyone? Any idea?

    Comment

    Working...
    X