1

我的 Jersey 应用程序中有一个简单的方法,它返回一个名为 PodList 的复杂对象:

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

在 catch 块中没有发现异常,我看到了打印输出,所以我确信 Jackson 能够序列化这种类型。

在我的 Grizzly 设置代码中,我肯定会这样做:

        final ResourceConfig rc = new ResourceConfig();
        rc.packages(CatalogEndPoint.class.getPackage().getName());
        rc.register(JacksonFeature.class);
		rc.property("jersey.config.server.tracing.type", "ALL");
		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.

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

4

2 回答 2

0

编译时和运行时环境之间可能存在库版本差异。

该消息说您编译的代码试图调用方法

constructType(Type t, Class c)

在课堂com.fasterxml.jackson.databind.type.TypeFactory上,但该方法不存在。由于它编译成功,您针对具有该方法的库版本进行编译,但在运行时可用的版本中不可用。

于 2016-08-05T15:39:43.763 回答
0

在我项目的 Maven pom.xml 中,我明确包含了 Jackson 依赖项。这是错误的做法,因为它导致 Jackson Jersey 的预期版本不匹配。解决方案是让 Jersey 决定使用什么版本的 Jackson,方法是在我的 pom 中包含这个依赖项,而不是自己尝试选择 jackson 版本:

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

另请参阅:我们应该在 Jersey 2.5.1 中使用 jersey-media-json-jackson 还是 jackson-jaxrs-json-provider?

另请参阅:Spring 4.2.3 和 fastxml Jackson 2.7.0 不兼容

于 2016-08-05T15:46:34.190 回答