@RequestParam与实际传参对不上导致,会抛出异常
可以定义一个MissingServletRequestParameterException的异常处理,获取、打印出要的接口信息
import cn.hutool.core.util.ObjectUtil; import com.byd.common.api.vo.Result; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.MissingServletRequestParameterException; 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.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; /** * @Author haoxinren * @Date 2024/6/21 13:54 */ @ControllerAdvice @Slf4j public class GlobalExceptionHandler { @ExceptionHandler(MissingServletRequestParameterException.class) @ResponseBody public Result<String> handleMissingServletRequestParameterException(MissingServletRequestParameterException ex) { String parameterName = ex.getParameterName(); String parameterType = ex.getParameterType(); log.error("接口传参异常,参数:{},参数类型:{}", parameterName, parameterType); StringBuilder resultMsg = new StringBuilder("error, MissingServletRequestParameterException."); RequestAttributes ra = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes) ra; if (ObjectUtil.isNotNull(sra)) { HttpServletRequest request = sra.getRequest(); String url = request.getRequestURL().toString(); String method = request.getMethod(); String uri = request.getRequestURI(); String queryString = request.getQueryString(); log.error("url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString); resultMsg.append(" RequestURI: ").append(uri); } return Result.error(resultMsg.toString()); } }
标签:MissingServletRequestParameterException,String,web,Required,request,org,import From: https://www.cnblogs.com/wx23/p/18260809