本小节我们建立一个Restful风格的WebService。
主要效果如下:
当你用get方式访问 http://localhost:8080/greeting 的时候
后台将会给你返回一个json数据:{"id":1,"content":"Hello, World!"}
当你加上一个参数 name 访问 http://localhost:8080/greeting?name = spring
后台将会返回:{"id":1,"content":"Hello, spring!"}
下面开始。
首先分析一下你的服务的交互过程。
首先后台先接受到 请求地址/greeting ,该地址有可能附带着参数name。该接口将会返回 200 状态 以及 Json数据。
Json数据如下
{"id":1,"content":"Hello, World!"}
id 为每一次请求的唯一标识符
content 为返回的文本信息
你需要建立一个java类型来将该信息模型化。建立一个简单类包含属性id,content, 并给他们提供相应的访问器(get方法)。
如下:
1 package cn.tiny77.guide01.po; 2 3 public class Greet { 4 5 private Integer id; 6 7 private String content; 8 9 public Greet(Integer id, String content) { 10 super(); 11 this.id = id; 12 this.content = content; 13 } 14 15 public Integer getId() { 16 return id; 17 } 18 19 public void setId(Integer id) { 20 this.id = id; 21 } 22 23 public String getContent() { 24 return content; 25 } 26 27 public void setContent(String content) { 28 this.content = content; 29 } 30 31 32 33 }
提示:接下来的步骤中,你将会看到,spring利用fastxml自动将类转换为json
现在来建立Controller。
通过Srping的RestController注解很容易就可以建立一个http协议的Controller,且该Controller处理/greet地址请求时直接返回一个Greet类对象即可。
如下图
1 package cn.tiny77.guide01.controller; 2 3 import java.util.concurrent.atomic.AtomicInteger; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestParam; 6 import org.springframework.web.bind.annotation.RestController; 7 8 import cn.tiny77.guide01.po.Greet; 9 10 @RestController 11 public class GreetController { 12 13 private static final String template = "Hello, %s"; 14 15 private final AtomicInteger atomicInteger = new AtomicInteger(); 16 17 @RequestMapping("greet") 18 public Greet greet( 19 @RequestParam(value = "name", required = false, defaultValue = "World") String name) { 20 return new Greet(atomicInteger.incrementAndGet(), String.format(template, name)); 21 } 22 23 }
这个例子虽然十分简单,但是简单表面下包含很多知识点,让我们一一分析。
首先, @RequestMapping 注解 作用是 保证了请求能分发给 greet 方法。
上面例子中没有注明接受的是GET、POST还是其他类型请求,默认是接受所有类型请求, 如需指定类型,可如下
@RequestMapping(method=GET)
@RequestParam 将地址中的参数name 的值 绑定到 方法greet的 参数 name上。如果地址中没有该参数, 则默认绑定值 为 “World”
方法的返回值是一个Greet对象。
Greet对象的id 以自增的Integer初始化。
Greet的 content 用 模板进行初始化。
与传统的Mvc不同,Restful Web Service直接将对象转化为Json数据并传回,而不是通过相关的视图技术将数据 渲染到 html页面 返回。
@RestController是Spring4的一个新注解,它下面的每一个方法直接返回对象而不是视图。
它的作用相当于@Controller 和@ResponseBody放在一起的作用。
得益于Spring对消息类型转换的支持,你不用手动地将Greet转换为Json,Spring的MappingJackson2HttpMessageConverter将会自动地帮你将Greet对象转为Json
接下来,让程序抱起来。
传统的Http项目需要打包成war包,并依赖外部服务器如tomcat等,更简洁的做法是打包成独立的应用程序。这里你只需要将所有东西打包成jar,并通过Main函数进行启动。
Spring支持嵌入Tomcat 到应用中。
代码如下
1 package cn.tiny77.guide01; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class App 8 { 9 public static void main( String[] args ) 10 { 11 SpringApplication.run(App.class, args); 12 } 13 }
@SpringBootApplication 包含 以下注解的功能:
@Configuration 标记该类为配置文件类
@EnableAutoConfiguration 告诉SpringBoot开始添加依赖中的Bean、其他Bean以及不同的设置。
通常你要在Spring应用中添加@EnableWebMvc, 但是Spring发现依赖中有spring-webmvc时将自动添加该注解。这个注解标记该程序为Web程序,程序作用如设置了DisapatchServlet一般。
@ComponentScan 告诉Spring扫描相关包中的其他组件、配置和服务。
你会发现这个程序没有web.xml文件,这个程序是100%的无配置程序。
运行程序,测试。

项目:https://github.com/qinrongjin/SpringGuide01