应用程序中解析视图

应用程序中解析视图

本文介绍了不要在 RESTful 应用程序中解析视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building a web application with RESTful web services using Spring MVC 3. The web services will be used by applications, so should never really resolve any requests to a view. Is there any way to specify in the servlet context that no requests should resolve to any view?

目前,我有:

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

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

which I know tries to resolve any request to a correspondingly named view in the jsp folder. However, if I remove this, the application just tries to use a default view resolver.

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

The reason I am concerned about this is that my application logs are going to be full of the following messages (even though it works fine):

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'

我想这是两种邪恶中更好的一种.我不想关闭记录警告级别.

which I guess is the better of the two evils. I don't want to turn off logging WARN level.

推荐答案

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

Try with @ResponseStatus. This code returns 204 with no content and view resolving skipped:

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

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

If you want to return raw data and simply serialize it to JSON or XML, use @ResponseBody:

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

这篇关于不要在 RESTful 应用程序中解析视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 02:25