return type xml or json in restful

long pham :

I have some code below, and when I test it with postman. It occurs an error "500". I don't understand the advantage of "@Produces(MediaType.APPLICATION_XML)". Does it define return type automatically as XML, or not.

import java.sql.SQLException;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/UserService")
public class UserService {
    UserDAO userDAO = new UserDAO();

    @GET
    @Path("/users")
    @Produces(MediaType.APPLICATION_XML)
    public List<User> getUsers() throws ClassNotFoundException, SQLException {
        return userDAO.getAllUsers();
    }

}
Ashish Shetkar :
//To process HTTP GET requests.
 @GET

//@Path Identifies the URI path that a resource class will serve requests for.
 @Path("/abcd")

//@Produces defines the media type(s) that the methods of a resource class can produce.
@Produces(MediaType.APPLICATION_XML

I hope you have prepared the User class - with XmlRootElement and XML elements

for an example -

@XmlRootElement(name="User")
public class User{

    private int id;
    private String name; 

    public User() {

    }


    @XmlElement
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @XmlElement
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }  
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130774&siteId=1