springboot不同功能注解的汇总

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页面的注解和示例

猜你喜欢

转载自www.cnblogs.com/hustdc/p/11333068.html