springboot不同功能注解的汇总

醉酒当歌 提交于 2019-11-26 20:50:01

1 返回字符串的注解和示例

@ResponseBody@RequestMapping("/in")String hello() {    return "hello";}

2 返回json对象的注解和示例

@RequestMapping(value = "/{id}",method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<User> getUserById (@PathVariable("id") String id){
//初始化用户信息
initUserInfo();
User result = users.get(id);
HttpStatus status = result != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
if(result == null){
throw new UserNotFountException(id);
}
return new ResponseEntity<User>(result,status);
}

3 返回mvc的html页面的注解和示例

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!