Spring RESTful服务

时间:2014-07-24 00:30:41

标签: spring-mvc

我无法点击我创建的Spring休息服务。我是春天新手,一直在尝试按照教程在线查找示例。我无法确定我做错了什么。

Web.xml中

<web-app>
  <display-name>Friend List</display-name>

  <servlet>
       <servlet-name>friendlist</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
       <servlet-name>friendlist</servlet-name>
       <url-pattern>/friend-list/*</url-pattern>
   </servlet-mapping>

</web-app>

朋友一览servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="demo.model"/>
</beans>

HelloWorld.java

package demo.model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorld {

    public HelloWorld() {}

    @RequestMapping("/hello")   
    public String hello() {
        return "Hello, World!";
    }
}

我试图只返回文本,而不是视图,这就是我使用@RestController的原因。我正在运行Java JDK 7,Eclipse Luna和Tomcat 7.我试过点击localhost:8080 / friend-list / hello并获得404.另外,我已经插入了@RequestMapping(&#34; / friend-list&# 34;)在类的标题之上,并且还收到了404s到/ friend-list / hello和/ friend-list / friend-list / hello。

Tomcat日志:

INFO: Mapped URL path [/hello] onto handler 'helloWorld'
Jul 23, 2014 8:20:35 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello.*] onto handler 'helloWorld'
Jul 23, 2014 8:20:35 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/] onto handler 'helloWorld'

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>demo</groupId>
  <artifactId>friend-list</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>friend-list Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <build>
    <finalName>friend-list</finalName>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.0.5</version>
        <exclusions>
            <exclusion>
                <artifactId>jta</artifactId>
                <groupId>javax.transaction</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
  </dependencies>
</project>

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

一些事情:

*您需要更新您的web.xml :(连字符对于servlet名称很重要)

<servlet>
    <servlet-name>friend-list</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>friend-list</servlet-name>
    <url-pattern>/friend-list/*</url-pattern>
</servlet-mapping>

*对于Hello World,您缺少一些东西。

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
@RequestMapping("/hello")
public class HelloWorld {

    public HelloWorld() {
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public String hello() {
        return "Hello, World!";
    }
}

RequestMapping for the Resource然后你想要映射资源方法。 ResponseBody也不见了

*一旦您的应用启动并运行,您应该能够点击:     本地主机:8080 / MyApp的/朋友列表/你好

myapp是你为应用命名的任何地方。 我喜欢使用postman(Chrome插件,对我们来说很容易。)

* http://spring.io/guides/tutorials/rest/2/有关Spring和REST的优秀教程。

答案 1 :(得分:0)

Spring没有加载你的@RestController,因为spring配置文件没有与你的servlet名称相匹配的文件名。因此,如果Web.xml中的servlet名称是friendlist,那么spring配置文件应该命名为friendlist-servlet.xml