2015-10-27 35 views
0

我正在開發一個帶有Jersey和Hibernate的小型REST Web服務器。一路上,我遇到了代表Hibernate的奇怪行爲。也就是說,在curl的POST請求之後嘗試Session.save()時,服務器會回覆500,但在命令窗口中看不到任何錯誤。HIbernate不會拋出異常或顯示錯誤

但是,當我切換一個斷點,並執行光標的一個步驟運行在依賴關係中的一些罐子裏,我最終追蹤錯誤信息。它仍然沒有顯示在任何地方。

如何強制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(); 
} 

服務器響應捲曲:

$捲曲-i -X POST -H 「內容類型:應用程序/ JSON」 - data @ comm.json localhost:8000/task/19/comments/new

HTTP/1.1 500請求失敗。 的Content-Type:text/html的;字符集= ISO-8859-1 日期:星期二,2015年10月27日20時34分16秒GMT 連接:關閉 的Content-Length:在控制檯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 : [email protected] 
Oct 27, 2015 9:34:15 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService 
INFO: HHH000397: Using ASTQueryTranslatorFactory 

對於成功的請求,它看起來是一樣的,除了一些從休眠狀態回顯的SQL。

調試時我終於在org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher類

java.lang.ClassCastException遇到異常的地方:java.lang.Long中不能轉換爲java.lang.Integer

這發生了好幾次,有不同的錯誤信息。

如何強制這些組件向stdout或stderr顯示錯誤消息?或者將它們記錄在某處?

回答

0

這就是這樣的情況我只註冊ExceptionMapper<Throwable>。這些用於將異常映射到Response。有時在有未處理的異常情況下,Jersey會吞下它,向客戶端發送一個有時無用的500 Server Error。在這些情況下,ExceptionMapper將允許使用處理它自己,在leasting發送響應

@Provider 
public class DebugExceptionMapper implements ExceptionMapper<Throwable> { 

    @Overrride 
    public Response toResponse(Throwable e) { 
     e.printStackTrace(); 
     return Response.serverError() 
      .entity(e.getMessage()) 
      .build(); 
    } 
} 

另請參閱之前打印的堆棧跟蹤:

+0

這解決了這個問題。非常感謝,對於遲到的確認感到抱歉。你知道這是否是記錄在澤西島的行爲,即他們說服務器代碼不會拋出異常的代碼調用它(我自己的)? – cristianmtr