SpringMVC中的常用注解
@ResponseBody
@RequestMapping("/login")
@ResponseBody
public User login(User user){
return user;
}
User字段:userName pwd;
那么在前台接收到的数据为:'{"userName":"xxx","pwd":"xxx"}'
@ResponseBody
效果等同于如下代码:
@RequestMapping("/login")
public void login(User user, HttpServletResponse response){
//通过response对象输出指定格式的数据
response.getWriter.write(JSONObject.fromObject(user).toString());
}
@PathVariable
符合rest风格的api传值方式,有多个变量时,也可以接受一个Map(前提:参数必须为String类型),springboot会自动封装到Map里
@RequestMapping(value = "/param/{userId}/{username}", method = RequestMethod.GET)
public Map testParams(
@PathVariable("userId") int id,
@PathVariable("username") String name,
@PathVariable Map<String, String> userInfo
) {
Map<String, Object> obj = new HashMap<>();
obj.put("id", id);
obj.put("name", name);
obj.put("userInfo", obj);
return obj;
}
springboot会自动把id和name两个变量封装到userInfo。