【转】Spring 4.x实现Restful web service

http://my.oschina.net/yuyidi/blog/352909

   首先我们还是跟之前一样,创建一个maven项目,不过因为Spring Restful web service是基于Spring 4.x版本的,所以我在这里就直接将Spring升级到了4.0.8,下面我贴出我的pom文件主要的依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<properties>
        <spring.version>4.0.8.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.4</version>
        </dependency>
        <!-- web项目编译时需要,部署到tomcat中就不需要了 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <!-- web项目编译时需要,部署到tomcat中就不需要了 -->
 
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.8.5</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.4.3</version>
        </dependency>
 
    </dependencies>
    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>

 

    这里有spring,springMVC,jackson及编译时需要的servlet-api及jsp-api依赖,就没有其他多余的依赖了。因为spring是原生支持Jackson,所以我这里并没有选择使用我常用的fastjson,或者gson。当然,如果你们在自己的项目中使用的是fastjson,或gson的话,也没有关系,替换使用的话也还是很方便与简单的,这里不做详细介绍。

    添加依赖后,就是配置web文件了。创建一个Restful web service与普通的springMVC项目没有什么区别,所以web配置也跟大家创建普通springMVC项目一样,简单的只需要配置一下servlet与servlet-mapping就可以了。然后就是指定springMVC配置文件,并添加到init-param或context-param中就可以了。

    

1
2
3
4
5
6
7
8
9
10
    <context:component-scan base-package="com.yyd.spring.aop.rest.controller" />
     
    <bean    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
            </list>
        </property>
    </bean>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController
 
public class GreetingController {
 
    private static final String template="Hello,%s";
 
    private final AtomicLong counter = new AtomicLong();
 
    @RequestMapping("/greeting")
 
    public Greeting greeting(@RequestParam(value="name",defaultValue="World") String name){
 
        return new Greeting(counter.getAndIncrement(),String.format(template, name));
 
    }
 
}

        这里我们看到控制器上的注解由我们经常使用的@Controller变成了@RestController。@RestControllr是spring 4.x中的新注解,@RestController,表明该类的每个方法返回对象而不是视图。它实际就是@Controller和@ResponseBody混合使用的简写方法。@ResponseBody的作用是将返回的对象放入响应消息体中("其实大家都懂的,啰嗦了一句。")。

        然后大家就可以启动tomcat或jetty服务器,在浏览器中输入:

1
http://localhost:8080/greeting

 

        然后就可以看到浏览器返回的结果了,

1
{"id":1,"content":"Hello, World!"}

 

        至此,一个简单的基于Spring 4.x的Restful web service框架就已经完成,谢谢大家,希望对大家有所帮助。

posted @ 2016-01-26 00:04  牧之丨  阅读(507)  评论(0编辑  收藏  举报