Spring 4.2.1 RestController 尝试 return 模板而不是 return JSON

Spring 4.2.1 RestController tried to return template instead return JSON

我尝试创建一个 Spring Rest 控制器,based on this example 我创建了一个这样的控制器。

DeveloperRestController.java

@RestController
public class DeveloperRestController {
    
    @RequestMapping("/developer/list")
    public Developer index() {
        Developer developer = new Developer("Developername", "developer@yahoo.com");
        return developer;
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> <!--Here we specify about the DispatcherServlet class in the Web Deployment Descriptor-->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
</web-app>

调度员-servlet.xml

<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"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <context:component-scan base-package="com.developerdata.controller" />
    <context:annotation-config />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

但它显示 404 页面未找到,似乎 spring 试图加载模板...

结果:

Error 404 /WEB-INF/jsp/developer/list.jsp

我该怎么办?

您似乎遇到了配置问题。您所基于的示例是基于 spring 引导的。所以它会为你处理配置。为了使您的工作正常,您需要将 jackson 添加到类路径中。如果您使用的是 Maven,则:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.2</version>
</dependency>

然后您需要更改 spring 配置以包括:

<mvc:annotation-driven />

来自 spring 文档:

The above registers a RequestMappingHandlerMapping, a RequestMappingHandlerAdapter, and an ExceptionHandlerExceptionResolver (among others) in support of processing requests with annotated controller methods using annotations such as @RequestMapping, @ExceptionHandler, and others.

如果 jackson 2 在您的类路径中,这也会启用 MappingJackson2HttpMessageConverter。

参考文献:

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-config-enable

https://spring.io/guides/gs/rest-service/

http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/(根据你的问题,这个似乎更适合你入门)

使用@ResponseBody,Spring 将处理您需要的JSON。杰克逊 图书馆也是必需的。

@RestController
public class DeveloperRestController {
    @RequestMapping("/developer/list")
    public @ResponseBody Developer index() {
        Developer developer = new Developer("Developername",  "developer@yahoo.com");
        return developer;
    }
}