当我们 Jackson 数据绑定时如何获取引用的 Pojo 名称 Json 对象
How to get Pojo name referenced Json object when we Jackson data-binding
我正在使用 Jackson 生成休息服务。我能够得到 JSON 这样的回复
[{"userName":"scott","userId":7},{"userName":"toe","userId":101}]
但我期待 Pojo 名称指向如下响应的响应
{"UserDetails":[{"userName":"scott","userId":7},{"userName":"toe","userId":101}]}
这是我的实现 class
@Service("userServices")
public class UserServiceImpl implements UserServices{
private UserServices userServices;
public UserServices getUserServices() {
return userServices;
}
public void setUserServices(UserServices userServices) {
this.userServices = userServices;
}
@Override
public List<UserDetails> getUser(UserDetails userDetails) {
ObjectMapper mapper = new ObjectMapper();
List<UserDetails> user = new ArrayList<UserDetails>();
List<UserDetails> list = new ArrayList<UserDetails>();
UserDetails userDetails2 = new UserDetails();
userDetails2.setUserName("scott");
userDetails2.setUserId(007);
UserDetails userDetails3 = new UserDetails();
userDetails3.setUserName("toe");
userDetails3.setUserId(101);
user.add(userDetails2);
user.add(userDetails3);
try{
String jsonString = mapper.writeValueAsString(user);
UserDetails[] userDetails4 = mapper.readValue(jsonString, UserDetails[].class);
list = Arrays.asList(userDetails4);
System.out.println(userDetails4);
}catch (Exception e) {
System.out.println(e);
}
return list;
}
}
这是我的 pojo
public class UserDetails implements Serializable{
private String userName;
private int userId;
public UserDetails(){
}
//getters and setters...
}
** 注意:** 我不想在域对象端使用任何注释。
您 return 一个 UserDetails 列表,它转换为您的响应对象是一个 JSON 数组。为了获得您想要的结果,您需要创建一个 Container class (UserDetailsContainer),其中包含 属性 List 类型的 userDetails 和 return 作为响应的实例。
就return
用户详细信息响应:
public class UserDetailsResponse {
private List<UserDetails> userDetails;
// getter and setter
}
而不是 UserServiceImpl 中的 UserDetails 列表(然后可能在控制器中)。
您可以使用 objectMapper.writeValueAsString(aCustomMap)
即时创建自定义 json 视图。
示例:
UserDetails userDetails2 = new UserDetails();
userDetails2.setUserName("scott");
userDetails2.setUserId(007);
UserDetails userDetails3 = new UserDetails();
userDetails3.setUserName("toe");
userDetails3.setUserId(101);
Map<String, List<UserDetails>> m = new HashMap<>();
m.put("UserDetails", Arrays.asList(userDetails2, userDetails3));
System.out.print(
//This is what you need
objectMapper.writeValueAsString(m)
);
//{"UserDetails":[{"userName":"scott","userId":7},{"userName":"toe","userId":101}]}
我正在使用 Jackson 生成休息服务。我能够得到 JSON 这样的回复
[{"userName":"scott","userId":7},{"userName":"toe","userId":101}]
但我期待 Pojo 名称指向如下响应的响应
{"UserDetails":[{"userName":"scott","userId":7},{"userName":"toe","userId":101}]}
这是我的实现 class
@Service("userServices")
public class UserServiceImpl implements UserServices{
private UserServices userServices;
public UserServices getUserServices() {
return userServices;
}
public void setUserServices(UserServices userServices) {
this.userServices = userServices;
}
@Override
public List<UserDetails> getUser(UserDetails userDetails) {
ObjectMapper mapper = new ObjectMapper();
List<UserDetails> user = new ArrayList<UserDetails>();
List<UserDetails> list = new ArrayList<UserDetails>();
UserDetails userDetails2 = new UserDetails();
userDetails2.setUserName("scott");
userDetails2.setUserId(007);
UserDetails userDetails3 = new UserDetails();
userDetails3.setUserName("toe");
userDetails3.setUserId(101);
user.add(userDetails2);
user.add(userDetails3);
try{
String jsonString = mapper.writeValueAsString(user);
UserDetails[] userDetails4 = mapper.readValue(jsonString, UserDetails[].class);
list = Arrays.asList(userDetails4);
System.out.println(userDetails4);
}catch (Exception e) {
System.out.println(e);
}
return list;
}
}
这是我的 pojo
public class UserDetails implements Serializable{
private String userName;
private int userId;
public UserDetails(){
}
//getters and setters...
}
** 注意:** 我不想在域对象端使用任何注释。
您 return 一个 UserDetails 列表,它转换为您的响应对象是一个 JSON 数组。为了获得您想要的结果,您需要创建一个 Container class (UserDetailsContainer),其中包含 属性 List 类型的 userDetails 和 return 作为响应的实例。
就return
用户详细信息响应:
public class UserDetailsResponse {
private List<UserDetails> userDetails;
// getter and setter
}
而不是 UserServiceImpl 中的 UserDetails 列表(然后可能在控制器中)。
您可以使用 objectMapper.writeValueAsString(aCustomMap)
即时创建自定义 json 视图。
示例:
UserDetails userDetails2 = new UserDetails();
userDetails2.setUserName("scott");
userDetails2.setUserId(007);
UserDetails userDetails3 = new UserDetails();
userDetails3.setUserName("toe");
userDetails3.setUserId(101);
Map<String, List<UserDetails>> m = new HashMap<>();
m.put("UserDetails", Arrays.asList(userDetails2, userDetails3));
System.out.print(
//This is what you need
objectMapper.writeValueAsString(m)
);
//{"UserDetails":[{"userName":"scott","userId":7},{"userName":"toe","userId":101}]}