2,Spring-mvc添加参数校验:避免参数格式不正确导致的400

 在有参数校验的基础上,新建异常处理类:(@ControllerAdvice("com.store.score.rpc.web")控制访问提示信息范围)

package com.store.score.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.validation.UnexpectedTypeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import com.store.score.model.response.MapResModel;

@ResponseBody
@ControllerAdvice("com.store.score.rpc.web")
public class ApiExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApiExceptionHandler.class);

    /**
     * @param exception UnexpectedTypeException
     * @param response
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(UnexpectedTypeException.class)
    public MapResModel unexpectedType(UnexpectedTypeException exception, HttpServletResponse response){
    	MapResModel mapResModel = new MapResModel();
        LOGGER.error("校验方法太多,不确定合适的校验方法。", exception);
        mapResModel.failure("校验方法太多,不确定合适的校验方法。");
        return mapResModel;
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public MapResModel messageNotReadable(HttpMessageNotReadableException exception, HttpServletResponse response){
    	MapResModel mapResModel = new MapResModel();
        LOGGER.error("请求参数不匹配。", exception);
        mapResModel.failure("请求参数不匹配:"+exception.getMessage());
        return mapResModel;
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(Exception.class)
    public MapResModel ex(MethodArgumentNotValidException exception, HttpServletResponse response){
    	MapResModel mapResModel = new MapResModel();
        LOGGER.error("请求参数不合法。", exception);
        Map<String, String> map = getErrors(exception.getBindingResult());
        mapResModel.setData(map);
        mapResModel.failure("请求参数不合法。");
        return mapResModel;
    }

    private Map<String, String> getErrors(BindingResult result) {
        Map<String, String> map = new HashMap<>();
        List<FieldError> list = result.getFieldErrors();
        for (FieldError error : list) {
            map.put(error.getField(), error.getDefaultMessage());
        }
        return map;
    }
}

 

转载于:https://my.oschina.net/u/3768722/blog/1924956

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值