Spring Hibernate验证程序不工作,但javax正在运行

Spring Hibernate验证程序不工作,但javax正在运行,java,spring,hibernate,validation,spring-mvc,Java,Spring,Hibernate,Validation,Spring Mvc,Spring验证和Hibernate验证程序有一些问题 我使用的是Spring4和HibernateValidator5,我的问题是javax注释运行正常,但看起来Hibernate注释没有被调用 这是我的控制器 @RequestMapping(method= RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public void create(@RequestBody @Valid CreatePlantCommand comma

Spring验证和Hibernate验证程序有一些问题

我使用的是Spring4和HibernateValidator5,我的问题是javax注释运行正常,但看起来Hibernate注释没有被调用

这是我的控制器

@RequestMapping(method= RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody @Valid CreatePlantCommand command, BindingResult bindingResult, HttpServletResponse response){

    if (bindingResult.hasErrors()) {
        // do something
    }

    // do work

    // should set the location header on new requests
    String location = ServletUriComponentsBuilder.fromCurrentRequest()
            .pathSegment("{code}").buildAndExpand(code)
            .toUriString();

    response.setHeader("Location", location);
}
@RestController
@RequestMapping("/form")
public class FormController {

    @RequestMapping(method = RequestMethod.POST, consumes = "application/json")
    public List<FieldError> submitForm(@RequestBody @Valid Form form, BindingResult result) {

        if(result.hasErrors()) {
            return result.getFieldErrors();
        }

        return null;
    }
}
和命令对象

public class CreatePlantCommand {

    @NotNull
    private Integer code;

    @Length(min = 1, max = 5)
    //@Max(5)
    private String description;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
NotNull和Max(它现在被注释掉了)正在工作,但是长度似乎被完全忽略了。如果我使用大小,它就会工作,如果我使用任何Hibernate注释,它就会被忽略

我正在使用Spring中的LocalValidatoryFactoryBean

<bean id='validator' class='org.springframework.validation.beanvalidation.LocalValidatorFactoryBean'/>
如果我发布这个json

{
  "name":"adafasdfasfdsafd",
  "email":"adf",
  "age":1,
  "creditCard":"123456"
}
连接到此控制器

@RequestMapping(method= RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody @Valid CreatePlantCommand command, BindingResult bindingResult, HttpServletResponse response){

    if (bindingResult.hasErrors()) {
        // do something
    }

    // do work

    // should set the location header on new requests
    String location = ServletUriComponentsBuilder.fromCurrentRequest()
            .pathSegment("{code}").buildAndExpand(code)
            .toUriString();

    response.setHeader("Location", location);
}
@RestController
@RequestMapping("/form")
public class FormController {

    @RequestMapping(method = RequestMethod.POST, consumes = "application/json")
    public List<FieldError> submitForm(@RequestBody @Valid Form form, BindingResult result) {

        if(result.hasErrors()) {
            return result.getFieldErrors();
        }

        return null;
    }
}
@RestController
@请求映射(“/form”)
公共类窗体控制器{
@RequestMapping(method=RequestMethod.POST,consumes=“application/json”)
公共列表提交表单(@RequestBody@Valid Form,BindingResult){
if(result.hasErrors()){
返回结果。getFieldErrors();
}
返回null;
}
}
不会返回任何错误

如果我取消对@Range字段的注释并再次发布,我会得到@Email和@Range错误,而不是@Length

我假设我做错了什么,任何指导都将不胜感激

在服务器方面,我使用的是WebSphereLiberty 8.5,我的Server.xml是

<?xml version="1.0" encoding="UTF-8"?>
<server description="Default Liberty Server">
  <!-- Enable features -->
  <featureManager>
    <feature>jsp-2.2</feature>
    <feature>jndi-1.0</feature>
    <feature>jdbc-4.0</feature>
    <feature>jpa-2.0</feature>
    <feature>localConnector-1.0</feature>
    <feature>beanValidation-1.0</feature>
  </featureManager>
  <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
  <httpEndpoint id="defaultHttpEndpoint" httpPort="9080" httpsPort="9443" />
  <applicationMonitor updateTrigger="mbean" />
  <application id="problems_war_exploded" location="C:\Users\jyoung\Code\hibernate-validation-problems\target\problems-0.0.1-SNAPSHOT" name="problems_war_exploded" type="war" context-root="/hibernate-validation-problems" />
</server>

jsp-2.2
jndi-1.0
jdbc-4.0
jpa-2.0
localConnector-1.0
beanValidation-1.0

Ugh。事实证明这是一个Websphere问题。在Tomcat内部运行,一切正常。快速搜索一下Websphere和Hibernate验证器,就会发现Websphere正在Hibernate验证器之上加载自己的验证器


这就解释了为什么Hibernate注释不起作用

这很奇怪。。你能提供一个可以复制它的工作示例吗?@SlavaSemushin,我做了一些编辑,并链接到一个给我带来同样问题的项目。任何建议都将不胜感激。