同福

使用SpringBoot实现REST接口服务的时候识别请求方式的方法

介绍

介绍

福哥在用SpringBoot制作REST接口服务的时候需要判断当前请求方式是POST?还是PUT?还是DELET?还是GET?

从网上一搜,全是乱七八糟的,自己桌面吧~~

教程

代码

崩废话了,直接上代码了

@RequestMapping("/test")
public String responseTest() {
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = attributes.getRequest();
    HttpServletResponse response = attributes.getResponse();

    switch (request.getMethod()) {
        case "POST":

            break;
        case "PUT":

            break;
        case "DELETE":

            break;
        default:

            break;
    }
    
    return request.getMethod();
}