2014-10-29 77 views
3

在Spring驗證和Hibernate驗證器中遇到了一些麻煩。Spring Hibernate驗證器無法正常工作,但是javax是

我使用的是Spring 4和Hibernate Validator 5,我的問題是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); 
} 

,並在NOTNULL和Max(其現在註釋掉)命令對象

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; 
    } 
} 

正在努力,但長度似乎被完全忽略。如果我使用Size,它會起作用,如果我使用任何Hibernate註釋,它將被忽略。

我使用從春天

<bean id='validator' class='org.springframework.validation.beanvalidation.LocalValidatorFactoryBean'/> 

的LocalValidatorFactoryBean爲什麼不適用Hibernate的驗證?

編輯

繼續嘗試理解這裏發生了什麼。

我注意到NotEmpty驗證是唯一有效的驗證。我可以把它放在描述字段上,而不是發送它,我會得到錯誤。如果我在電子郵件,信用卡或網址上添加說明併發送錯誤的數據,我不會收到錯誤消息。

編輯

我已經創建了一些演示這種行爲的一個小項目。它在我的bitbucket回購https://bitbucket.org/joeyoung/hibernate-validation-problems

Form對象有4個公共屬性,其中兩個被註釋掉,它們使用@Length和@Email驗證器。

public class Form { 

    @Length(max=10) 
    public String name; 

    @Email 
    public String email; 

// @Range(min=18) 
// public Integer age; 

// @CreditCardNumber 
// public String creditCard; 
} 

如果我發佈這個json;

{ 
    "name":"adafasdfasfdsafd", 
    "email":"adf", 
    "age":1, 
    "creditCard":"123456" 
} 

到該控制器

@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; 
    } 
} 

返回任何錯誤。

如果我取消@Range字段的註釋並重新發布,我會收到@Email和@Range錯誤,而不是@Length。

我假設我做錯了什麼,任何指導將不勝感激。

服務器明智的,我使用WebSphere 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> 
+0

這是非常奇怪的..你能提供一個可以重現它的工作示例嗎? – 2014-10-29 23:17:03

+0

@SlavaSemushin,我做了一些編輯並鏈接到一個給我同樣問題的項目。任何意見非常感謝。 – 2014-10-30 16:38:06

回答

2

唉。原來這是一個Websphere問題。在Tomcat內部運行一切正常。 Websphere和Hibernate驗證器的快速搜索表明,Websphere正在加載它自己的Hibernate之上。

這解釋了爲什麼Hibernate註釋不起作用。

+0

你的解決方法是什麼?謝謝。 – 2015-07-13 21:05:23