gpt4 book ai didi

java - Wildfly 忽略 Jackson 注释并且不使用 @Provider 类

转载 作者:行者123 更新时间:2023-12-02 03:26:45 24 4
gpt4 key购买 nike

我环顾四周,认为这与以下任何一个都没有重复:

using Jackson annotations in Wildfly

jackson annotations being ignored

Wildfly and Jackson @JsonIgnore annotation

使用 Wildfly 10 并使用以下类部署 war :

@Provider
public class JaxApplication implements ContextResolver<ObjectMapper> {
private final ObjectMapper mapper;

public JaxApplication() {
mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
//throw new RuntimeException("HERE");
}

@Override
public ObjectMapper getContext(Class<?> type) {
throw new RuntimeException("HERE");
//return mapper;
}

}

如果没有注释,我会在部署时看到构造函数中抛出的异常,但当我向 REST 服务发出请求时,我没有看到 getContext 方法中的异常。

我在实体上有一个 @JsonIgnore 注释,但它不起作用,我正在使用的 @JsonIdentityInfo 注释也不起作用

该类看起来像这样(包括导入,以验证我正在使用 com.fasterxml.*)

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
* Created by mb995a on 7/29/2016.
*/
@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@request_id", scope=Request.class)

public class Request extends BaseEntity {

我需要在 REST 配置中做一些特殊的事情吗?

@ApplicationPath("api")
public class RestApplication extends Application {
public RestApplication() {
super();
}
}

有什么想法吗?

编辑说:我还有一个使用 ObjectMapper 的单元测试,并且它可以正确序列化。

最佳答案

对于其他人来说,我已经弄清楚了这一点。问题是我的 pom 中有 jackson jar ,但没有标记为“提供”范围。这意味着我的 war 文件的 WEB-INF/lib 目录中有 jars。

即使它们与 Wildfly 的 jar 版本完全相同(我使用与我正在使用的 WF 版本相同的版本属性),它们也无法正常工作。看起来对于来自不同 jar 的相同注释,hashCode() 是不同的,即使它们完全相同(校验和和所有内容)。

因此,解决方案是在 Maven 中为所有 Jackson 内容将依赖项标记为“提供”。

关于java - Wildfly 忽略 Jackson 注释并且不使用 @Provider 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38754831/

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