2012-04-27 72 views
1

大家好,我現在嘗試七天來從Web上的存儲庫中檢索數據。特別是我嘗試檢索從這裏作者的列表(其中有網址:HTTP://www.../base/author):使用Spring MVC從存儲庫中檢索XML元數據

<authors xmlns="http:www.../base" xmlns:atom="http://www.w3.org/2005/atom"> 
    <author> 
     <atom:link rel="author" type="application/xml" href="http://www.../base/author/1"/> 
    </author> 
    <author> 
     <atom:link rel="author" type="application/xml" href="http://www.../base/author/2"/> 
    </author> 
    <author> 
     <atom:link rel="author" type="application/xml" href="http://www.../base/author/3"/>   
    </author> 
</authors> 

而且每個作者seperately從這裏:

<author xmlns="http://http:www.../base" xmlns:atom="http://www.w3.org/2005/atom"> 
    <name>S. Crocker</name> 
    <address>None</address> 
    <affiliation></affiliation> 
    <email>None</email> 
</author> 

我的模型類:

import javax.xml.bind.annotation.XmlRootElement; 

    @XmlRootElement(name="author") 
    public class Author { 

private Long id; 
private String name; 
private String address; 
private String affiliation; 
    private String email; 

public Long getId() { 
    return id; 
} 
public void setId(Long id) { 
    this.id = id; 
} 
public String getName() { 
    return name; 
} 
public void setFirstName(String name) { 
    this.name = name; 
} 
public String getAddress() { 
    return address; 
} 
public void setAddress(String address) { 
    this.address = address; 
} 
public String getAffiliation() { 
    return affiliation; 
} 
public void setAffiliation(String affiliation) { 
    this.affiliation = affiliation; 
} 
public String getEmail() { 
    return email; 
} 
public void setEmail(String email) { 
    this.email = email; 
} 

} 

和:

import java.util.List; 

    import javax.xml.bind.annotation.XmlRootElement; 

    @XmlRootElement(name="authors") 
    public class AuthorList { 

private List<Author> data; 

public List<Author> getData() { 
    return data; 
} 

public void setData(List<Author> data) { 
    this.data = data; 
} 

}

控制器:

public class Controller { 

protected static Logger logger = Logger.getLogger("controller"); 

private RestTemplate restTemplate = new RestTemplate(); 

/** 
* Retrieves all records from the REST provider 
* and displays the records in a JSP page 
*/ 
@RequestMapping(value = "/authors", method = RequestMethod.GET)//getall 
public String getAll(Model model) { 


    // Prepare acceptable media type 
    List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>(); 
    acceptableMediaTypes.add(MediaType.APPLICATION_XML); 

    // Prepare header 
    HttpHeaders headers = new HttpHeaders(); 
    headers.setAccept(acceptableMediaTypes); 
    HttpEntity<Author> entity = new HttpEntity<Author>(headers); 

    // Send the request as GET 
    try { 
     ResponseEntity<AuthorList> result = restTemplate.exchange("href="http://www.../base/author/", 
         HttpMethod.GET, entity, AuthorList.class); 
     // Add to model 
     model.addAttribute("authors", result.getBody().getData()); 

    } catch (Exception e) { 
    } 

    // This will resolve to /WEB-INF/jsp/personspage.jsp 
    return "personspage"; 
} 

/** 
* Retrieves a single record from the REST provider 
* and displays the result in a JSP page 
*/ 
@RequestMapping(value = "/author", method = RequestMethod.GET) 
public String getPerson(@RequestParam("id") Long id, Model model) { 


    // Prepare acceptable media type 
    List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>(); 
    acceptableMediaTypes.add(MediaType.APPLICATION_XML); 

    // Prepare header 
    HttpHeaders headers = new HttpHeaders(); 
    headers.setAccept(acceptableMediaTypes); 
    HttpEntity<Author> entity = new HttpEntity<Author>(headers); 

    // Send the request as GET 
    try { 
     ResponseEntity<Author> result = restTemplate.exchange("href="http://www.../base/author/{id}", 
          HttpMethod.GET, entity, Author.class, id); 
     // Add to model 
     model.addAttribute("author", result.getBody()); 

    } catch (Exception e) { 
    } 

    // This will resolve to /WEB-INF/jsp/getpage.jsp 
    return "getpage"; 
} 




} 

最後的2點意見之一,GETPAGE:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
    <%@page contentType="text/html" pageEncoding="UTF-8"%> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 

    <html> 
    <head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
<h1>Get Author</h1> 

<c:if test="${empty author}"> 
No records found! 
</c:if> 

<c:if test="${!empty author}"> 
<table style="border: 1px solid #333"> 
    <tr> 
    <td style="width: 100px">Id</td> 
    <td>${author.id}</td> 
    </tr> 

    <tr> 
    <td>Name</td> 
    <td>${author.name}</td> 
    </tr> 

    <tr> 
    <td>Address</td> 
    <td>${author.address}</td> 
    </tr> 

    <tr> 
    <td>Affiliation</td> 
    <td>${author.affiliation}</td> 
    </tr> 

      <tr> 
    <td>Email</td> 
    <td>${author.email}</td> 
    </tr> 
</table> 
</c:if> 

現在,wheni只要運行該程序我坐查看其中說:

沒有找到記錄!

當我給出以下網址: http://localhost:8080/Client_for_rest/author?id=1

我應該從reposiory接收數據,姓名等電子郵件,但 我這一翻譯收到此: 所請求的資源()不可用。 我不明白是什麼問題,URL的 之間的映射接收到的數據? 任何命題?

回答

2

關於您的第一個問題,它看起來不像JAXB註釋是正確的,作者xml元素沒有映射到您的AuthorList類,作者內部元素作者沒有映射到Author類,並且命名空間不是被佔。

我覺得有些快速解決JAXB註釋可能是: 您可以定義幾個類: AtomLink: @XmlAccessorType(XmlAccessType.FIELD)

public class Atomlink { 
    @XmlAttribute 
    private String rel; 
    @XmlAttribute 
    private String type; 
    @XmlAttribute 
    private String href; 
    ....... 

的AuthorMeta,以代表作者作者內標籤:

@XmlAccessorType(XmlAccessType.FIELD) 
public class AuthorMeta { 

    @XmlElement(name="link", namespace="http://www.w3.org/2005/atom") 
    private Atomlink link; 

    public Atomlink getLink() { 
     return link; 
    } 

    public void setLink(Atomlink link) { 
     this.link = link; 
    } 
} 

終於AuthorList像你已經有了,但命名空間:

@XmlRootElement(name = "authors", namespace="http:www.../base") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class AuthorList { 

    private List<AuthorMeta> author; 

    public List<AuthorMeta> getAuthor() { 
     return author; 
    } 

    public void setAuthor(List<AuthorMeta> data) { 
     this.author = data; 
    } 
} 

這應該照顧你不能幹淨地綁定你的第一個問題。

你必須定義筆者沿着相同的路線的結果,結合:

@XmlRootElement(name = "author", namespace="http://http:www.../base") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Author { 
    private String name; 
    private String address; 
    private String affiliation; 
    private String email; 

現在關於你的資源不found..the映射好看的第二個問題,它可能會更好設置日誌級別以跟蹤並查看發生了什麼。

相關問題