gpt4 book ai didi

java - 如何让 Wildfly 使用额外的 Jackson 数据类型?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:17:23 25 4
gpt4 key购买 nike

我遇到了这个错误

03:33:14,938 WARN  [org.jboss.resteasy.core.ExceptionHandler] (default task-1) Failed executing PUT /individual/5a247ce9-0a73-4373-89ce-e4177f911259/activities/432e6e5b-4185-462f-b57e-9ec04bec3d58: org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class java.time.Instant] from JSON String; no single-String constructor/factory method (through reference chain: com.lm.activity.DTOActivity["created"])

这是因为 Wildfly 不知道如何处理瞬间。通过阅读错误,我可以简单地在那个类中处理它,但这看起来很愚蠢。我正在使用以下库并在我的 war 文件中传送它和所有更新的 jackson,但仅此一项并没有做到这一点。

    <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.3.2</version>
</dependency>

我找到了 this wiki

但是我的实现有问题,类定义语句有错误

package com.lm.infrastructure;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

@Provider
@Produces( MediaType.APPLICATION_JSON )
public class JacksonProducer implements ContextResolver<ObjectMapper> {

public JacksonProducer() throws Exception {
this.json = new ObjectMapper()
.findAndRegisterModules()
.configure( FAIL_ON_UNKNOWN_PROPERTIES, false );

}

@Override
public ObjectMapper getContext( Class<?> type ) {
return json;
}
private final ObjectMapper json;
}

我把它放在我的 web.xml 中(不要与 jboss-web.xml 混淆)

<web-app ...
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.lm.infrastructure.JacksonProducer</param-value>
</context-param>
</web-app>

我尝试修改 modules/system/layers/base/com/fasterxml/jackson/jaxrs/jackson-jaxrs-json-provider/main/module.xml 这似乎是建议 here

 <module xmlns="urn:jboss:module:1.1" name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider">
<resources>
<resource-root path="jackson-jaxrs-json-provider-2.3.2.jar"/>
<resource-root path="jackson-jaxrs-base-2.3.2.jar"/>
<resource-root path="jackson-module-jaxb-annotations-2.3.2.jar"/>
<resource-root path="jackson-databind-2.3.2.jar"/>
<resource-root path="jackson-datatype-jsr310-2.3.2.jar"/>
<!-- Insert resources here -->
</resources>

<dependencies>
<module name="javax.api"/>
<module name="javax.ws.rs.api"/>
<module name="javax.xml.bind.api"/>
<module name="com.fasterxml.jackson.core.jackson-annotations"/>
<module name="com.fasterxml.jackson.core.jackson-core"/>
<module name="com.fasterxml.jackson.core.jackson-databind"/>
<module name="com.fasterxml.jackson.datatype.jackson-datatype-jsr310"/>
</dependencies>
</module>

我的主要目标是让 jsr310 序列化和反序列化。我需要做什么才能做到这一点?但我怀疑答案将适用于 wildfly 未附带的任何其他数据类型。 (我认为 this issue 是相关的)

最佳答案

最终我被This Bug in 8.0.0.Final影响了它应该在 8.0.1 中修复,直到那时我已经降级到 8.0.0.CR1。

这是我的最终pom.xml,注意我使用的是2.2.3

    <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.2.3-beta5</version>
</dependency>

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
...
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.lm.infrastructure.JacksonProducer</param-value>
</context-param>
</web-app>

我的jboss-deployment-structure.xml这个是必须的,之前没有

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson-provider" />
<module name="org.jboss.resteasy.resteasy-jettison-provider" />
</exclusions>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jackson2-provider" services="import" />
</dependencies>
</deployment>
</jboss-deployment-structure>

我的制作人

package com.lm.infrastructure;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import com.fasterxml.jackson.databind.ObjectMapper;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

@Provider
@Produces( MediaType.APPLICATION_JSON )
public class JacksonProducer implements ContextResolver<ObjectMapper> {

public JacksonProducer() throws Exception {
this.json
= new ObjectMapper()
.findAndRegisterModules()
.configure( WRITE_DATES_AS_TIMESTAMPS, false )
.configure( FAIL_ON_UNKNOWN_PROPERTIES, false );
}

@Override
public ObjectMapper getContext( Class<?> objectType ) {
return json;
}

private final ObjectMapper json;
}

最后但并非最不重要的是我的 arquillian war 一代

public static WebArchive testWar() {
File[] libs
= Maven.resolver().loadPomFromFile( "pom.xml" )
.resolve(
"com.fasterxml.jackson.datatype:jackson-datatype-jsr310"
)
.withTransitivity()
.asFile();

return ShrinkWrap.create( WebArchive.class )
.setWebXML( new File( "src/main/webapp/WEB-INF/web.xml" ) )
.addAsWebInfResource( new File( "src/main/webapp/WEB-INF/jboss-web.xml" ) )
.addAsWebInfResource( new File( "src/main/resources/logback.xml" ) )
.addAsWebInfResource( new File( "src/main/webapp/WEB-INF/jboss-deployment-structure.xml" ) )
.addAsWebInfResource( EmptyAsset.INSTANCE, "beans.xml" )
.addPackages( false, Filters.exclude( ".*Test.*" ), getCorePackages() )
.addAsLibraries( libs );
}

关于java - 如何让 Wildfly 使用额外的 Jackson 数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22126131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com