@在SpringJava中无效

@在SpringJava中无效,java,spring,maven,spring-mvc,model-view-controller,Java,Spring,Maven,Spring Mvc,Model View Controller,我在Spring mvc中使用了@Valid。看来它什么也没做。 这是我的节目 springservlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mv

我在Spring mvc中使用了@Valid。看来它什么也没做。 这是我的节目

springservlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        <mvc:annotation-driven/>
        <context:component-scan base-package="com.mvc"></context:component-scan>
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix">
                        <value>/</value>
                </property>
                <property name="suffix">
                        <value>.jsp</value>
                </property>
        </bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mvc</groupId>
    <artifactId>SpringMvc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.0.Alpha2</version>
        </dependency>
    </dependencies>
</project>
Person.java 这里我用过
@NotNull
@Size(最小值=5,最大值=30,message=“姓氏长度应为5-30”)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        <mvc:annotation-driven/>
        <context:component-scan base-package="com.mvc"></context:component-scan>
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix">
                        <value>/</value>
                </property>
                <property name="suffix">
                        <value>.jsp</value>
                </property>
        </bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mvc</groupId>
    <artifactId>SpringMvc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.0.Alpha2</version>
        </dependency>
    </dependencies>
</project>

4.0.0
com.mvc
SpringMvc
1.0-快照
战争
org.springframework
SpringWebMVC
4.1.4.1发布
org.hibernate.validator
休眠验证器
6.0.0.2

查找6以下的Hibernate版本并重试,使用@ModelAttribute可以将表单的输入映射到bean,而api 2.0的6.0版本可能会导致bean出现一些问题。我建议您查看此主题以清楚地了解这些问题::

查找6以下的Hibernate版本并重试,使用@ModelAttribute可以将表单的输入映射到bean,而api 2.0的6.0版本可能会导致一些关于bean的问题。我建议您查看此主题,以便更清楚地了解这些问题::

我添加了您所做的常规工作(pom中的库、@Valid on RequestBody等)

Spring文档(和许多博客)让它变得微妙的是,Spring寻找一个退出点来抛出错误,但如果该退出点不存在,它将以404作为回复。在阅读了大量内容(尤其是本文)之后,添加此类使Spring能够识别@Valid并找到一个退出点来抛出错误

    @RestControllerAdvice
    @Order(1) 
    public class RestControllerExceptionHandler {

    @RequestMapping(value = { "error/404" }, method = RequestMethod.GET)
    @ExceptionHandler(Exception.class)
    public String handleUnexpectedException(Exception e) {
        return "views/base/rest-error";         
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public String handlemethodArgumentNotValid(MethodArgumentNotValidException exception) { //
        // TODO you can choose to return your custom object here, which will then get transformed to json/xml etc.
        return "Sorry, that was not quite right: " + exception.getMessage();
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ConstraintViolationException.class)
    public String handleConstraintViolation(ConstraintViolationException exception) { //
        // TODO you can choose to return your custom object here, which will then get transformed to json/xml etc.
        return "Sorry, that was not quite right: " + exception.getMessage();
    }

    
}

我添加了您所做的常规工作(pom中的库、@Valid on RequestBody等)

Spring文档(和许多博客)让它变得微妙的是,Spring寻找一个退出点来抛出错误,但如果该退出点不存在,它将以404作为回复。在阅读了大量内容(尤其是本文)之后,添加此类使Spring能够识别@Valid并找到一个退出点来抛出错误

    @RestControllerAdvice
    @Order(1) 
    public class RestControllerExceptionHandler {

    @RequestMapping(value = { "error/404" }, method = RequestMethod.GET)
    @ExceptionHandler(Exception.class)
    public String handleUnexpectedException(Exception e) {
        return "views/base/rest-error";         
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public String handlemethodArgumentNotValid(MethodArgumentNotValidException exception) { //
        // TODO you can choose to return your custom object here, which will then get transformed to json/xml etc.
        return "Sorry, that was not quite right: " + exception.getMessage();
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ConstraintViolationException.class)
    public String handleConstraintViolation(ConstraintViolationException exception) { //
        // TODO you can choose to return your custom object here, which will then get transformed to json/xml etc.
        return "Sorry, that was not quite right: " + exception.getMessage();
    }

    
}

您可以用表单显示jsp页面吗?您还应该将jsp页面放在web inf文件夹下。请改用hibernate validator 5.4.1最终版本。版本6适用于bean验证api 2.0您可以用表单显示jsp页面吗?您还应该将jsp页面放在web inf文件夹下。请改用hibernate validator 5.4.1最终版本。版本6用于bean验证api 2.0