【发布时间】:2014-01-15 06:52:05
【问题描述】:
我是 Spring、REST 和 Hibernate 的新手。也就是说,我已经尝试将企业级控制器方法放在一起,我计划将其用作未来开发的模式。
您认为可以通过哪些方式进行改进?我敢肯定有很多。
@RequestMapping(value = "/user", method = RequestMethod.GET)
public @ResponseBody User getUser(@RequestParam(value="id", required=true) int id) {
Session session = null;
User user = null;
try {
HibernateUtil.configureSessionFactory();
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
user = (User) session.get(User.class, id);
session.getTransaction().commit();
} catch (HibernateException e) {
if (session != null) {
session.getTransaction().rollback();
}
e.printStackTrace();
throw new ServerErrorException();
}
if (user == null) {
throw new ResourceNotFoundException();
}
return user;
}
例外:
ServerErrorException uses Spring's annotations to throw an HTTP 500, and the ResourceNotFoundException does the same with a 404.
谢谢,感谢您的任何意见。
【问题讨论】:
-
我将此标记为转移到 Code Review Stack Exchange,在那里它将得到应有的爱。
-
甚至不知道那是一回事。谢谢。
标签: java spring hibernate rest spring-mvc