本文讲述如何用Spring做与Web Service相关的工作,包括建立服务器端和客户端。本文假设读者已经读过《在Eclipse中使用Maven建立Spring项目》一文。
用Spring构建RESTful Web Service Server
本节目标:
建立一个通过HTTP Get访问的Web Service:
http://localhost:8080/greeting
http://localhost:8080/greeting?name=User
返回JSON数据
{"id":1,"content":"Hello, World!"}
{"id":1,"content":"Hello, User!"}
用Maven建立项目
和《在Eclipse中使用Maven建立Spring项目》一文中介绍的一样,我们用spring-boot-starter来初始化我们的项目,所以parent project的Group Id写org.springframework.boot,Artifact Id写spring-boot-starter-parent,Version填1.2.5RELEASE,而自己的工程的名字可以随意填写。如下:
在建立好的项目的pom.xml中,添加依赖项,build的方式和java的版本号。由于我们要建立一个web app,这里的依赖项和上篇有所不同,为spring-boot-starter-web,其它则完全一致。
<properties>
<jave.version>1.8</jave.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugin