2016-08-05 111 views
0

我在新澤西的應用程序的簡單方法,它返回一個名爲PodList複雜的對象:爲什麼當我使用Jackson返回JSON時,Grizzly返回500「請求失敗」?

@GET 
 
\t @Produces(MediaType.APPLICATION_JSON) 
 
\t @Path("/x") 
 
\t public PodList getPods(){ 
 
\t \t try (KubernetesClient client = new DefaultKubernetesClient();) { \t 
 
\t \t \t PodList pl = client.pods().list(); 
 
\t \t \t ObjectMapper om = new ObjectMapper(); 
 
\t \t \t try { 
 
\t \t \t \t String x = om.writeValueAsString(pl); 
 
\t \t \t \t System.out.println(x); 
 
\t \t \t } catch (JsonProcessingException ex) { 
 
\t \t \t \t Logger.getLogger(CatalogEndPoint.class.getName()). 
 
\t \t \t \t \t \t log(Level.SEVERE, null, ex); 
 
\t \t \t } 
 
\t \t \t return pl; 
 
\t \t } \t 
 
\t }

無例外被捕獲在catch塊,我看到打印輸出,所以我敢肯定傑克遜能夠序列化這種類型。

在我的灰熊設置代碼,我肯定要做到這一點:

 final ResourceConfig rc = new ResourceConfig(); 
 
     rc.packages(CatalogEndPoint.class.getPackage().getName()); 
 
     rc.register(JacksonFeature.class); 
 
\t \t rc.property("jersey.config.server.tracing.type", "ALL"); 
 
\t \t rc.property("jersey.config.server.tracing.threshold", "VERBOSE");

請注意,我啓用了服務器跟蹤,但它是無用的。當我在CatalogEndPoint資源上點擊我的「/ x」路徑時,它不會跟蹤任何內容。所以,我所做的就是設置一個Java logging.properties文件:

#logging.properties file. 
 
# "handlers" specifies a comma separated list of log Handler 
 
# classes. These handlers will be installed during VM startup. 
 
# Note that these classes must be on the system classpath. 
 
# By default we only configure a ConsoleHandler, which will only 
 
# show messages at the INFO and above levels. 
 

 
\t handlers= java.util.logging.ConsoleHandler 
 

 
# Default global logging level. 
 
# This specifies which kinds of events are logged across 
 
# all loggers. For any given facility this global level 
 
# can be overriden by a facility-specific level. 
 
# Note that the ConsoleHandler also has a separate level 
 
# setting to limit messages printed to the console. 
 
.level=ALL 
 
com.sun.jersey.level=ALL 
 

 

 
# Limit the messages that are printed on the console to INFO and above. 
 

 
    java.util.logging.ConsoleHandler.level = ALL 
 
    java.util.logging.ConsoleHandler.formatter = 
 
            java.util.logging.SimpleFormatter

然後我終於可以看到堆棧跟蹤

INE: service exception 
 
org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.type.TypeFactory.constructType(Ljava/lang/reflect/Type;Ljava/lang/Class;)Lcom/fasterxml/jackson/databind/JavaType; 
 
\t at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer$ResponseWriter.rethrow(GrizzlyHttpContainer.java:324) 
 
\t at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer$ResponseWriter.failure(GrizzlyHttpContainer.java:306) 
 
\t at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:509) 
 
\t at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:334) 
 
\t at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) 
 
\t at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) 
 
\t at org.glassfish.jersey.internal.Errors.process(Errors.java:315) 
 
\t at org.glassfish.jersey.internal.Errors.process(Errors.java:297) 
 
\t at org.glassfish.jersey.internal.Errors.process(Errors.java:267) 
 
\t at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) 
 
\t at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305) 
 
\t at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154) 
 
\t at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:384) 
 
\t at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:224) 
 
\t at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591) 
 
\t at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571) 
 
\t at java.lang.Thread.run(Thread.java:745) 
 
Caused by: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.type.TypeFactory.constructType(Ljava/lang/reflect/Type;Ljava/lang/Class;)Lcom/fasterxml/jackson/databind/JavaType; 
 
\t at com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector._fullSerializationType(JaxbAnnotationIntrospector.java:1465) 
 
\t at com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector._findContentAdapter(JaxbAnnotationIntrospector.java:1401) 
 
\t at com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector.findSerializationContentConverter(JaxbAnnotationIntrospector.java:804) 
 
\t at com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair.findSerializationContentConverter(AnnotationIntrospectorPair.java:372) 
 
\t at com.fasterxml.jackson.databind.ser.std.StdSerializer.findConvertingContentSerializer(StdSerializer.java:417) 
 
\t at com.fasterxml.jackson.databind.ser.std.MapSerializer.createContextual(MapSerializer.java:346) 
 
\t at com.fasterxml.jackson.databind.SerializerProvider.handlePrimaryContextualization(SerializerProvider.java:928) 
 
\t at com.fasterxml.jackson.databind.ser.AnyGetterWriter.resolve(AnyGetterWriter.java:89) 
 
\t at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.resolve(BeanSerializerBase.java:350) 
 
\t at com.fasterxml.jackson.databind.ser.SerializerCache.addAndResolveNonTypedSerializer(SerializerCache.java:197) 
 
\t at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:1177) 
 
\t at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:490) 
 
\t at com.fasterxml.jackson.databind.SerializerProvider.findTypedValueSerializer(SerializerProvider.java:688) 
 
\t at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:107) 
 
\t at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1428) 
 
\t at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:930) 
 
\t at com.fasterxml.jackson.jaxrs.base.ProviderBase.writeTo(ProviderBase.java:650) 
 
\t at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:265) 
 
\t at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:250) 
 
\t at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) 
 
\t at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:106) 
 
\t at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) 
 
\t at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:86) 
 
\t at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) 
 
\t at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130) 
 
\t at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:711) 
 
\t at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:444) 
 
\t at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:434) 
 
\t at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:329) 
 
\t ... 13 more

+0

可能是您的編譯時和運行時環境之間的庫版本差異。 –

回答

0

可能編譯時和運行時環境之間的庫版本差異。

有消息稱你的編譯代碼試圖調用方法

constructType(Type t, Class c) 
com.fasterxml.jackson.databind.type.TypeFactory

但方法不存在。由於編譯成功,因此您針對具有該方法的庫的版本進行編譯,但在運行時可用的版本中不可用。

0

在我的項目的Maven pom.xml中,我明確包含了Jackson的依賴關係。這是一件錯誤的事情,因爲它導致了Jackson Jersey期待的版本之間的版本不匹配。解決的辦法是讓澤西決定使用什麼版本的傑克遜,通過在我的POM這種依賴關係,而不是試圖挑選自己傑克遜版本:

<dependency> 
     <groupId>org.glassfish.jersey.media</groupId> 
     <artifactId>jersey-media-json-jackson</artifactId> 
    </dependency> 

另見:Should we use jersey-media-json-jackson or jackson-jaxrs-json-provider in Jersey 2.5.1?

另請參見:Spring 4.2.3 and fasterxml Jackson 2.7.0 are incompatible

相關問題