【发布时间】:2015-11-22 04:15:14
【问题描述】:
基于 Spring 4 MVC 的项目,具有基于 xml 的配置,想要使用 Jackson 绑定来为某些控制器映射返回 Json(它会发送到 Google App Engine)。 做了很多调查,许多不同的解决方案,但他们所做的,每一个,与我们所拥有的方法有点不同。尚无可行的解决方案。
web.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<web-app version="3.0" 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_3_0.xsd">
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config/application-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Spring application-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<context:component-scan base-package="com.wixpress.automation"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
我的示例 controller 非常苗条:
@Controller
@RequestMapping("/api/")
public class Controller {
@RequestMapping(value = "/name", method = RequestMethod.GET)
@ResponseBody
public Name nameexample(WebRequest request, Model model) {
final Name name = new Name("name", "class", "package");
return name;
}
}
类名是一个简单的带有getter/setter的POJO,实现了Serializable。
pom.xml 获得了所有 Jackson 依赖项:jackson-databind、jackson-core、jackson-annotations、2.6.0 版。使用的弹簧:4.2.0.RELEASE
尝试获取网址时收到的错误: org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示
【问题讨论】:
-
那么,您会做什么,您希望发生什么,以及会发生什么?
-
@JBNizet 期望接收对象的 JSON 表示(得到其他返回 VIEW 的 url 映射,它们工作)。将所有其他相关信息添加到 q。如有遗漏请告诉我。
-
Accept 请求标头的值是多少?
-
@JBNizet 接受:应用程序/json
标签: json spring spring-mvc jackson spring-4