Jackson JSON / XML根元素不一样

时间:2017-10-10 21:28:35

标签: java json xml jackson

首先,请注意我的问题非常类似于: Jackson xml and json root element 但只是略有不同,我只想要一个JSON的单个根元素。

这是我的UserList类:

@XmlRootElement(name = "users")
@JsonRootName(value = "users")
@JsonTypeName(value = "users")
public class UserList {

  // Tried all of these:
  // @JacksonXmlElementWrapper(localName = "user")
  // @JacksonXmlProperty(localName = "user")
  // @JsonUnwrapped
  // @JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NONE /**.NAME */)
  // @JsonProperty("users") // <-- Sets both XML and JSON to 'users'
  @JacksonXmlElementWrapper(useWrapping = false) // <-- This gets rid of duplicate 'users' in XML
  public List<User> user = new ArrayList<User>();

  public UserList() {}

}

这是我的用户类:

@XmlRootElement(name = "user")
@JsonRootName(value = "user")
@JsonInclude(Include.NON_NULL)
@XmlAccessorType(XmlAccessType.FIELD)
public class User {

  private int                  userId;
  private String               userName;
  private String               password;
  private long                 passwordUpdated;
  private long                 passwordExpire;
  private String               sessionKey;

  public User () {}

  getters and setters here ...
}

这是我想要的JSON(目前我使用当前测试代码获得“用户”而不是“用户”):

{
  "users": [{
    "userId": 1,
    "userName": "test1@user.com",
    "passwordUpdated": 0,
    "passwordExpire": 0,
    "sessionKey": "key"
  }, {
    "userId": 2,
    "userName": "test2@user.com",
    "passwordUpdated": 0,
    "passwordExpire": 0,
    "sessionKey": "key"
  }]
}

这是我想要的XML(这是我用当前测试代码得到的):

<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user>
    <userId>1</userId>
    <userName>test1@user.com</userName>
    <passwordUpdated>0</passwordUpdated>
    <passwordExpire>0</passwordExpire>
    <sessionKey>key</sessionKey>
  </user>
  <user>
    <userId>2</userId>
    <userName>test2@user.com</userName>
    <passwordUpdated>0</passwordUpdated>
    <passwordExpire>0</passwordExpire>
    <sessionKey>key</sessionKey>
  </user>
</users>

这是我的测试代码:

{

  User user1 = new User();
  user1.setUserId(1);
  user2.setUserName("test1@user.com");
  user1.setPasswordExpire(0);
  user1.setPasswordUpdated(0);
  user1.setSessionKey("key");

  User user2 = new User();
  user2.setUserId(1);
  user2.setUserName("test2@user.com");
  user2.setPasswordExpire(0);
  user2.setPasswordUpdated(0);
  user2.setSessionKey("key");

  UserList userList = new UserList();
  userList.user.add(user1);
  userList.user.add(user2);

  String json = MapperUtils.modelToJson(userList);

  String xml = MapperUtils.modelToXml(userList);

}

public class MapperUtils {

  final static ObjectMapper jsonMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  final static ObjectMapper xmlMapper = new XmlMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

  public static String modelToJson(final Object object) throws IOException {
    return jsonMapper.writeValueAsString(object);
  }

  public static String modelToXml(final Object object) throws IOException {
     return xmlMapper.writer().writeValueAsString(object);
  }

}

我的问题是我希望我的JSON的根元素是“用户”而不是“用户”。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

@JacksonXmlProperty是如何在xml中重命名属性的。然后让JSON的名称成为我在下面更改为users的字段名称。您还可以删除您尝试过的大多数注释。这将产生预期的XML和JSON:

@JsonInclude(JsonInclude.Include.NON_NULL)
@XmlAccessorType(XmlAccessType.FIELD)
class User {
    private int userId;
    private String userName;
    private String password;
    private long passwordUpdated;
    private long passwordExpire;
    private String sessionKey;
}

@JsonRootName(value = "users")
class UserList {
    @JacksonXmlProperty(localName = "user")
    @JacksonXmlElementWrapper(useWrapping = false)
    List<User> users = new ArrayList<>();
}