我正在使用Jersey和Hibernate开发一个小型REST Web服务器。一路走来,我遇到了一些代表Hibernate的怪异行为。也就是说,当在curl发出POST请求后尝试Session.save()一个对象时,服务器回复500,但我在命令窗口中看不到任何错误。
但是,当我切换一个断点并在游标上执行一步时,依赖项中的某些jar内部会运行,最后我会追踪错误消息。它仍然没有在任何地方显示它。
如何强制hibernate显示所有这些错误?还是需要设置泽西?
切换断点的方法:
@POST
@Path("{id}/comments/new")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response handlePostNewComment(@PathParam("id") int taskId, ItemComment newComment) {
newComment.setItemId(taskId);
Session s = HibernateUtil.getSessionFactory().openSession();
Transaction tx = s.beginTransaction();
**Integer newCommentId = (Integer)s.save(newComment);**
s.flush();
tx.commit();
s.close();
HashMap res = new HashMap();
res.put("id", newCommentId);
return Response.status(201).entity(res).build();
}
服务器响应curl:
$ curl -i -X POST -H“Content-type:application / json” - data @ comm.json localhost:8000 / task / 19 / comments / new
HTTP / 1.1 500请求失败。 Content-Type:text / html; charset = ISO-8859-1 日期:2015年10月27日星期二20:34:16 GMT 连接:关闭 内容长度:1033
IntelliJ中控制台中的消息:
Connected to the target VM, address: '127.0.0.1:57501', transport: 'socket'
task tracker enterprise Edition running
Oct 27, 2015 9:34:12 PM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [localhost:8000]
Application started.
Stop the application using CTRL+C
Oct 27, 2015 9:34:12 PM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Oct 27, 2015 9:34:14 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.2.Final}
Oct 27, 2015 9:34:14 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 27, 2015 9:34:14 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Oct 27, 2015 9:34:14 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
Oct 27, 2015 9:34:14 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Oct 27, 2015 9:34:14 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/tracker]
Oct 27, 2015 9:34:14 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=cristian, password=****}
Oct 27, 2015 9:34:14 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Oct 27, 2015 9:34:14 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Oct 27, 2015 9:34:14 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL94Dialect
Oct 27, 2015 9:34:15 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Oct 27, 2015 9:34:15 PM org.hibernate.type.BasicTypeRegistry register
INFO: HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@43aa6671
Oct 27, 2015 9:34:15 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
对于成功的请求看起来是一样的,除了从hibernate回显的一些SQL。
调试时,我终于遇到了org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher类中的异常
java.lang.ClassCastException:java.lang.Long无法强制转换为java.lang.Integer
这已多次发生,但有不同的错误消息。
如何强制这些组件在stdout或stderr上显示错误消息?或者将它们记录在某个地方?
答案 0 :(得分:0)
在这种情况下,我只是注册ExceptionMapper<Throwable>
。这些用于将例外映射到Response
。有时,如果存在未处理的异常,Jersey会吞下它,向客户端发送有时无用的500 Server Error。在这些情况下,ExceptionMapper
将允许我们自己处理它,至少在发送响应之前打印堆栈跟踪
@Provider
public class DebugExceptionMapper implements ExceptionMapper<Throwable> {
@Overrride
public Response toResponse(Throwable e) {
e.printStackTrace();
return Response.serverError()
.entity(e.getMessage())
.build();
}
}
另见: