参考Jersey_返回JSON格式
1. User类
//说明:将该类转化成XML时,说明这个是XML的根节点
@XmlRootElement
public class User {
private String userId;
private String userName;
private String age;
public User() {};
public User(String userId, String userName, String age) {
this.userId = userId;
this.userName = userName;
this.age = age;
}
//getter setter
)
2.Controller类
@Singleton
@Component
@RestController
@Path("/user")
public class JerseyControllerXml {
//返回多节点xml
@GET
@Produces(MediaType.APPLICATION_XML)
public List<User> getAllUser(){
List<User> users = new ArrayList<User>();
users.add(new User("001","HuiJia","26"));
users.add(new User("002","Andy","23"));
users.add(new User("003","BoWen","21"));
return users;
}
//返回单节点xml
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/getUser")
public User getUser(){
User user = new User("004","Lucy","26");
return user;
}
//返回Json
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/getUserJson")
public User getUserJson(){
User user = new User("004","Lucy","26");
return user;
}
}