1)springboot继承了spring优秀的基因,上手难度小
2)简化配置,提供各种默认配置来简化项目配置
3)内嵌式容器简化web项目,简化编码
spring boot 则会帮助开发着快速启动一个 web 容器,在 spring boot 中,只需要在 pom 文件中添加如下一个 starter-web 依赖即可.
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency>
4)发展趋势看
微服务是未来发展的趋势,项目会从传统架构慢慢转向微服务架构,因为微服务可以使不同的团队专注于更小范围的工作职责、使用独立的技术、更安全更频繁地部署。
img
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency>
application.yml
# 端口 server: port: 8001
import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication public class helloapplication { public static void main(string[] args) { springapplication.run(helloapplication.class,args) ; } }
丝毫没有问题,就这样吧启动上面这个类,springboot的基础环境就搭建好了。
想想之前的spring框架的环境搭建,是不是就是这个感觉:意会一下吧。
import com.boot.hello.entity.projectinfo; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; /** * springboot 2.0 第一个程序 */ @restcontroller public class hellocontroller { @requestmapping("/getinfo") public projectinfo getinfo (){ projectinfo info = new projectinfo() ; info.settitle("springboot 2.0 基础教程"); info.setdate("2019-06-05"); info.setauthor("知了一笑"); return info ; } }
@restcontroller 注解 等价 @controller + @responsebody 返回json格式数据。
1)首先看看springboot 如何区分环境
这里标识配置加载指定的配置文件。
2)参数配置
application-pro.yml
user: author: 知了一笑 title: springboot 2.0 程序开发 time: 2019-07-05
3)参数内容读取
@component public class paramconfig { @value("${user.author}") private string author ; @value("${user.title}") private string title ; @value("${user.time}") private string time ; // 省略 get 和 set 方法 }
4)调用方式
/** * 环境配置,参数绑定 */ @restcontroller public class paramcontroller { @resource private paramconfig paramconfig ; @requestmapping("/getparam") public string getparam (){ return "["+paramconfig.getauthor()+";"+ paramconfig.gettitle()+";"+ paramconfig.gettime()+"]" ; } }
1)rest风格接口
/** * rest 风格接口测试 */ @restcontroller // 等价 @controller + @responsebody 返回json格式数据 @requestmapping("rest") public class restapicontroller { private static final logger log = loggerfactory.getlogger(restapicontroller.class) ; /** * 保存 */ @requestmapping(value = "/insert",method = requestmethod.post) public string insert (userinfo userinfo){ log.info("===>>"+userinfo); return "success" ; } /** * 查询 */ @requestmapping(value = "/select/{id}",method = requestmethod.get) public string select (@pathvariable integer id){ log.info("===>>"+id); return "success" ; } }
2)测试代码
@runwith(springjunit4classrunner.class) @springboottest(classes = mockservletcontext.class) @webappconfiguration public class testrestapi { private mockmvc mvc; @before public void setup() throws exception { mvc = mockmvcbuilders.standalonesetup(new restapicontroller()).build(); } /** * 测试保存接口 */ @test public void testinsert () throws exception { requestbuilder request = null; request = post("/rest/insert/") .param("id", "1") .param("name", "测试大师") .param("age", "20"); mvc.perform(request) .andexpect(content().string(equalto("success"))); } /** * 测试查询接口 */ @test public void testselect () throws exception { requestbuilder request = null; request = get("/rest/select/1"); mvc.perform(request) .andexpect(content().string(equalto("success"))); } }
这样springboot2.0的入门案例就结束了,简单,优雅,有格调。
github:知了一笑 https://github.com/cicadasmile/spring-boot-base
如对本文有疑问, 点击进行留言回复!!
[杭电多校2020]第一场 1004 Distinct Sub-palindromes
Swift -- 将本地生成的UIImage进行持久化保存(存到文件中fileManager.createFile)
网友评论