1.1 创建资源表示类 --设置好项目,创建了web服务,需要完成返回带有JSON的响应 代码实例:
public class Greeting { private final long id; private final String content; public Greeting(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } }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
