2013-07-16 95 views
11

我跟隨澤西島2.0文檔(https://jersey.java.net/documentation/latest/user-guide.html#json.moxy),修改pom.xml,包括澤西媒體moxy神器,編譯和安裝。我可以基本POJO到生產和消費案例的JSON映射工作。澤西2.0和Moxy內部服務器錯誤,但沒有服務器日誌

但是,當我嘗試使用複雜數據類型作爲資源返回類型的一些POJO時,出現了很多Status 500 Internal Server Error,但沒有任何服務器日誌。這是非常煩人的。有人知道它是一個錯誤還是我錯過了配置中的某些東西?

順便說一句,爲了對POJO對象使用Moxy映射,POJO需要有一個空的參數構造函數。還有其他要求嗎?

+0

參見[我的回答(http://stackoverflow.com/a/40896460/1751640) – Mordechai

回答

1

官方Jersey示例演示如何整合EclipseLink MOXy可以在下面的鏈接中找到:

哪裏可以找到爲內部服務器錯誤的堆棧跟蹤將取決於您的設置,你能提供關於你的更多信息嗎?

使用上面的例子,如果我從org.glassfish.jersey.examples.jsonmoxy.TestBean類中刪除默認的構造函數,運行mvn test時我會得到以下例外。其中一個是好的,另一個是壞的。我將會跟進澤西島的領導來制定正確的行爲。

壞異常

testGet(org.glassfish.jersey.examples.jsonmoxy.JsonResourceTest) Time elapsed: 0.507 sec <<< ERROR! 
javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error 
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:904) 
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:749) 
    at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:88) 
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:650) 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315) 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297) 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228) 
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:426) 
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:646) 
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:375) 
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:275) 
    at org.glassfish.jersey.examples.jsonmoxy.JsonResourceTest.testGet(JsonResourceTest.java:76) 

良好的異常

roundTripTest(org.glassfish.jersey.examples.jsonmoxy.JsonResourceTest) Time elapsed: 0.048 sec <<< ERROR! 
javax.ws.rs.WebApplicationException: HTTP 500 Internal Server Error 
    at org.eclipse.persistence.exceptions.JAXBException.factoryMethodOrConstructorRequired(JAXBException.java:144) 
    at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.finalizeProperties(AnnotationsProcessor.java:896) 
    at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processClassesAndProperties(AnnotationsProcessor.java:282) 
    at org.eclipse.persistence.jaxb.compiler.Generator.<init>(Generator.java:150) 
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1017) 
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:174) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:165) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:152) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:112) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:102) 
    at org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.getJAXBContext(MOXyJsonProvider.java:302) 
    at org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.writeTo(MOXyJsonProvider.java:787) 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:194) 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139) 
    at org.glassfish.jersey.filter.LoggingFilter.aroundWriteTo(LoggingFilter.java:268) 
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139) 
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1005) 
    at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:430) 
    at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:290) 
    at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:203) 
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:215) 
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:650) 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315) 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297) 
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228) 
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:426) 
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:646) 
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:402) 
    at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:305) 
    at org.glassfish.jersey.examples.jsonmoxy.JsonResourceTest.roundTripTest(JsonResourceTest.java:84) 

UPDATE

與EclipseL開始墨水2.5.1您可以使用MOXyJsonProvider獨立測試您的模型。

import java.lang.reflect.Field; 
import java.util.*; 
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider; 

public class Demo { 

    private List<Foo> foos; 

    public static void main(String[] args) throws Exception { 
     MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider(); 

     Field field = Demo.class.getDeclaredField("foos"); 

     List<Foo> foos = new ArrayList<Foo>(1); 
     Foo foo = new Foo(); 
     foo.setBar("Hello World"); 
     foos.add(foo); 

     moxyJsonProvider.writeTo(foos, field.getType(), field.getGenericType(), null, null, null, System.out); 
    } 

} 
+0

感謝您的答覆。我按照以下文檔(https://jersey.java.net/documentation/latest/modules-and-dependencies.html#server-jdk)將我的應用程序部署爲Servlet到Jetty服務器。我也嘗試了與Tomcat服務器相同的servlet。回送500內部服務器錯誤時,服務器都沒有服務器登錄標準輸出或服務器日誌文件。 –

+3

澤西島最近有一個錯誤跟蹤這個缺少的日誌問題:https://java.net/jira/browse/JERSEY-2000 –

5

我有同樣的問題,發現這個帖子:

實現ApplicationEventListener筆者登錄由莫西拋出的異常。這是我的實現(需要在應用程序中進行註冊):

public class ExceptionListener implements ApplicationEventListener { 

    @Override 
    public void onEvent(ApplicationEvent event) { 

    } 

    @Override 
    public RequestEventListener onRequest(RequestEvent requestEvent) { 
     return new ExceptionRequestEventListener(); 
    } 

    public static class ExceptionRequestEventListener implements RequestEventListener{ 
     private final Logger logger; 

     public ExceptionRequestEventListener(){ 
      logger = Logger.getLogger(getClass()); 
     } 

     @Override 
     public void onEvent(RequestEvent event) { 
      switch (event.getType()){ 
       case ON_EXCEPTION: 
        Throwable t = event.getException(); 
        logger.error("Found exception for requestType: "+event.getType(), t); 
      } 
     } 
    } 
} 
+0

Thx,這真的幫了我。 – wvdz

相關問題