我正在使用杰克逊生成休息服务。我能够得到 JSON 响应
[{"userName":"scott","userId":7},{"userName":"toe","userId":101}]
但我期待 Pojo 名称的响应指向如下响应
{"UserDetails":[{"userName":"scott","userId":7},{"userName":"toe","userId":101}]}
这是我的实现类
@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...
}
** 注意:** 我不想在域对象端使用任何注释。