后端获取http请求中的参数

1.参数在请求url上,如:http://{host:port}/mnagement//{ruleId}

方法:在入口方法的参数前面,使用@PathVariable注解,并且变量名与url中大括号中变量名一致,即可直接传入该方法中

获取其中{ruleId}的示例代码:

	//对应请求:http://{host:port}/mnagement/10086,获取url中的10086

    @RequestMapping(value = "/mnagement/{ruleId}", method = RequestMethod.DELETE)
    public JsonResult fun(@PathVariable long ruleId) {
        return Service.funByRuleId(ruleId );
    }

2.参数在请求url的Paramter中,如http://{host:port}/mnagement/{ruleId}?username={username}

方法:首先需要获取HttpServletRequest,然后调用request.getParameter("username")方法获取;

示例代码如下:

	//对应请求:http://{host:port}/mnagement/10086?username=zjtmeng,获取url中的10086,以及zjtmeng
    @RequestMapping(value = "/mnagement/{ruleId}", method = RequestMethod.DELETE)
    public JsonResult fun(@PathVariable long ruleId,HttpServletRequest request) {
        if (request.getParameter("username") != null)
            effectiveStatus = request.getParameter("username");
        return Service.funByRuleId(ruleId,username );
    }

3.参数在请求url的header中,url同上http://{host:port}/mnagement/{ruleId}?username={username}

方法:首先需要获取HttpServletRequest,然后调用request.getHeader("user_id")方法获取;

	//对应请求:http://{host:port}/mnagement/10086?username=zjtmeng,获取url中的10086,以及zjtmeng
    @RequestMapping(value = "/mnagement/{ruleId}", method = RequestMethod.DELETE)
    public JsonResult fun(@PathVariable long ruleId,HttpServletRequest request) {
    	String username = ull;
    	String userId = null;
        //获取url中parameter参数值
        if (request.getParameter("username") != null)
            username= request.getParameter("username");
  
  		//获取header中的参数值
        if (request.getHeader("user_id") != null)
            String userId = request.getHeader("user_id");
        return Service.funByRuleId(ruleId,username,userId );
    }

你可能感兴趣的:(springBoot)