【发布时间】:2018-01-11 11:27:59
【问题描述】:
我使用的是 Spring 版本 4(spring 数据),我想将 Object 作为 JSON 返回,我想知道即使没有使用 xmlRootElement 注释用户类,以下代码也能正常工作:
@RequestMapping(value = "/resources/users", method = RequestMethod.GET)
public ResponseEntity<User> getUserByLogonId(OAuth2Authentication auth) {
String userLogonId = ((org.springframework.security.core.userdetails.User) auth.getUserAuthentication()
.getPrincipal()).getUsername();
UsersServices usersServices = new UsersServicesImpl(usersOperations);
User user = usersServices.findByLogonId(userLogonId);
HttpStatus userStatus = HttpStatus.NOT_FOUND;
if (user != null) {
userStatus = HttpStatus.FOUND;
}
return new ResponseEntity<User>(user, userStatus);
}
任何机构都可以解释一下吗? ResponseBody/ResponseEntity 是自己做的吗?当我需要注释要作为 JSON 返回的对象类时。
【问题讨论】:
-
你在使用 Spring Boot 吗?
-
我正在使用弹簧数据
-
你在使用@RestController 吗?
-
是的,没错,restcontroller 默认包含 responsebody 对吧?
标签: java spring spring-mvc spring-data