Spring 参数效验validator与StringHttpM

2020-01-15  本文已影响0人  麦特桃塔总

Spring 参数效验与返回乱码冲突解决

spring版本 spring-core-4.3.14.RELEASE

Tomcat版本 apache-tomcat-7.0.99

JDK版本jdk1.8.0_152

关键依赖jar包

classmate-1.5.1.jar

hibernate-validator-5.1.3.Final.jar

javax.el-2.2.6.jar

javax.el-api-3.0.0.jar

validation-api-2.0.1.Final.jar

jboss-logging-3.4.0.Final.jar

关键配置文件

<mvc:annotation-driven />
    <mvc:annotation-driven validator="validator">
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <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="useCodeAsDefaultMessage" value="false"/>
        <property name="defaultEncoding" value="utf-8"/>
        <property name="cacheSeconds" value="120"/>
    </bean>

Controller 请求设置

@RequestMapping(value = "openCard", method = RequestMethod.POST)
    @ResponseBody 
    public JsonResultUtil openCard(HttpServletRequest request, @Valid @RequestBody OpenCardParam param, BindingResult errorResult){
    Validator.fieldValidate(errorResult);
    .......
    }

OpenCardParam 内部类

//该参数只有上面的controller 用,所以可以作为上面Controller的内部类
public static class OpenCardParam{
        @NotEmpty(message = "不能为空")
        private String userName;
        @NotEmpty(message = "不能为空")
        @Size(min=18, max=18,message = "身份证号码长度有误")
        private String idNo;
        @NotEmpty(message = "不能为空")
        @Size(min=11, max=11,message = "手机号码长度有误")
        private String phone;
        ......
}

返回结果封装

/**
 * Created by matt0614@qq.com on 4月22月0022.
 * 这里根据自己的业务系统,封装对应的结果对象
 */
public class Validator {

    public static JsonResultUtil fieldValidate(BindingResult errorResult) {
        if (errorResult.hasErrors()) {
            List<FieldError> errorLists = errorResult.getFieldErrors();
            List<Map> list = new ArrayList<>();
            for (FieldError fieldError : errorLists) {
                String fieldName = fieldError.getField();
                String errorMessage = fieldError.getDefaultMessage();
                Map<String,String> map = new HashMap();
                map.put(fieldName, errorMessage);
                list.add(map);
            }
            return JsonResultUtil.ok(new Gson().toJson(list));
        }
        return JsonResultUtil.ok();
    }
}
上一篇下一篇

猜你喜欢

热点阅读