gpt4 book ai didi

java - 使用 Spring MVC 从存储库检索 XML 元数据

转载 作者:行者123 更新时间:2023-11-30 04:48:32 25 4
gpt4 key购买 nike

大家好,我现在尝试 7 天从网络存储库中检索数据。特别是我尝试从这里检索作者列表(其 URL:http://www.../base/author):

 <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>

每个作者都来自这里:

<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;
}

}

Controller :

 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";
}




}

最后是两个 View 之一,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>

现在,当我运行该程序时,我会看到如下内容:

没有找到记录!

当我给出以下 URL 时: http://localhost:8080/Client_for_rest/author?id=1

我应该从存储库接收数据,姓名电子邮件等但相反我收到了这个:请求的资源 () 不可用。我不明白 URL 之间的映射有什么问题收到的数据?有什么建议吗?

最佳答案

关于您的第一个问题,JAXB注释看起来不正确,authors xml元素没有映射到您的AuthorList类,author内的内部元素author没有映射到Author类,并且命名空间没有被映射已占。

我认为对 JAXB 注释的一些快速修复可能是:您可以定义几个类:原子链接:@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,但带有 namespace :

@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;
}
}

这应该可以解决您无法干净绑定(bind)的第一个问题。

您必须按照相同的方式定义作者结果的绑定(bind):

@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;

现在关于找不到资源的第二个问题..映射看起来不错,最好将日志级别设置为跟踪并查看发生了什么。

关于java - 使用 Spring MVC 从存储库检索 XML 元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10356312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com