SpringBoot-当代码打包在WAR中时,@Valid(javax.validation.Valid)不起作用

时间:2018-10-01 12:26:30

标签: java spring-boot

我正在使用@Valid批注来验证传入的请求正文,当我将代码打包在jar中时,它工作正常。但是,当我将包装从罐子改为战争时,@Valid注释不适用于请求正文。

下面是我的 pom.xml 文件:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.db</groupId>
    <artifactId>bff-global-payment-simulator</artifactId>
    <packaging>war</packaging>
    <name>bff-global-payment-simulator</name>
    <version>1.0.0</version>
    <properties>
        <java.version>1.7</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <springfox-version>2.5.0</springfox-version>
        <spring-boot.version>1.4.1.RELEASE</spring-boot.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>

            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <id>central</id>
            <url>https://artifactory.intranet.db.com/artifactory/mvn-public-local/</url>
            <name>mvn-public-local</name>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>https://artifactory.intranet.db.com/artifactory/mvn-snapshot-local/</url>
            <name>mvn-snapshot-local</name>
        </snapshotRepository>
    </distributionManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--SpringFox dependencies -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox-version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox-version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-joda</artifactId>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>

        </dependency>
        <!-- Bean Validation API support -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>com.apple</groupId>
            <artifactId>AppleJavaExtensions</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>
</project>

以下是我的 Java 文件:

public ResponseEntity<POSTResponseForGlobalPaymentRequestForDomesticPayments> createSingleImmediatePayment(
            @Valid @RequestBody GlobalPaymentPOSTRequestForDomesticPayments body,
            @Valid @RequestHeader MultiValueMap<String, String> headers)
            throws UserDefinedFailure, URISyntaxException, ParseException, IOException {
            ....
            }

无法弄清楚为什么会发生这种情况。

2 个答案:

答案 0 :(得分:0)

我从spring-boot-starter-web附带的pom.xml中删除了javax.validation依赖项。不需要显式定义。因此提供了我在javax.validation依赖项中明确提到的问题。

答案 1 :(得分:0)

有一个 spring 依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>        

它对我有用。