我将验证连接到我的项目,但它不起作用。我检查了几次是否忘记了什么-一切似乎都很好。我创建了测试文件:控制器,服务,存储库,实体,Map器,dto,并在这个项目上测试了它(它仍然没有工作),在另一个项目上,验证工作,一切都已经开始了。
实体:
package com.example.entity;
import lombok.Data;
import javax.persistence.*;
@Entity
@Data
@Table(name = "test")
public class TestEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic
@Column(name = "id")
private Long id;
@Basic
@Column(name = "test")
private String test;
}
测试响应:
package com.example.dto.response;
import lombok.Data;
import javax.validation.constraints.NotNull;
@Data
public class TestResponse {
private Long id;
@NotNull
private String test;
}
测试请求:
package com.example.dto.request;
import lombok.Data;
import javax.validation.constraints.NotNull;
@Data
public class TestRequest {
@NotNull
private String test;
}
回购、控制器和服务:
package com.example.repository;
import com.example.entity.TestEntity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TestRepo extends CrudRepository<TestEntity, Long> {
}
package com.example.controller;
import com.example.dto.request.TestRequest;
import com.example.dto.response.TestResponse;
import com.example.mapper.TestMapperImpl;
import com.example.service.TestService;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@RestController
@AllArgsConstructor
@RequestMapping("/test")
public class TestController {
private TestService testService;
private TestMapperImpl testMapper;
@PostMapping("/add")
public ResponseEntity<TestResponse>add(@Valid @RequestBody TestRequest testRequest){
return new ResponseEntity<>(testMapper.toDto(testService.add(testMapper.fromDto(testRequest))), HttpStatus.OK);
}
}
package com.example.service;
import com.example.entity.TestEntity;
import com.example.repository.TestRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@AllArgsConstructor
public class TestService {
private TestRepo testRepo;
public TestEntity add(TestEntity entity){
return testRepo.save(entity);
}
}
Postman 屏幕。它应该抛出一个错误,但它没有
enter image description here
我的pom.xml
<?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.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>kursova</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
<org.lombok.version>1.18.26</org.lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>${org.lombok.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!--Mapper-->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.lombok.version}</version>
</path>
<!-- This is needed when using Lombok 1.18.16 and above -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
<!-- Mapstruct should follow the lombok path(s) -->
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<!-- Set the componentModel to 'spring' by default -->
</annotationProcessorPaths>
<compilerArgs>
<compilerArg>
-Amapstruct.defaultComponentModel=spring
</compilerArg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
1条答案
按热度按时间pftdvrlh1#
@Basic
注解有一个名为optional
的属性,它是一个布尔参数,用于定义标记的字段或属性是否允许null。默认为true。请参阅这篇文章,它解释了什么时候应该使用它。
尝试将
@NotNull
注解直接应用于您的实体。