controller参数接收

  • restful( someUrl/{paramId})格式url中参数

@PathVariable

@RequestMapping(value = "/cxhdlb/{param}", method = RequestMethod.GET)
    public List<String> findEventList(@PathVariable String param) {
        System.out.println(param);
}
  • request header部分

@requestHeader @cookieValue

header:

Host                    localhost:8080
Accept                  text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language         fr,en-gb;q=0.7,en;q=0.3
Accept-Encoding         gzip,deflate
Accept-Charset          ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive              300

接收:

@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
                              @RequestHeader("Keep-Alive") long keepAlive)  {
          //...
}

cookie部分类似

  • request body部分

@requestBody @requestParam @Validated

@RequestParam注解用来接收地址中的参数,参数的格式是http://*****?uid=111111&uname=张三:

@Controller
@RequestMapping("/users")
public class UserController{
    @RequestMapping(value = "/hqyhxx",method = RequestMethod.GET)
    public String getUserInfo(@RequestParam("uid") String uid,@RequestParam("uname") String uname) {
     //... 
  }
}

@Validated注解可以用一个模型来接收地址栏中的参数,参数的格式是http://*****?uid=111111&uname=张三:

@Controller
@RequestMapping("/users")
public class UserController{
    @RequestMapping(value = "/hqyhxx",method = RequestMethod.GET)
    public String getUserInfo(@Validated User user) {
         String uid = user.getUid();
      String uname = user.getUname();
    }
}

@RequestBody注解用来接收request的body中的参数:

@RequestMapping(value = "/cjhd", method = RequestMethod.POST)
    public Result createEvent(@RequestBody ParameterModel parameterModel, HttpServletRequest request,
            HttpServletResponse response) {
     String rowkey = parameterModel.getRowkey();
}

你可能感兴趣的:(controller参数接收)