【问题标题】:Jackson marshal/unmarshal behaving differently in jaxrs wildfly 15Jackson marshal/unmarshal 在 jaxrs wildfly 15 中表现不同
【发布时间】:2021-01-04 04:50:20
【问题描述】:

作为背景,我们正在将我们的应用程序从 JBoss 7 迁移到 Wildfly 15 作为其中的一部分,我们将 FasterXML (2.9.5)、RestEasy (3.6.2.final) 的所有依赖项更新到版本Wildfly 15 模块支持。 我们还更新了代码库以使用 fasterxml 而不是 codehaus。至此编译部署成功。当我们通过邮递员触发 REST 请求时,某些参数无法反序列化。示例如下。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User", propOrder = {
    "userInfo",
    "profiles",
    "groups"
})
public class User extends NfvdResource implements IUser {
    
    @XmlElement(name = "user-info", required = true)
    protected UserInfo userInfo;
    protected Profiles profiles;
    protected Groups groups;
..
..
}


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Groups", propOrder = {
    "group"
})
public class Groups implements IGroups {
    
    private List<Group> group;
 @Override
    public List<Group> getGroup() {
        if (group == null) {
            group = new ArrayList<Group>();
        }
        return this.group;
    }

    @Override
    public String toString() {
        return "Groups [group=" + group + "]";
    }

}

@XmlRootElement( name = "user")
public interface IUser extends IProfiles, IGroups {

    @JsonProperty("user-info")
    public UserInfo getUserInfo();

    @JsonIgnore
    public Profiles getProfiles();

    @JsonIgnore
    public Groups getGroups();

}

@XmlRootElement(name = "profiles")
public interface IProfiles {
    
    @JsonProperty("profiles")
    public List<Profile> getProfile();
}

public interface IGroups {

    @JsonProperty("groups")
    public List<Group> getGroup();
}

示例载荷如下。

{
   "user-info":{"username":"dem115","name":"dem115","surname":"dem115","phonenumber":"123546","email":"abc@xyz.com","preferred-language":"en-us","preferred-theme":"light","role":"domain","password":"xxxx","public-key":"TBD"
},
   "profiles":[
            {"type":"domain","name":"administrator","description":"","operations":[]},
            {"type":"domain","name":"scriptManager","description":"","operations":[]}
   ],
   "groups":[
      {"domain":"sample.domain","datacenter":null,"organization":null,"tenant":null,"vnf":null,"type":"domain","@uri":"/abc/domains/95b3c440-843e-4163-b737-cc0f273238c1","@internal-id":"xxxxxx-843e-4163-b737-cc0f273238c1"}
   ],
}

使用上述有效负载,user 对象中的 profilesgroups 参数设置为 null,这在 JBoss 7 和 jackson1 以及相关配置中并非如此。 作为迁移的一部分,我们是否缺少某些配置是我们不确定的。

为了使其正常工作,我们提出了以下解决方法。只需将 @JsonUnwrapped 注释添加到 profilesgroups 字段编组和解组即可。

@JsonUnwrapped
protected Profiles profiles;
@JsonUnwrapped
    protected Groups groups;

我们不确定我们之前进行的迁移中缺少什么。我们也不确定这个注释的副作用。 这里的另一个问题是我们在整个应用程序中有类似的模式(大约 250 个 java 文件)。所以我们担心它会影响功能。

我们从根本上缺少什么吗?有人可以在这里阐明一下吗?在尝试调查了一个多星期后,我们正在访问此论坛。

另一个重要的一点是,我们也在将 java 1.7 迁移到 jdk11 作为此过程的一部分。

提前感谢您的帮助。

【问题讨论】:

标签: jboss jackson jax-rs wildfly resteasy


【解决方案1】:

这可以自定义 Jackson 在 json 中序列化 bean 的方式。 例如,在 CDI 容器中(如 Wildfly):

@Provider
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public class CustomizedJsonProvider extends JacksonJsonProvider {

    public CustomizedJsonProvider() {
        super();
        super.setMapper( initMapper() );
    }
    
    private static ObjectMapper initMapper() {
         ObjectMapper mapper = new ObjectMapper();
         mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)          
             .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)                                              
            .setSerializationInclusion(JsonInclude.Include.NON_NULL);
         return mapper; 
    }
    
}

查看所有功能here

【讨论】:

    猜你喜欢
    • 2017-10-05
    • 2018-10-15
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多