簡體   English   中英

@Valid Bean驗證在球衣中不起作用

[英]@Valid Bean validation not working in jersey

在我的pom.xml文件中,我添加了以下這些依賴項

 <dependency>
     <groupId>org.glassfish.jersey.ext</groupId>
     <artifactId>jersey-bean-validation</artifactId>
     <version>2.25.1</version>
  </dependency> 

在擴展ResourceConfig CustomApplication類中,我注冊了這2個休閑屬性

property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true); 
property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);

我有一個這樣的DTO(POJO)

import java.util.ArrayList;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;

public class PlotDTO {

private String guid;
@NotNull
private String plot_name;
@NotNull
private String farmer_id;
@NotNull
private float distance_from_warehouse;
@NotNull
private int area;
@NotNull
private float sand;
.............
.............
}

在我的資源類中,我正在嘗試使用@Valid對此進行驗證

@Path("addplot")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addPlot(@Valid PlotDTO plot) 
{
 ..............
 ..............
}

當我發送請求時,這些@NotNull驗證都不起作用

有人可以幫我弄這個嗎

@NotNull  

注解僅對Strings和Wrappers有效。 它不適用於int,float和boolean等原語,因為它們已經具有默認值。

請執行以下操作以進行驗證。 這個對我有用。

  1. 實現Validator類,用於Spring的InitializingBean
  2. 為Jersey配置實現ContextResolver<ValidationConfig>類。
  3. 實現ExceptionMaper<ConstraintViolationException>

     @Component public class BeanValidator implements Validator,InitializingBean { private Validator validator; public void afterPropertiesSet() throws Exception { ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); validator = validatorFactory.usingContext().getValidator(); } public boolean supports(Class clazz) { return true; } public void validate(Object target, Errors errors) { Set<ConstraintViolation<Object>> constraintViolations = validator.validate(target); for (ConstraintViolation<Object> constraintViolation : constraintViolations) { String propertyPath = constraintViolation.getPropertyPath().toString(); String message = constraintViolation.getMessage(); errors.rejectValue(propertyPath, "", message); } } @Override public ExecutableValidator forExecutables() { // TODO Auto-generated method stub return null; } @Override public BeanDescriptor getConstraintsForClass(Class<?> arg0) { // TODO Auto-generated method stub return null; } @Override public <T> T unwrap(Class<T> arg0) { // TODO Auto-generated method stub return null; } @Override public <T> Set<ConstraintViolation<T>> validate(T arg0, Class<?>... arg1) { // TODO Auto-generated method stub return null; } @Override public <T> Set<ConstraintViolation<T>> validateProperty(T arg0, String arg1, Class<?>... arg2) { // TODO Auto-generated method stub return null; } @Override public <T> Set<ConstraintViolation<T>> validateValue(Class<T> arg0, String arg1, Object arg2, Class<?>... arg3) { // TODO Auto-generated method stub return null; } } 2. Jersey configuration Classes. @Provider public class ValidationConfigurationContextResolver implements ContextResolver<ValidationConfig> { @Context private ResourceContext resourceContext; /**Get a context*/ @Override public ValidationConfig getContext(Class<?> type) { final ValidationConfig config = new ValidationConfig(); config.constraintValidatorFactory(resourceContext.getResource(InjectingConstraintValidatorFactory.class)); config.parameterNameProvider(new CustomParameterNameProvider()); return config; } private class CustomParameterNameProvider implements ParameterNameProvider { private final ParameterNameProvider nameProvider; public CustomParameterNameProvider() { nameProvider = Validation.byDefaultProvider().configure().getDefaultParameterNameProvider(); } @Override public List<String> getParameterNames(final Constructor<?> constructor) { return nameProvider.getParameterNames(constructor); } @Override public List<String> getParameterNames(final Method method) { return nameProvider.getParameterNames(method); } } } 

    如果您使用的是maven,則可以通過使用放置在資源路徑中的ValidationMessages.properties來實現內部化和本地化。 請參考下面的鏈接Github- bean-validation-webapp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM