0

I tried to create a Spring Rest Controller, based on this example i create a controller like this.

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>

dispatcher-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>

But it shows 404 page not found, seems that spring tried to load a template...

Result:

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

what should i do?

4

2 回答 2

3

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

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

然后你需要改变你的弹簧配置以包括:

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

于 2015-10-02T07:38:44.907 回答
1

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

@RestController
public class DeveloperRestController {
    @RequestMapping("/developer/list")
    public @ResponseBody Developer index() {
        Developer developer = new Developer("Developername",  "developer@yahoo.com");
        return developer;
    }
}
于 2015-10-02T08:25:17.403 回答