基于xml的配置的Spring 4基于MVC的项目希望使用Jackson绑定为某些控制器映射返回Json(并且它将转至Google App Engine)。 做了很多调查,许多不同的解决方案,但他们每个人都有不同的方法,然后我们有。还没有工作解决方案。Spring 4 MVC |使用Jackson绑定以基于xml的配置返回Json
的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>
春应用程序的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
@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;
}
}
类名是一个简单的POJO,带getters/setters,实现Serializable。
pom.xml得到了所有杰克逊依赖项:jackson-databind,jackson-core,jackson-annotations,版本2.6.0。用弹簧:4.2.0.RELEASE
的错误我试图获取URL时收到: org.springframework.web.HttpMediaTypeNotAcceptableException:找不到接受表示
那么,你会做什么,你期望会发生什么,取而代之的是什么? –
@JBNizet希望接收对象的JSON表示(获得其他URL映射,它返回VIEWs,它们工作)。将所有其他相关信息添加到q。请告诉我是否有遗漏。 – Stas
您的Accept请求标头的值是多少? –