1.@RequestParam
String uri = "http://sso.jt.com/user/login";
Map<String, String> map = new HashMap<String,String>();
//注意不要有空格 为了网络中传递的速度更快 采用简单字符传递
map.put("u", username);
map.put("p", password);
String resutJSON = httpClient.doPost(uri,map);
采用post提交,所以要用@RequestParam来接收
@RequestMapping("/login")
@ResponseBody
public SysResult doLogin(@RequestParam("u")String username,
@RequestParam("p")String password){
try {
//获取ticket信息
String ticket = userService.findUserByUP(username,password);
return SysResult.oK(ticket);
} catch (Exception e) {
e.printStackTrace();
}
return SysResult.build(201, "登录失败");
}
2.@PathVariable
在uri中传参,所以要用@PathVariable来接收
@RequestMapping("/{itemId}")
public String findItemById(@PathVariable Long itemId,Model model){
Item item = itemService.findItemById(itemId);
model.addAttribute("item", item);
return "item";
}
3.不使用任何注解,直接按传参名来接收
www.jt.com/findItemCat?callback=方法名称 ....(hello,say)
public void findItemCatAll(String callback,HttpServletResponse response){
//查询商品的全部的分类信息
ItemCatResult itemCatResult =
itemCatService.findItemCatAll();
String JSONData = null;
try {
JSONData = objectMapper.writeValueAsString(itemCatResult);
response.setContentType("html/text;charset=utf-8");
String jsonPString = callback+"("+JSONData+")";
response.getWriter().write(jsonPString);
} catch (Exception e) {
e.printStackTrace();
}
}
直接用参数名来接收不需要任何注解