2016-08-21 56 views
-2

我正在嘗試使用JAX-RS學習REST風格的Web服務的實現。我正在關注this tutorial here on YouTube。在本教程中,您可以看到GET請求返回一個類的所有私有變量。但在我的情況下,它不。當我嘗試時,它會返回一個空白屏幕。然後,當我將某些成員更改爲public時,請求僅返回那些成員變量。GET請求到REST風格的Web服務不會返回私有變量

下面是我的文件:

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變量。我哪裏錯了?

+2

我在代碼中看不到任何公共成員變量? – Jens

回答

0

當你的代碼示例是不一致的,

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

使用4個paramenters,而構造函數只接受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; 

應解決你的問題