Spring MVC @ResponseBody返回一个List [英] Spring MVC @ResponseBody return a List

查看:212
本文介绍了Spring MVC @ResponseBody返回一个List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想创建一个WebService,它返回特定对象的列表。
我们想通过apache http客户端库从另一个java程序中调用这个web服务。

We would like to create a "WebService" which return a list of specific objects. And we would like to call this webservice from another java program by apache http clients library.

此时,如果我们从Firefox调用web服务,出现406错误页面。

At this moment, if we call the webservice from Firefox, an 406 error page appears.

我们是否必须使用JSON或XML来传输列表?
怎么做,以及如何使用apache http客户端获取列表?

Do we have to use JSON or XML to transfert the list ? How to do that, and how to get the list with apache http clients ?

谢谢。

唯一有效的方法是创建一些带有JAXB注释的实体以便序列化到XML。

The only thing which is working is to create some entities with JAXB annotations in order to serialize into 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);
}

我尝试使用产品并直接返回对象,但仍然是错误406.
XML + ResponseEntity可以工作。

I tried with produces and to return directly the object but still error 406. XML + ResponseEntity works.

这是非常奇怪的,因为我看到一个非常简单的例子,其中对象被转换为json并出现在Web浏览器中。

It is very strange cause i saw an exemple very simple where the object is converted into json and appears into web browser.

所以,现在我必须了解如何获得响应并将XML转换为实体...

So, now i have to understand how to get the response and to convert XML into entities...

推荐答案

在2天内,我尝试了很多方法:
- responseEntity
- httpheaders
- XML
等......

During 2 days, i tried many ways : - responseEntity - httpheaders - XML etc...

对于JSON(默认行为),项目需要一个包含所有Spring库的库。
这里是要在Maven项目中声明的库。

For a JSON (default behavior), the project need a library with all Spring library. Here the library to declare in Maven project.

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.7.1</version>
</dependency> 

没有这个库,我有一个错误(406)。

Without this library, i have an Error (406).

无论如何,谢谢你的所有答案&建议。

Thank you anyway for all your answers & advices.

这篇关于Spring MVC @ResponseBody返回一个List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆