Jersey_返回XML格式

参考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 getAllUser(){
        List users = new ArrayList();
        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;
    }
}

3. 访问

http://localhost:8087/rest/user

Jersey_返回XML格式_第1张图片
Paste_Image.png

你可能感兴趣的:(Jersey_返回XML格式)