走近SpringBoot

(博客园不支持MarkDown编辑,看完整版请移步:https://www.zybuluo.com/Allen-llh/note/1199946)

1. (Building a RESTful Web Service)构建一个RESTFUL风格的浏览器

1.1 创建资源表示类 --设置好项目,创建了web服务,需要完成返回带有JSON的响应 
代码实例:

  1. public class Greeting {
  2. private final long id;
  3. private final String content;
  4. public Greeting(long id, String content) {
  5. this.id = id;
  6. this.content = content;
  7. }
  8. public long getId() {
  9. return id;
  10. }
  11. public String getContent() {
  12. return content;
  13. }
  14. }

1.2 创建资源控制器 --在Spring构建RESTful风格的服务器时,http请求由控制器处理

@RestController注释标记为控制器,其中每个方法返回一个域对象而不是视图,相当于@Controller和@ResponseBody拼凑在一起的。 
@RequestMapping注释可以确保http请求的get,post方法,要想指定get或者post的方法的话可以使用@RequestMapping(method=RequestMethod.GET/POST),或者可以直接使用@GetMapping,@PostMapping来指定请求方式

1.3 使应用程序可执行

SpringBoot项目虽然也可以打包为传统的war文件打包部署到服务器上运行,但是在此要学的是将所有内容打包为一个可执行的jar文件,使用的spring支持Tomcat Servlet容器作为http运行时嵌入,而不是到外部实例。 
@SpringBootApplication就是springboot项目的默认启动类

原文地址:https://www.cnblogs.com/javallh/p/9261789.html