I am new to Spring, REST, and Hibernate. That said, I've tried to put together an Enterprise-class controller method, which I plan to use as a pattern for future development.
What ways do you see that this can be improved? I'm sure there are plenty.
@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;
}
Exception:
ServerErrorException uses Spring's annotations to throw an HTTP 500, and the ResourceNotFoundException does the same with a 404.
Thanks, I appreciate any input.