[英]Jackson JSON / XML root elements are not the same
First, note that my issue is very similar to: Jackson xml and json root element but differs only slightly where I only want a single root element for JSON.首先,请注意,我的问题非常类似于: Jackson xml 和 json 根元素,但仅在我只需要一个JSON 根元素的情况下略有不同。
Here is my UserList class:这是我的 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() {}
}
Here is my User class:这是我的用户类:
@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 ...
}
Here is my desired JSON (currently I get "user" instead of "users" with the current test code):这是我想要的 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"
}]
}
Here is my desired XML (which is what I get with the current test code):这是我想要的 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>
Here is my test code:这是我的测试代码:
{
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);
}
}
My issue is I would like the root element of my JSON to be "users" and not "user".我的问题是我希望我的 JSON 的根元素是“用户”而不是“用户”。 Any help would be appreciated.任何帮助,将不胜感激。
@JacksonXmlProperty
is how you can rename a property in xml. @JacksonXmlProperty
是在 xml 中重命名属性的方法。 Then let name for JSON be the field name that I changed to users
below.然后让 JSON 的名称成为我在下面更改为users
的字段名称。 You can also remove most of the annotations you experimented with.您还可以删除您尝试过的大部分注释。 This will produce expected XML and JSON:这将产生预期的 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<>();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.