【问题标题】:Do not resolve view in RESTful Application不在 RESTful 应用程序中解析视图
【发布时间】:2011-12-06 11:58:44
【问题描述】:

我正在使用 Spring MVC 3 构建一个带有 RESTful Web 服务的 Web 应用程序。Web 服务将被应用程序使用,因此永远不应该真正解决对视图的任何请求。有没有办法在 servlet 上下文中指定任何请求都不应解析为任何视图?

目前,我有:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

我知道它会尝试将任何请求解析为jsp 文件夹中相应命名的视图。但是,如果我删除它,应用程序只会尝试使用默认视图解析器。

我担心这一点的原因是我的应用程序日志将充满以下消息(即使它工作正常):

SEVERE: Servlet.service() for servlet [DispatcherServlet] in context with path [/vouchd] threw exception [Circular view path [signup]: would dispatch back to the current handler URL [/vouchd/signup] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause
javax.servlet.ServletException: Circular view path [signup]: would dispatch back to the current handler URL [/vouchd/signup] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

或使用InternalViewResolver:

WARN [http-bio-8080-exec-4] (DispatcherServlet.java:1057) - No mapping found for HTTP request with URI [/app/WEB-INF/jsp/call.jsp] in DispatcherServlet with name 'DispatcherServlet'

我猜这是两个弊端中更好的一个。我不想关闭日志记录 WARN 级别。

【问题讨论】:

    标签: rest spring-mvc


    【解决方案1】:

    试试@ResponseStatus。此代码返回 204,没有内容并跳过视图解析:

    @ResponseStatus(NO_CONTENT)
    void noView() {
      //...
    }
    

    如果您想返回原始数据并将其简单地序列化为 JSON 或 XML,请使用@ResponseBody:

    @ResponseBody
    MyPojo noView() {
      return new MyPojo();
    }
    

    【讨论】:

    • 我仍在向请求应用程序返回 JSON 数据。这不会否定这一点吗?
    • @NicoHuysamen:抱歉,我误解了任何请求都不应针对任何视图,请参阅我的更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多