Spring 3.0 RESTful 控制器在重定向时失败 [英] Spring 3.0 RESTful Controller Fails on Redirect

查看:44
本文介绍了Spring 3.0 RESTful 控制器在重定向时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为具有 XML 表示的 Todo 资源设置一个简单的 RESTful 控制器.一切都很好 - 直到我尝试重定向.例如,当我 POST 一个新的 Todo 并尝试重定向到它的新 URL(例如 /todos/5,我收到以下错误:

I am setting up a simple RESTful controller for a Todo resource with an XML representation. It all works great - until I try to redirect. For example, when I POST a new Todo and attempt to redirect to its new URL (for example /todos/5, I get the following error:

Error 500 Unable to locate object to be marshalled in model: {}

我确实知道 POST 有效,因为我可以手动转到新 URL (/todos/5) 并查看新创建的资源.只有在尝试重定向时才会失败.我知道在我的例子中我可以只返回新创建的 Todo 对象,但我还有其他情况下重定向是有意义的.该错误看起来像是封送处理问题,但就像我说的那样,它仅在我向 RESTful 方法添加重定向时才会出现,如果手动点击我重定向到的 URL,则不会发生.

I do know the POST worked because I can manually go to the new URL (/todos/5) and see the newly created resource. Its only when trying to redirect that I get the failure. I know in my example I could just return the newly created Todo object, but I have other cases where a redirect makes sense. The error looks like a marshaling problem, but like I said, it only rears itself when I add redirects to my RESTful methods, and does not occur if manually hitting the URL I am redirecting to.

代码片段:

@Controller
@RequestMapping("/todos")
public class TodoController {

    @RequestMapping(value="/{id}", method=GET)
    public Todo getTodo(@PathVariable long id) {
        return todoRepository.findById(id);
    }

    @RequestMapping(method=POST)
    public String newTodo(@RequestBody Todo todo) {
        todoRepository.save(todo); // generates and sets the ID on the todo object
        return "redirect:/todos/" + todo.getId();
    }

    ... more methods ...

    public void setTodoRepository(TodoRepository todoRepository) {
        this.todoRepository = todoRepository;
    }

    private TodoRepository todoRepository;
}

你能发现我遗漏了什么吗?我怀疑它可能与返回重定向字符串有关 - 也许它实际上不是触发重定向,而是实际传递给我的视图解析器使用的 XML 封送处理视图(未显示 - 但所有在线示例的典型),而 JAXB(配置的 OXM 工具)不知道如何处理它.只是猜测...

Can you spot what I am missing? I am suspecting it may have something to do with returning a redirect string - perhaps instead of it triggering a redirect it is actually being passed to the XML marshaling view used by my view resolver (not shown - but typical of all the online examples), and JAXB (the configured OXM tool) doesn't know what to do with it. Just a guess...

提前致谢.

推荐答案

发生这种情况是因为 redirect: 前缀由 InternalResourceViewResolver 处理(实际上,由 UrlBasedViewResolver).因此,如果您没有 InternalResourceViewResolver 或者您的请求在视图解析过程中没有进入它,则不会处理重定向.

This happend because redirect: prefix is handled by InternalResourceViewResolver (actually, by UrlBasedViewResolver). So, if you don't have InternalResourceViewResolver or your request doesn't get into it during view resolution process, redirect is not handled.

要解决这个问题,您可以从控制器方法返回一个 RedirectView,或者添加一个自定义视图解析器来处理重定向:

To solve it, you can either return a RedirectView from your controller method, or add a custom view resolver for handling redirects:

public class RedirectViewResolver implements ViewResolver, Ordered {
    private int order = Integer.MIN_VALUE;

    public View resolveViewName(String viewName, Locale arg1) throws Exception {
        if (viewName.startsWith(UrlBasedViewResolver.REDIRECT_URL_PREFIX)) {
            String redirectUrl = viewName.substring(UrlBasedViewResolver.REDIRECT_URL_PREFIX.length());
            return new RedirectView(redirectUrl, true);
        }
        return null;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }
}

这篇关于Spring 3.0 RESTful 控制器在重定向时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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