杰克逊元帅/非元帅在《 Jaxrs Wildfly 15》中表现不同

问题描述

为了给出一个基础,我们正在将应用程序从JBoss 7迁移到Wildfly 15,作为其中的一部分,我们将FasterXML(2.9.5),RestEasy(3.6.2.final)的所有依赖项更新到该版本。 Wildfly 15模块支持功能。 我们还更新了代码库,以使用fastxml代替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","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"}
   ],}

使用上述有效负载profiles对象中的groupsuser参数被设置为null,而JBoss 7和jackson1及其相关配置则不是这种情况。 我们不确定不确定是否要进行迁移吗?

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

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

我们不确定我们之前工作的迁移中缺少什么。同样,我们不确定此注释的副作用。 这里的另一个问题是我们在整个应用程序中具有相似的种类模式(大约250个Java文件)。因此,我们担心它会影响功能

我们从根本上缺少什么吗?有人可以在这里说些什么吗?经过一个多星期的调查,我们已经到达这个论坛。

要提及的另一个重要点是,作为此过程的一部分,我们还将Java 1.7迁移到jdk11。

在此先感谢您的帮助。

解决方法

这可以自定义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