对RESTful Web服务的GET请求不会返回私有变量

时间:2016-08-21 06:57:54

标签: java xml rest get

我正在尝试使用JAX-RS学习RESTful Web服务的实现。我正在关注this tutorial here on YouTube。在本教程中,您可以看到GET请求返回类的所有私有变量。但在我的情况下,它并没有。当我尝试时,它返回一个空白屏幕。然后当我将一些成员更改为public时,请求只返回那些成员变量。

以下是我的文件:

MovieObject.java

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MovieObject {

    private String movieName;
    private String yearOfRelease;
    private String movieRating;

    public MovieObject(){

    }

    public MovieObject(String movieName, 
            String yearOfRelease, 
            String movieRating){

        this.movieName = movieName;
        this.yearOfRelease = yearOfRelease;
        this.movieRating = movieRating;
    }

    public String getMovieName(){
        return movieName;
    }


    public String getYearOfRelease(){
        return yearOfRelease;
    }

    public String getMovieRating(){
        return movieRating;
    }
}

ResourceHandler.java

import java.util.ArrayList;
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("/movies")
public class ResourceHandler {

    ListMovies listMovies = new ListMovies();

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public List<MovieObject> getListOfMovies(){
        MovieObject mv1 = new MovieObject("The Independence Day 1", "1996", "7");
        MovieObject mv2 = new MovieObject("The Independence Day 2", "2016", "4");

        List<MovieObject> listOfMovies = new ArrayList<MovieObject>();

        listOfMovies.add(mv1);
        listOfMovies.add(mv2);

        return listOfMovies;
    }

}

的index.jsp

<html>
<body>
    <h2>Jersey RESTful Web Application!</h2>
    <p><a href="webapi/myresource">Jersey resource</a>
    <p>Visit <a href="http://jersey.java.net">Project Jersey website</a>
    for more information on Jersey!

    <p> Get list of movies <a href="webapi/movies"> here</a>
</body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>org.auro.self.movielib</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

以下输出是我在Tomcat 8.0上运行时获得的输出

enter image description here

正如您所看到的,我只获取了类public成员变量(本例中为7和4),而不是private成员变量。

但是在上面的教程中,请求成功返回了所有private个变量。我哪里错了?

1 个答案:

答案 0 :(得分:0)

虽然您的代码示例不一致,但

new MovieObject("The Independence Day 1", "1996", "7");

使用4个参数,而构造函数只接受3个,你应该添加

@XmlRootElement
public class MovieObject {

@XmlAttribute
private String movieName;
@XmlAttribute
private String yearOfRelease;
@XmlAttribute
private String movieRating;

或者

@XmlRootElement
public class MovieObject {

@XmlElement
private String movieName;
@XmlElement
private String yearOfRelease;
@XmlElement
private String movieRating;

这应该可以解决你的问题