在花了 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;
}
}
它真的不能再简单了。我一直在传递请求 header 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 View 。为什么 - 考虑到我要求 JSON,我不知道。
知道我做错了什么吗?
我已经阅读了大量的博客,到目前为止,我看不出他们所做的和我所做的有什么不同。
哦,这是我的 gradle 解决的依赖项:
最佳答案
您只有一个 Controller ,它不会为启用 JSON 做任何事情。
你必须有一个 @Configuration
注释的类,它也用 @EnableWebMvc
注释启用自动 JSON 转换。另见 this引用指南的一部分。
@Configuration
@EnableWebMvc
public class WebConfiguration {}
您正在使用 DispatcherServlet
defaults 运行这是非常基本的。
关于java - Spring 4.1.4 和 Jackson 2.5,注释驱动设置不将 REST 响应转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27916493/