springspring-mvcjaxb2oxm

javax.servlet.ServletException: Unable to locate object to be marshalled in model


I've looked around bit but can't figure out what am I missing trying to get xml view of result.

Following is the exception I am getting:

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

Following are the files included in handling the request:

servlet application context file

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

domain object

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

controller

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

Could someone help me out with what I might be missing here?

Thanks.

EDIT: I am basically trying to see BeanNameViewResolver in action for which I already have BeanNameViewResolver configured in the configuration file as following:

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

Solution

  • Following changes got things working:

    wrapper for Movie class to keep JAXB happy

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

    controller

    @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 application context

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