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">
            <!-- 配置Fastjson 替换原来的jackson支持 -->
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

猜你喜欢

转载自blog.csdn.net/cx118118/article/details/77852794