2015-10-02 64 views
0

我試圖創建一個Spring Rest Controller,我創建了一個像這樣的控制器。Spring 4.2.1 RestController試圖返回模板,而不是返回JSON

DeveloperRestController.java

@RestController 
public class DeveloperRestController { 

    @RequestMapping("/developer/list") 
    public Developer index() { 
     Developer developer = new Developer("Developername", "[email protected]"); 
     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頁,似乎春天試圖加載一個模板...

結果:

錯誤404 /WEB-INF/jsp/developer/list.jsp

我應該怎麼辦?

+1

add''。 –

+1

你在classpath上有jackson嗎? –

+1

你在classpath上有jackson嗎?你需要json jackson視圖解析器,jackson mapper以及添加mvc:註解驅動的3件東西。 –

回答

3

好像你有配置問題。你基於你的樣本基於spring boot。所以它爲你處理配置。爲了讓你的工作,你需要將jackson添加到類路徑中。如果您在使用maven則:

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

然後,你需要改變你的Spring配置包括:

<mvc:annotation-driven /> 

從春天文檔:

以上寄存器中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/(這一次似乎更適合你,我們將根據您的問題開始)

+0

也許OP應該縮小他的sitemash映射。休息響應通過sitemesh路由? –

+0

有效的點。還要考慮從sitemesh過濾器中排除您的其餘資源的網址。 –

+0

是的,也許我有配置問題,'現在它顯示沒有找到類型返回值的轉換器' – zho

1

@ResponseBody隨着春季將處理JSON你需要。傑克遜 庫也是必需的。

@RestController 
public class DeveloperRestController { 
    @RequestMapping("/developer/list") 
    public @ResponseBody Developer index() { 
     Developer developer = new Developer("Developername", "[email protected]"); 
     return developer; 
    } 
}