javax.servlet.ServletException:无法在模型中找到要编组的对象

时间:2013-03-07 14:13:05

标签: spring spring-mvc jaxb2 oxm

我环顾四周但是无法弄清楚我在试图获取结果的xml视图时错过了什么。

以下是我得到的例外情况:

javax.servlet.ServletException: Unable to locate object to be marshalled in model: {movies=[com.wickedlynotsmart.imdb.model.Movie@1450f1f, com.wickedlynotsmart.imdb.model.Movie@ac622a, com.wickedlynotsmart.imdb.model.Movie@160c21a, com.wickedlynotsmart.imdb.model.Movie@1677737, com.wickedlynotsmart.imdb.model.Movie@1c3dc66]}
    at org.springframework.web.servlet.view.xml.MarshallingView.renderMergedOutputModel(MarshallingView.java:100)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    ...
    ...

以下是处理请求时包含的文件:

servlet应用程序上下文文件

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.wickedlynotsmart.imdb.model.Movie</value>
        </list>
    </property>
</bean>

<bean id="movies" class="org.springframework.web.servlet.view.xml.MarshallingView">
    <constructor-arg ref="jaxbMarshaller" />
</bean>

域对象

@Entity
@XmlRootElement
public class Movie implements Serializable {
    public Movie() {}
    //interesting stuff
}

控制器

@RequestMapping("/movies")
public class MoviesController {
    private static final Log logger = LogFactory.getLog(MoviesController.class);

    @Autowired
    private MovieManagementService movieManagementService;

    @RequestMapping(method=RequestMethod.GET)
    public String findAllMovies(Model model) {
        List<Movie> movies = movieManagementService.getAllMovies();
        model.addAttribute("movies", movies);
        return "movies";
    }   
        //interesting stuff
}

有人可以帮助我解决我在这里可能缺少的事情吗?

感谢。

编辑:我基本上是在尝试查看BeanNameViewResolver,我已经在配置文件中配置了BeanNameViewResolver,如下所示:

<bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver">
    <property name="order" value="2" />
    </bean> 

1 个答案:

答案 0 :(得分:2)

以下更改让事情顺利进行:

Movie类的包装器,以保持JAXB的快乐

@XmlRootElement(name="movies")
public class MovieList {

    private List<Movie> movieList;

    public MovieList() {}
    public MovieList(List<Movie> movieList) {
        this.movieList = movieList;
    }

    @XmlElement(name="movie")
    public List<Movie> getMovieList() {
        return movieList;
    }
    public void setMovieList(List<Movie> movieList) {
        this.movieList = movieList;
    }

}

<强>控制器

@RequestMapping(method=RequestMethod.GET)
public String findAllMovies(Model model) throws MovieNotFoundException {
    List<Movie> movieList = movieManagementService.getAllMovies();
    MovieList movies = new MovieList(movieList);
    model.addAttribute("movies", movies);
    return "movies";
}

sevlet应用程序上下文

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.wickedlynotsmart.imdb.model.Movie</value>
            <value>com.wickedlynotsmart.imdb.model.MovieList</value>
        </list>
    </property>
</bean>