Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

java - Spring 3.2 @ResponseBody not working with a Model return value

The following mapping was working with Spring 3.1, but is not working with Spring 3.2. I get a 404 error with an explanation that the table.jsp file is missing. Instead, the "model" should be serialized to json.

    @RequestMapping(value = {"/table"}, method = RequestMethod.GET, produces="application/json")
    public @ResponseBody Model table(Model model, @RequestParam(defaultValue = "1") Integer pg) {
        fillListModel(model, pg);
        return model;
    }

Is there a way to fix this without any impact to the existing code?

The following code works fine:

    @RequestMapping(value = {"/table"}, method = RequestMethod.GET, produces="application/json")
    public @ResponseBody Model table(Model model, @RequestParam(defaultValue = "1") Integer pg) {
        return new User();
    }

So it looks like Spring cannot recognize that the model is returned with a purpose to be turned into json instead to be rendered in a view.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a consequence of how Spring 3.2+ (I don't remember how 3.1 does it) handles @RequestMapping methods' return values. Spring uses instances of type HandlerMethodReturnValueHandler to resolve how the value returned should be handled. Go through the javadoc too see the different types.

When you configure your MVC environment, if you use the default @EnableWebMVC or <mvc:annotation-driven>, Spring registers these instances in a specific order. This happens in the RequestMappingHandlerAdapter#getDefaultReturnValueHandlers() method as shown below

private List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers() {
    List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>();

    // Single-purpose return value types
    handlers.add(new ModelAndViewMethodReturnValueHandler());
    handlers.add(new ModelMethodProcessor());
    handlers.add(new ViewMethodReturnValueHandler());
    handlers.add(new HttpEntityMethodProcessor(getMessageConverters(), this.contentNegotiationManager));
    handlers.add(new CallableMethodReturnValueHandler());
    handlers.add(new DeferredResultMethodReturnValueHandler());
    handlers.add(new AsyncTaskMethodReturnValueHandler(this.beanFactory));

    // Annotation-based return value types
    handlers.add(new ModelAttributeMethodProcessor(false));
    handlers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(), this.contentNegotiationManager));

    // Multi-purpose return value types
    handlers.add(new ViewNameMethodReturnValueHandler());
    handlers.add(new MapMethodProcessor());

    // Custom return value types
    if (getCustomReturnValueHandlers() != null) {
        handlers.addAll(getCustomReturnValueHandlers());
    }

    // Catch-all
    if (!CollectionUtils.isEmpty(getModelAndViewResolvers())) {
        handlers.add(new ModelAndViewResolverMethodReturnValueHandler(getModelAndViewResolvers()));
    }
    else {
        handlers.add(new ModelAttributeMethodProcessor(true));
    }

    return handlers;
}

When your method returns a value, Spring iterates through these handlers, calling their supportsReturnType() method and picking the first it finds that returns true.

In this case, the ModelMethodProcessor which handles Model return values has higher priority (is registered before) the RequestResponseBodyMethodProcessor which handles @ResponseBody.

As such, you can't return a Model and have it be converted to JSON through the @ResponseBody. In my opinion, you shouldn't do this at all. The Model is accessible to most parts of the DispatcherServlet stack and therefore many modules can add/remove attributes which you may not want in the final JSON.

Just use a DTO like you have in your second example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...