Spring 4 MVC |使用Jackson绑定以基于xml的配置返回Json

时间:2015-08-27 16:45:31

标签: json spring spring-mvc jackson spring-4

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 
@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:找不到可接受的代表

0 个答案:

没有答案