- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我们想创建一个返回特定对象列表的“WebService”。我们想通过 apache http 客户端库从另一个 java 程序调用这个 web 服务。
此时,如果我们从 Firefox 调用 web 服务,则会出现 406 错误页面。
我们是否必须使用 JSON 或 XML 来传输列表?如何做到这一点,以及如何使用 apache http 客户端获取列表?
谢谢。
[编辑]
唯一可行的是创建一些带有 JAXB 注释的实体,以便序列化为 XML。
@XmlRootElement(name = "person")
public class Person {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
@XmlRootElement(name = "persons")
public class PersonList {
@XmlElement(required = true)
public List<Person> persons;
public List<Person> getData() {
return persons;
}
public void setData(List<Person> persons) {
this.persons = persons;
}
}
@RequestMapping(value = "/hello.html", method = RequestMethod.GET, produces = "application/xml")
@ResponseBody
public ResponseEntity<PersonList> hello() {
PersonList test = new PersonList();
List<Person> rep = new ArrayList<Person>();
Person person1 = new Person();
person1.setId("1");
Person person2 = new Person();
person2.setId("2");
rep.add(person1);
rep.add(person2);
test.setData(rep);
// return test;
HttpHeaders responseHeaders = new HttpHeaders();
List<MediaType> medias = new ArrayList<MediaType>();
medias.add(MediaType.ALL);
responseHeaders.setAccept(medias);
return new ResponseEntity<PersonList>(test, responseHeaders, HttpStatus.OK);
}
我尝试使用 produces 并直接返回对象,但仍然是 406 错误。XML + ResponseEntity 有效。
这很奇怪,因为我看到一个非常简单的例子,其中对象被转换为 json 并出现在网络浏览器中。
所以,现在我必须了解如何获取响应并将 XML 转换为实体...
最佳答案
是的,当您的 Controller 方法用@ResponseBody
注释时,Spring 会将返回的数据转换为JSON。 .
关于java - Spring MVC @ResponseBody 返回一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20515607/
有什么区别吗 @ResponseBody public Object method(etc..) { etc.. } 和 public @ResponseBody Object method(
我有多个返回 JSON 对象的 REST 端点。对于大多数区域设置,响应都是正确的(所有符号都正确显示在响应中,并且内容类型为 application/json)。 如果我提供值为“en_NL”的 A
我有一个@restController类型 Controller ,当我访问请求的资源时,我得到一个json作为响应,问题是所有属性都出现在我看来,并且它不尊重我生成的构造函数。 但是,该类的所有属性
当我使用下面来获取用户对象时,它工作得很好。 @GetMapping("/findOne") @ResponseBody public Optional findOne (Long id) {
我有一个用于普通旧 xml Web 服务的 Spring MVC Controller ,方法如下: @RequestMapping( value = "/trade/{tradeId
我有一些东西可以通过 spring security 进行 ajax 登录: @RequestMapping(value="/entry.html", method=RequestMethod.GET
我有一个要求,我想处理异常的返回方式。根据 Controller 方法的类型,如果使用 @ResponseBody 注释,则应返回一个 json 字符串。此外,如果它是一个 String 返回方法,则
@ResponseBody 转化成json后与实体类字段名不一致 实体类A字段名由B改成C后,Controller 中返回的List中字段名仍然是C 经过@ResponseBody返回到前台后又变成了
我正在看一段代码,其中我假设spring决定在幕后使用Jackson为@RestController自动将对象转换为json @RestController @RequestMapping("/ap
它可能很简单,但它对我理解非常有帮助...... 我在我的 restcontroller 中使用 @ResponseBody 将 String 值返回给浏览器。浏览器成功接收响应字符串。 即: @Re
使用 @ResponseBody 时有没有办法将空值映射到空字符串的注释? 最佳答案 你将不得不编写一个自定义的 Jackson Serializer - 一个很好的例子在这里 - http://wi
我想在 spring mvc 方法中重定向到另一个 .jsp。我不想使用 javascripts 方法,例如:window.location.replace(url)。 我的方法: @RequestM
我有一个 RestController,它将 Film 对象列表发送回用户: @RestController @RequestMapping("/view") public class FilmRes
在 Spring Boot 中,我有最简单的 Controller ,返回我的 View 名称: @Controller public class HelloController { @Req
我只想将我的用户对象作为 JSON 返回,以供客户端的 ajax 调用使用。 这在某一点上是有效的,经过一些更新(即,将应用程序更改为部署到/在 Jetty 中),现在就不行了。 我没有从代码中抛出异
我基于 Spring MVC 构建我的 Web 应用程序,并在尝试在处理 ajax 请求的方法中添加 cookie 时遇到问题。 我意识到带有@ResponseBody 的方法(在我的示例中它返回一个
我希望我的字符串方法返回值。下面的代码返回一个 View (jsp)。在代码“line”中存储产品详细信息。我希望该方法返回该值。有人可以告诉我该怎么做吗? @RequestMapping(value
我正在尝试从网站获得响应。我正在使用 HttpURLConnection 类。 这是我的代码: BufferedReader in = null; in = new Buffer
我有以下 Spring @RestController 方法 @RequestMapping(value = "/getPeople", method = RequestMethod.GET) pub
如何创建用于模拟以下服务的 ResponseBody 对象? public interface SBRestfulApi { @FormUrlEncoded @POST("authen
我是一名优秀的程序员,十分优秀!