SpringFramework ResponseBody返回具有空值的对象

时间:2017-11-29 15:24:03

标签: java json spring serialization

我有一个RestController,它向用户发送一个Film对象列表:

@RestController
@RequestMapping("/view")
public class FilmRestController {

    @RequestMapping(value = "/", method = RequestMethod.GET,  produces = "application/json")
    public @ResponseBody List<Film> getCurrentFilms(){
        FilmInfo filmInfo = new FilmInfo();
        return filmInfo.getCurrentFilms();
    }
}

每个Film对象包含两个Strings(title,director)和List,它们是actor。这么简单的pojo。

当我在Postman中提出请求时,我收到以下回复:

[
{
    "title": null,
    "director": null,
    "actors": null
},
{
    "title": null,
    "director": null,
    "actors": null
},
{
    "title": null,
    "director": null,
    "actors": null
},
{
    "title": null,
    "director": null,
    "actors": null
}
]

我认为这是一个小故障,所以我在以下后面编写了以下方法:

@RequestMapping(value = "/getOneFilm", method = RequestMethod.GET,  produces = "application/json")
public @ResponseBody Film getOneFilm(){
    Film film = new Film("The Shawshank Redemption", "Frank Darabont", Arrays.asList("Tim Robbins","Morgan Freeman","Bob Gunton"));
    return film;
}

我的回答肯定是:

{
"title": null,
"director": null,
"actors": null
}

这里要求的是我的电影课:

package entities;

import java.util.List;

public class Film {

private String title;
private String director;
private List<String> actors;

public Film(String title, String director, List<String> actors) {
    title = this.title;
    director = this.director;
    actors = this.actors;
}


public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getDirector() {
    return director;
}
public void setDirector(String director) {
    this.director = director;
}
public List<String> getActors() {
    return actors;
}
public void setActors(List<String> actors) {
    this.actors = actors;
}
}

有没有理由ResponseBody无法将我的对象转换为在Response中发回?我应该改变策略并手动执行我的JSON序列化程序吗?我

0 个答案:

没有答案