【问题标题】:Spring 4.2.1 RestController tried to return template instead return JSONSpring 4.2.1 RestController 尝试返回模板而不是返回 JSON
【发布时间】:2015-10-02 04:11:03
【问题描述】:

我尝试创建一个 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试图加载模板...

结果:

错误 404 /WEB-INF/jsp/developer/list.jsp

我该怎么办?

【问题讨论】:

  • 添加&lt;mvc:annotation-driven /&gt;
  • 类路径上有杰克逊吗?
  • 类路径上有杰克逊吗?你需要 json jackson view resolver、jackson mapper 和添加 mvc:annotation-driven 3 样东西。

标签: spring spring-mvc spring-restcontroller spring-rest


【解决方案1】:

您似乎遇到了配置问题。您所基于的示例是基于弹簧靴的。所以它会为你处理配置。为了让你的工作,你需要将杰克逊添加到类路径中。如果您使用的是 maven,那么:

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

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

<mvc:annotation-driven />

来自 spring 文档:

上面注册了一个RequestMappingHandlerMapping,一个 RequestMappingHandlerAdapter 和一个 ExceptionHandlerExceptionResolver (除其他外)支持处理带有注释的请求 使用 @RequestMapping 等注解的控制器方法, @ExceptionHandler 等。

如果 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/(根据您的问题,这个似乎更适合您入门)

【讨论】:

  • 也许 OP 也应该缩小他的站点映射。通过sitemesh路由的休息响应?
  • 有效点。还可以考虑从 sitemesh 过滤器中排除其余资源的 url。
  • 是的,也许我有配置问题,'现在它显示 No converter found for return value of type'
  • 有没有说是什么类型?
  • 是的,它是一个 java pojo 对象,我将“jackson-mapper-asl”添加到依赖路径中,现在它已解决...谢谢@KevinBayes
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 2017-10-07
    • 2015-12-18
    相关资源
    最近更新 更多