SpringMVC字段验证返回400错误码

错误信息

HTTP Status 400 – Bad Request

Type Status Report

Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

错误原因

如果一个方法中有参数被 @Valid 标注了,但该参数后面没有紧跟一个 BindingResult 类型的参数,那么提交到该方法时,将返回 400 错误。

解决方法

//@Valid后跟个JavaBean,紧跟其后的参数必定是BindingResult
@RequestMapping("doLogin")
private String doLogin(@Valid LoginBean loginBean, BindingResult bindingResult, Model model) {
    return null;
}

SpringMVC整合Fastjson

    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8value>
                        <value>application/jsonvalue>
                    list>
                property>
            bean>
        mvc:message-converters>
    mvc:annotation-driven>

你可能感兴趣的:(JavaEE实战)