Spring Boot 搭载你的网站框架(本次demo演示 使用工具 idea)
使用springboot优点,简单快速便捷,只需要管理好pom文件,构建一个新项目是非常快的。
而且内置tomcat,用命令 java -jar jar包路径 的形式启动网站,不管是Win还是Linux都非常方便,省去配置环境的步骤。
我先用一个 hello world 程序 来展示 springboot 的便捷。
一,idea 构建项目
Create New Project
一直Next
个人习惯,把原有的application.properties删掉,改成application.yml。需要的时候执行一下 clean;
我把端口改成 8080
启动后,网页访问。
我们来看一下控制器
使用@RestController注解,统一Api风格,对后面的前后端分离非常方便
@RestController:是@ResponseBody和@Controller的结合,返回的是数据(json或者字符串)
@GetMapping:查询 同等 @RequestMapping(method = RequestMethod.GET)
@PostMapping:新增 同等 @RequestMapping(method = RequestMethod.POST)
@PutMapping:修改 同等 @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping:删除 同等 @RequestMapping(method = RequestMethod.DELETE)
@PatchMapping:组合注释 同等 @RequestMapping(method = RequestMethod.PATCH)
具体可以看看官方文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestmapping