java.lang.ClassNotFoundException: javax.validation.Validation
java.lang.ClassNotFoundException: javax.validation.Validation
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import lombok.extern.slf4j.Slf4j;
import com.example.demo.entity.Country;
import com.example.demo.repository.CountryRepository;
@Slf4j
@RestController
@RequestMapping("/countries")
public class CountryController {
@Autowired
private Country country;
@PostMapping
public Country addCountry(@RequestBody @Valid Country country){
log.info("Started");
// Create validator factory
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
// Validation is done against the annotations defined in country bean
Set<ConstraintViolation<Country>> violations = validator.validate(country);
List<String> errors = new ArrayList<String>();
// Accumulate all errors in an ArrayList of type String
for (ConstraintViolation<Country> violation : violations) {
errors.add(violation.getMessage());
}
// Throw exception so that the user of this web service receives appropriate error message
if (violations.size() > 0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, errors.toString());
}
return country;
}
}
每当我发送 post 请求时,我都会收到错误 java.lang.ClassNotFoundException: javax.validation.Validation。我试着在 git bash -> curl -i -H 'Content-Type: application/json' -X POST -s -d '{"code":"IN","nae ":"印度"}' http://localhost:8080/countries
我得到了错误
您必须将 javax.validation
API 的实现添加到项目的依赖项中。
如果您使用 Maven 构建它,则可以在 pom
文件的依赖项部分添加以下内容。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.2.Final</version>
</dependency>
此问题是由于 hibernate-validator 依赖项的版本不正确造成的。您需要尝试此依赖的其他版本才能解决此问题。
在您的 pom 文件中添加以下依赖项可能会解决问题:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import lombok.extern.slf4j.Slf4j;
import com.example.demo.entity.Country;
import com.example.demo.repository.CountryRepository;
@Slf4j
@RestController
@RequestMapping("/countries")
public class CountryController {
@Autowired
private Country country;
@PostMapping
public Country addCountry(@RequestBody @Valid Country country){
log.info("Started");
// Create validator factory
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
// Validation is done against the annotations defined in country bean
Set<ConstraintViolation<Country>> violations = validator.validate(country);
List<String> errors = new ArrayList<String>();
// Accumulate all errors in an ArrayList of type String
for (ConstraintViolation<Country> violation : violations) {
errors.add(violation.getMessage());
}
// Throw exception so that the user of this web service receives appropriate error message
if (violations.size() > 0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, errors.toString());
}
return country;
}
}
每当我发送 post 请求时,我都会收到错误 java.lang.ClassNotFoundException: javax.validation.Validation。我试着在 git bash -> curl -i -H 'Content-Type: application/json' -X POST -s -d '{"code":"IN","nae ":"印度"}' http://localhost:8080/countries 我得到了错误
您必须将 javax.validation
API 的实现添加到项目的依赖项中。
如果您使用 Maven 构建它,则可以在 pom
文件的依赖项部分添加以下内容。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.2.Final</version>
</dependency>
此问题是由于 hibernate-validator 依赖项的版本不正确造成的。您需要尝试此依赖的其他版本才能解决此问题。
在您的 pom 文件中添加以下依赖项可能会解决问题:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>