Support
Quality
Security
License
Reuse
kandi has reviewed spring-boot-demo and discovered the below as its top functions. This is intended to give you an instant insight into spring-boot-demo implemented functionality, and help decide if they suit your requirements.
该项目已成功集成 actuator(监控)、admin(可视化监控)、logback(日志)、aopLog(通过AOP记录web请求日志)、统一异常处理(json级别和页面级别)、freemarker(模板引擎)、thymeleaf(模板引擎)、Beetl(模板引擎)、Enjoy(模板引擎)、JdbcTemplate(通用JDBC操作数据库)、JPA(强大的ORM框架)、mybatis(强大的ORM框架)、通用Mapper(快速操作Mybatis)、PageHelper(通用的Mybatis分页插件)、mybatis-plus(快速操作Mybatis)、BeetlSQL(强大的ORM框架)、upload(本地文件上传和七牛云文件上传)、redis(缓存)、ehcache(缓存)、email(发送各种类型邮件)、task(基础定时任务)、quartz(动态管理定时任务)、xxl-job(分布式定时任务)、swagger(API接口管理测试)、security(基于RBAC的动态权限认证)、SpringSession(Session共享)、Zookeeper(结合AOP实现分布式锁)、RabbitMQ(消息队列)、Kafka(消息队列)、websocket(服务端推送监控服务器运行信息)、socket.io(聊天室)、ureport2(中国式报表)、打包成war文件、集成 ElasticSearch(基本操作和高级查询)、Async(异步任务)、集成Dubbo(采用官方的starter)、MongoDB(文档数据库)、neo4j(图数据库)、docker(容器化)、JPA多数据源、Mybatis多数据源、代码生成器、GrayLog(日志收集)、JustAuth(第三方登录)、LDAP(增删改查)、动态添加/切换数据源、单机限流(AOP + Guava RateLimiter)、分布式限流(AOP + Redis + Lua)、ElasticSearch 7.x(使用官方 Rest High Level Client)、HTTPS、Flyway(数据库初始化)、UReport2(中国式复杂报表)。
Using jasypt-spring-boot when deplying to Apache Tomcat
export JASYPT_ENCRYPTOR_PASSWORD=your-password
QUESTION
spring boot component bean validation @Positive not working but @NotNull works fine
Asked 2022-Jan-04 at 09:04I have a service managed by spring, I put @Validated annotation on that service refer to this article, it tells that spring will do the validation for us, we don't even need to valid it manually.
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
@Component
@Validated
public class MyService {
public void execute(@Valid MyRequest request) {
//todo
}
}
MyRequest is a DTO which will be validated by spring
import lombok.Data;
import javax.validation.constraints.Positive;
@Data
public class MyRequest {
@Positive(message = "id should be positive")
private Long id;
}
here is my controller, in some reason, I don't want to do the validation in controller.
@Controller
public class MainController {
@Autowired
private MyService service;
@RequestMapping(value = "/execute", method = {GET, POST})
@ResponseBody
public MyResponse execute() {
MyResponse res = new MyResponse();
service.execute(new MyRequest());
res.setHtmlText("hello world");
return res;
}
}
when I visit the url: http://localhost:8001/execute it doesn't give me any exception and show the result hello world
here is the maven dependency
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fudy</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
however after I replace @Positive to @NotNull, it works fine
import lombok.Data;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
@Data
public class MyRequest {
//@Positive(message = "id should be positive")
@NotNull(message = "id should not be null")
private Long id;
}
ANSWER
Answered 2022-Jan-04 at 09:04It works as it should. The validator associated with @Positive
, the PositiveValidatorForLong
accepts null
values as valid. So it will only validate actual values, not null
.
Basically you need them both to fullfil your requirements, so both @Positive
and @NotNull
to only allow positive values.
pro-tip: As you already have the spring-boot-starter-validation
dependency you don't need the additional validation-api
and hibernate-validator
dependencies, those are added already.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit