英文:
Import of jakarta.validation missing
问题
你遇到的问题可能是因为在Spring Boot 3.1.0-M1版本中,验证注解的包路径已经从javax.validation
迁移到jakarta.validation
。以下是您可以尝试的解决方法:
-
更新依赖:您的
pom.xml
文件中已经包含了spring-boot-starter-validation
依赖,但是可能需要确保它包含了正确的jakarta.validation
库。请检查一下您的pom.xml
文件中是否包含了以下依赖:<dependency> <groupId>jakarta.validation</groupId> <artifactId>jakarta.validation-api</artifactId> <version>2.0.2</version> <!-- 或者适当的版本 --> </dependency>
这将确保
jakarta.validation
库在您的项目中可用。 -
清理和构建:如果您已经更新了依赖,请尝试在Eclipse中执行项目的清理和重新构建操作,以确保所有依赖都正确加载。
-
IDE配置:有时Eclipse可能需要手动更新项目的构建路径或刷新依赖。您可以尝试右键单击项目,然后选择"Refresh"或"Refresh Gradle Project"(如果您使用Gradle)来刷新项目。
-
检查Java版本:确保您的项目使用的是Java 17版本,因为不同的Java版本可能会影响库的可用性。
-
重启Eclipse:有时候Eclipse可能会出现缓存问题,重新启动Eclipse可能有助于解决这些问题。
尝试上述步骤后,您应该能够成功导入和使用@Valid
注解以进行输入验证。如果问题仍然存在,请确保您的Eclipse IDE和插件是最新版本,并尽可能提供更多的错误信息以便进一步诊断问题。
英文:
I'm following and online course on Springboot MVC. I'm working with Springboot 3.1.0-M1 version, java 17 and Eclipse 2022-09.
Now the course tells me to use the annotation @Valid in the file UserResource.java for the validation of the input.
This annotation should import the library
import jakarta.validation.Valid
but Eclipse does not give me this possibility: the library of jakarta.validation is not in list that Eclipse propones me. Morover, if i try to add manually that libray (jakarta.validation.*
), Eclipse underlines that code line in red and tells me that The import jakarta.validation cannot be resolved
.
the @Valid annotation return me the following message: Valid cannot be resolved to a type
.
I tried to import the library javax.validation.Valid
without results.
If i write jakarta.
in Eclipse for import a libray, the system return me a lot of libraries (ie: jakarta.ammotation, jakarta.servlet) but not the validation library.
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>3.1.0-M1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.in28minutes.rest.webservices</groupId>
<artifactId>restful-web-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restful-web-services</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</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>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
UserResouce.java
package com.in28minutes.rest.webservices.restfulwebservices.user;
import java.net.URI;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import javax.validation.*;
@RestController
public class UserResource {
private UserDaoService service;
public UserResource(UserDaoService service) {
this.service = service;
}
@GetMapping("/users")
public List<User> retriveAllUsers() {
return service.findAll();
}
@GetMapping("/users/{id}")
public User retriveUser(@PathVariable int id) {
User user = service.findOne(id);
if (user==null)
throw new UserNotFoundException("id:"+id);
return user;
}
@DeleteMapping("/users/{id}")
public void deleteUser(@PathVariable int id) {
service.deleteById(id);
}
@PostMapping("/users")
public ResponseEntity<User> createUser(@Valid @RequestBody User user) {
User savedUser = service.save(user);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(savedUser.getId()).toUri();
return ResponseEntity.created(location).build();
}
}
What am I wronging?
Thanks
答案1
得分: 1
你有这个依赖项 spring-boot-starter-validation
,所以应该可以工作,请尝试重新启动 Eclipse 并确保 Maven 已经拉取了这个依赖项。
英文:
you have this dependency spring-boot-starter-validation
so it should work, try good old restart of eclipse and make sure maven did pull the dependency
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论