【问题标题】:Spring 4.1.4 and Jackson 2.5, annotation driven setup not converting REST responses to JSONSpring 4.1.4 和 Jackson 2.5,注释驱动设置不将 REST 响应转换为 JSON
【发布时间】:2015-01-13 06:58:44
【问题描述】:

在做了 5 年其他事情之后,我回到了 Spring。我有一个初始项目,旨在提供返回 JSON 的 HTTP REST 服务。

我的问题是我无法获得将响应转换为 JSON 的服务。相反,我得到这样的错误:

javax.servlet.ServletException: Circular view path [hello]: would dispatch back to the current handler URL [/hello] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:205) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:145) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE]
... 

我的 web.xml 看起来像这样:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>au.com.abc.service</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>fxServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>au.com.abc.controller</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>fxServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

我的 Controller 类看起来像这样:

@RestController
public class FXRESTController {

@RequestMapping(value = "/hello")
public Map<String,Object> rootContextHandler() {
    Map<String,Object> data = new HashMap<>();
    data.put("X", "abc");
    return data;
}
}

真的不能再简单了。我一直在传递请求标头 Accept='application/json',但它仍然无法正常工作。我过去曾有过这种类型的工作,但我不再拥有该代码。我也可以在日志中看到这一点:

... Invoking request handler method: public java.util.Map au.com.abc.controller.FXRESTController.rootContextHandler()
... Service responding
... Invoking afterPropertiesSet() on bean with name 'hello'
... Rendering view [org.springframework.web.servlet.view.JstlView: name 'hello'; URL [hello]] in DispatcherServlet with name 'fxServlet'
... Added model object 'X' of type [java.lang.String] to request in view with name 'hello'
... Error rendering view [org.springframework.web.servlet.view.JstlView: name 'hello'; URL [hello]] in DispatcherServlet with name 'fxServlet'

这表明它正在尝试呈现 JSTL 视图。为什么 - 考虑到我要求的是 JSON,我不知道。

任何想法我做错了什么?

我已经阅读了大量的博客,到目前为止,我看不出他们所做的与我所做的有任何区别。

哦,这是我的 gradle 解析依赖项:

【问题讨论】:

  • 你的控制器看起来像那个还是你的实际控制器...
  • 这是来自控制器的代码的剪切粘贴减去导入等。
  • 您似乎只有一个控制器,它对启用 JSON 没有任何作用。您必须有一个 @Configuration 带注释的类,该类也带有 @EnableWebMvc 注释,才能启用自动 JSON 转换。您正在使用非常基本的 DispatcherServlet 默认值运行。
  • 啊,让我试试,我没听懂:-(
  • 你的传奇!那行得通。非常感谢。

标签: java json spring rest spring-mvc


【解决方案1】:

您只有一个控制器,它对启用 JSON 没有任何作用。

您必须有一个 @Configuration 注释类,该类也使用 @EnableWebMvc 注释才能启用自动 JSON 转换。另请参阅参考指南的this 部分。

@Configuration
@EnableWebMvc
public class WebConfiguration {}

您使用的是非常基本的DispatcherServlet defaults

【讨论】:

    【解决方案2】:

    使用@ResponseBody。 也不要将所有请求都指向 spring

    <servlet-mapping>
        <servlet-name>fxServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    

    总是最好添加这样的层:

    <servlet-mapping>
        <servlet-name>fxServlet</servlet-name>
        <url-pattern>/webapp/*</url-pattern>
    </servlet-mapping>
    

    对于给定的项目,只有以下请求才会被定向到 spring

    http://localhost:8080/Proj/app/hello
    

    其他请求可以不同的处理..因此您可以以不同的方式处理请求,而不依赖于 spring..like

    http://localhost:8080/Proj/servlethandler
    

    上面的请求不会去spring,可以被servlet等拦截。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      相关资源
      最近更新 更多