Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导验证不适用于所有bean_Java_Spring Boot - Fatal编程技术网

Java Spring引导验证不适用于所有bean

Java Spring引导验证不适用于所有bean,java,spring-boot,Java,Spring Boot,我有一个springboot:1.2.3应用程序,我有两个验证问题: 它适用于@Path端点,但不适用于@Service或@Component类 如果验证失败,它不会抛出或记录任何内容,它只返回一个400,没有任何附加信息 这些是spring boot:1.2.3附带的验证库: [INFO] | +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile [INFO] | | +- javax.validation:vali

我有一个
springboot:1.2.3
应用程序,我有两个验证问题:

  • 它适用于@Path端点,但不适用于@Service或@Component类
  • 如果验证失败,它不会抛出或记录任何内容,它只返回一个400,没有任何附加信息
这些是spring boot:1.2.3附带的验证库:

[INFO] |  +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  +- org.glassfish.jersey.ext:jersey-bean-validation:jar:2.14:compile
这些是spring-boot-starter-log4j附带的日志库:

[INFO] +- org.springframework.boot:spring-boot-starter-log4j:jar:1.2.2.RELEASE:compile
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.10:compile
[INFO] |  \- log4j:log4j:jar:1.2.17:compile
此验证有效,但不记录或显示任何堆栈跟踪:

@Component
@Path("/validation-example")
public class ValidationExampleResource {

    @GET
    @Path("/{positiveNumber}")
    public int validationExample(@Min(0) @PathParam("positiveNumber") int positiveNumber) {
        ...
    }
}
但验证在这里根本不起作用:

@Service
public class ValidationExampleServiceImpl implements ValidationExampleService {

    @Override
    public int validationExample(@Min(0) int positiveNumber) {
        ...
    }
}

为了使验证生效,您需要添加@Valid

@Component
@Path("/validation-example")
public class ValidationExampleResource {

    @GET
    @Path("/{positiveNumber}")
    public int validationExample(@Valid @Min(0) @PathParam("positiveNumber") int positiveNumber) {
        ...
    }
}

试试这个配置。在我的项目中成功了!弹簧靴:
1.5.9.释放

@Slf4j
@Service
@Validated
public class ValidationService {

    @Validated(CreateGroup.class)
    public void create(@Valid User user) {
        log.info("验证服务业务的作用");
    }

    public void basic(@NotNull(message = "名称不能为空") String name) {

    }

}

@cahen这里有一个例子,不幸的是,没有。如果你能找到答案,请告诉我们。我猜如果我使用Spring MVC控制器而不是JAX-RS资源类,这会起作用,但我希望这两种方法都能起作用,当然。奇怪的是,我无法让它与Path Endpoints一起工作。您使用的是哪个jersey版本?
2.14
spring boot dependencies:1.2.3.RELEASE
设置。您错过了@Valid。请在min之前尝试@Valid。