spring web PostMapping ,RequestMapping, 全局日志

注解来自于 spring-web-5.3.21.jar

例子1

复制代码
@RestController
@RequestMapping("/v1/alg")
public class AlgController {

@PostMapping(value = "/task/{taskNo}", produces = "application/json; charset=utf-8")

@ResponseBody

public BaseRep<Boolean> insertTaskResult(

@PathVariable @NotBlank String taskNo,

@RequestBody Object result) {

例子2

@Controller

@RequestMapping("")

public class GatewayController {

@RequestMapping(value = "/proxy", method = { RequestMethod.GET,

RequestMethod.POST }, produces = "application/json; charset=utf-8")

@ResponseBody

@SkipXss

public BaseResponse<Object> proxy(@RequestParam(value = "facade") String facade,

@RequestParam(value = "method") String method,

@RequestParam(value = "paramJson") String paramJson,

@RequestParam(value = "env") String env) {

get发送的时候 ,可以在body里填数据么?

全局监控

复制代码
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public BaseRep bizExceptionHandler(HttpServletRequest req, BizException e){
        LogUtil.error(LOGGER,e,"发生异常!BizException,code="+e.getErrorEnum()+",msg="+e.getMessage());
        BaseRep baseRep = new BaseRep();
        baseRep.setSuccess(false);
        baseRep.setResultCode(e.getErrorEnum().getCode());
        baseRep.setResultMessage(e.getErrorEnum().getDesc());
        return baseRep;
    }

    /**
     * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Throwable.class)
    @ResponseBody
    public BaseRep exceptionHandler(HttpServletRequest req, Throwable e) throws IOException {
        LogUtil.error(LOGGER,e,"异常!Throwable,req="+req.getRequestURL()+",input="+req.getInputStream()+",msg="+e.getMessage());
       
相关推荐
码路飞16 分钟前
热榜全是 OpenClaw,但我用 50 行 Python 就造了个桌面 AI Agent 🤖
java·javascript
Nyarlathotep011327 分钟前
LinkedList源码分析
java·后端
用户83071968408237 分钟前
Java 告别繁琐数据统计代码!MySQL 8 窗口函数真香
java·sql·mysql
前端Hardy1 小时前
别再忽略 Promise 拒绝了!你的 Node.js 服务正在“静默自杀”
前端·javascript·面试
前端Hardy1 小时前
别再被setTimeout闭包坑了!90% 的人都写错过这个经典循环
前端·javascript·vue.js
小林coding1 小时前
专为程序员打造的简历模版来啦!覆盖前端、后端、测开、大模型等专业简历
前端·后端
前端Hardy1 小时前
你的 Vue 组件正在偷偷吃掉内存!5 个常见的内存泄漏陷阱与修复方案
前端·javascript·面试
RaidenLiu1 小时前
Flutter Platform Channel 底层架构解析 —— 从 BinaryMessenger 到跨平台消息通信机制
前端·flutter·前端框架
bluceli1 小时前
CSS容器查询:响应式设计的新范式
前端·css
Tapir1 小时前
被 Karpathy 下场推荐的 NanoClaw 是什么来头
前端·后端·github