导入 jakarta.validation 丢失

huangapple go评论82阅读模式
英文:

Import of jakarta.validation missing

问题

你遇到的问题可能是因为在Spring Boot 3.1.0-M1版本中,验证注解的包路径已经从javax.validation迁移到jakarta.validation。以下是您可以尝试的解决方法:

  1. 更新依赖:您的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库在您的项目中可用。

  2. 清理和构建:如果您已经更新了依赖,请尝试在Eclipse中执行项目的清理和重新构建操作,以确保所有依赖都正确加载。

  3. IDE配置:有时Eclipse可能需要手动更新项目的构建路径或刷新依赖。您可以尝试右键单击项目,然后选择"Refresh"或"Refresh Gradle Project"(如果您使用Gradle)来刷新项目。

  4. 检查Java版本:确保您的项目使用的是Java 17版本,因为不同的Java版本可能会影响库的可用性。

  5. 重启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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
 xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
 &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
 &lt;parent&gt;
 	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 	&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
 	&lt;version&gt;3.1.0-M1&lt;/version&gt;
 	&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
 &lt;/parent&gt;
 &lt;groupId&gt;com.in28minutes.rest.webservices&lt;/groupId&gt;
 &lt;artifactId&gt;restful-web-services&lt;/artifactId&gt;
 &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
 &lt;name&gt;restful-web-services&lt;/name&gt;
 &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
 &lt;properties&gt;
 	&lt;java.version&gt;17&lt;/java.version&gt;
 &lt;/properties&gt;
 &lt;dependencies&gt;
 	&lt;dependency&gt;
 		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 		&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
 	&lt;/dependency&gt;
 	&lt;dependency&gt;
 		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
 	&lt;/dependency&gt;

 	&lt;dependency&gt;
 		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 		&lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
 	&lt;/dependency&gt;
 	
 	&lt;dependency&gt;
 		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 		&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
 		&lt;scope&gt;runtime&lt;/scope&gt;
 		&lt;optional&gt;true&lt;/optional&gt;
 	&lt;/dependency&gt;
 	&lt;dependency&gt;
 		&lt;groupId&gt;com.h2database&lt;/groupId&gt;
 		&lt;artifactId&gt;h2&lt;/artifactId&gt;
 		&lt;scope&gt;runtime&lt;/scope&gt;
 	&lt;/dependency&gt;
 	&lt;dependency&gt;
 		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 		&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
 		&lt;scope&gt;test&lt;/scope&gt;
 	&lt;/dependency&gt;
 &lt;/dependencies&gt;

 &lt;build&gt;
 	&lt;plugins&gt;
 		&lt;plugin&gt;
 			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 			&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
 		&lt;/plugin&gt;
 	&lt;/plugins&gt;
 &lt;/build&gt;
 &lt;repositories&gt;
 	&lt;repository&gt;
 		&lt;id&gt;spring-milestones&lt;/id&gt;
 		&lt;name&gt;Spring Milestones&lt;/name&gt;
 		&lt;url&gt;https://repo.spring.io/milestone&lt;/url&gt;
 		&lt;snapshots&gt;
 			&lt;enabled&gt;false&lt;/enabled&gt;
 		&lt;/snapshots&gt;
 	&lt;/repository&gt;
 &lt;/repositories&gt;
 &lt;pluginRepositories&gt;
 	&lt;pluginRepository&gt;
 		&lt;id&gt;spring-milestones&lt;/id&gt;
 		&lt;name&gt;Spring Milestones&lt;/name&gt;
 		&lt;url&gt;https://repo.spring.io/milestone&lt;/url&gt;
 		&lt;snapshots&gt;
 			&lt;enabled&gt;false&lt;/enabled&gt;
 		&lt;/snapshots&gt;
 	&lt;/pluginRepository&gt;
 &lt;/pluginRepositories&gt;

&lt;/project&gt;

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(&quot;/users&quot;)
	public List&lt;User&gt; retriveAllUsers() {
		return service.findAll();
	}
	 
	@GetMapping(&quot;/users/{id}&quot;)
	public User retriveUser(@PathVariable int id) {
		User user = service.findOne(id);
		
		if (user==null)
			throw new UserNotFoundException(&quot;id:&quot;+id);
		
		return user;
	}
	
	@DeleteMapping(&quot;/users/{id}&quot;)
	public void deleteUser(@PathVariable int id) {
		service.deleteById(id);
	}
	
	@PostMapping(&quot;/users&quot;)
	public ResponseEntity&lt;User&gt; createUser(@Valid @RequestBody User user) {
		User savedUser = service.save(user);
		URI location = ServletUriComponentsBuilder.fromCurrentRequest().path(&quot;/{id}&quot;).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

huangapple
  • 本文由 发表于 2023年6月26日 09:10:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定