hibernate的校正架構validation 和 HttpMessageConverter的配置方式

來源:互聯網
上載者:User

標籤:hibernate的校正架構validation 和 httpmessageconverter的配置方式

hibernate的校正架構validation 和 HttpMessageConverter的配置方式

好像是2個不相干的配置內容,但他們都用到了<mvc:annotation-driven />

結果在配置的時候一直無法糅合在一起。


解決方案:


hibernate的校正架構validator的驗證


主要說的是使用Annotaion JSR標準的驗證,

public class User {    @NotEmpty(message = "使用者名稱不可為空")        private String username;        @Size(min=6 ,max= 20 ,message = "密碼長度不符合標準")        private String password;        private String nickname;   ......}


需要在Spring的設定檔中配置校正器



 <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">        <property name="providerClass"  value="org.hibernate.validator.HibernateValidator"/>        <!--不設定則預設為classpath下的 ValidationMessages.properties -->        <property name="validationMessageSource" ref="messageSource"/>    </bean>    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">        <property name="basenames">              <list>                <!-- 在web環境中一定要定位到classpath 否則預設到當前web應用下找  -->                <value>classpath:validatemessages</value>              </list>        </property>        <property name="useCodeAsDefaultMessage" value="false"/>          <property name="defaultEncoding" value="utf-8"/>          <property name="cacheSeconds" value="120"/>      </bean>

配置好了校正器後,需要將校正器注入到處理器適配器中,

將我們配好的validator注入進去,如下:

<mvc:annotation-driven validator="validator"/>


HttpMessageConverter的配置方式

沒有validator配置的時候,因為json一直出現亂碼,所以我是這樣配置的

<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">      <constructor-arg value="UTF-8" index="0"/>      <property name="supportedMediaTypes">            <list>                  <value>text/plain;charset=UTF-8</value>            </list>      </property></bean>    <bean id="jackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">        <property name="supportedMediaTypes">            <list>                <value>text/html;charset=UTF-8</value>                <value>text/plain;charset=UTF-8</value>                <value>application/json;charset=UTF-8</value>            </list>        </property>    </bean><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">      <property name="messageConverters">            <list>                  <ref bean="stringHttpMessageConverter"/>                  <ref bean="jackson2HttpMessageConverter"/>             </list>      </property></bean><mvc:annotation-driven />

配置必須在 <mvc:annotation-driven /> 之前,否則將不會啟效


兩部分配置都要<mvc:annotation-driven />

而且好像還對位置敏感,簡單的連起來有一個配置會不生效。

最後終於用下面的寫法融合起來了

  <mvc:annotation-driven validator="validator">   <mvc:message-converters register-defaults="true">      <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">        <property name="supportedMediaTypes">          <list>           <value>text/html;charset=UTF-8</value>           <value>text/plain;charset=UTF-8</value>          <value>application/json;charset=UTF-8</value>         </list>          </property>        </bean>      </mvc:message-converters>   </mvc:annotation-driven>   <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">        <property name="providerClass"  value="org.hibernate.validator.HibernateValidator"/>        <!--不設定則預設為classpath下的 ValidationMessages.properties -->        <property name="validationMessageSource" ref="messageSource"/>    </bean>    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">        <property name="basenames">              <list>                <!-- 在web環境中一定要定位到classpath 否則預設到當前web應用下找  -->                <value>classpath:validatemessages</value>              </list>        </property>        <property name="useCodeAsDefaultMessage" value="false"/>          <property name="defaultEncoding" value="utf-8"/>          <property name="cacheSeconds" value="120"/>      </bean>




代碼不全,請見諒。因為是一邊調試一邊寫的

hibernate的校正架構validation 和 HttpMessageConverter的配置方式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.