SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口

Stella981
• 阅读 500
本文源码
GitHub:知了一笑
https://github.com/cicadasmile/spring-boot-base

一、SpringBoot 框架的特点

1、SpringBoot2.0 特点

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)发展趋势看 微服务是未来发展的趋势,项目会从传统架构慢慢转向微服务架构,因为微服务可以使不同的团队专注于更小范围的工作职责、使用独立的技术、更安全更频繁地部署。

二、搭建SpringBoot的环境

1、创建一个Maven项目

SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口

2、引入核心依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3、编写配置文件

application.yml

# 端口
server:
  port: 8001

4、启动文件注解

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框架的环境搭建,是不是就是这个感觉:意会一下吧。

三、SpringBoot2.0 几个入门案例

1、创建一个Web接口

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格式数据。

2、参数映射

1)首先看看SpringBoot 如何区分环境 SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口

这里标识配置加载指定的配置文件。

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()+"]" ;
    }
}

3、RestFul 风格接口和测试

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
码云:知了一笑
https://gitee.com/cicadasmile/spring-boot-base

SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口

SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口

点赞
收藏
评论区
推荐文章
浅梦一笑 浅梦一笑
5个月前
初学 Python 需要安装哪些软件?超级实用,小白必看!
编程这个东西是真的奇妙。对于懂得的人来说,会觉得这个工具是多么的好用、有趣,而对于小白来说,就如同大山一样。其实这个都可以理解,大家都是这样过来的。那么接下来就说一下python相关的东西吧,并说一下我对编程的理解。本人也是小白一名,如有不对的地方,还请各位大神指出01名词解释:如果在编程方面接触的比较少,那么对于软件这一块,有几个名词一定要了解,比如开发环
blmius blmius
1年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
技术小男生 技术小男生
5个月前
linux环境jdk环境变量配置
1:编辑系统配置文件vi/etc/profile2:按字母键i进入编辑模式,在最底部添加内容:JAVAHOME/opt/jdk1.8.0152CLASSPATH.:$JAVAHOME/lib/dt.jar:$JAVAHOME/lib/tools.jarPATH$JAVAHOME/bin:$PATH3:生效配置
光头强的博客 光头强的博客
5个月前
Java面向对象试题
1、请创建一个Animal动物类,要求有方法eat()方法,方法输出一条语句“吃东西”。创建一个接口A,接口里有一个抽象方法fly()。创建一个Bird类继承Animal类并实现接口A里的方法输出一条有语句“鸟儿飞翔”,重写eat()方法输出一条语句“鸟儿吃虫”。在Test类中向上转型创建b对象,调用eat方法。然后向下转型调用eat()方
刚刚好 刚刚好
5个月前
css问题
1、在IOS中图片不显示(给图片加了圆角或者img没有父级)<div<imgsrc""/</divdiv{width:20px;height:20px;borderradius:20px;overflow:h
小森森 小森森
5个月前
校园表白墙微信小程序V1.0 SayLove -基于微信云开发-一键快速搭建,开箱即用
后续会继续更新,敬请期待2.0全新版本欢迎添加左边的微信一起探讨!项目地址:(https://www.aliyun.com/activity/daily/bestoffer?userCodesskuuw5n)\2.Bug修复更新日历2.情侣脸功能大家不要使用了,现在阿里云的接口已经要收费了(土豪请随意),\\和注意
晴空闲云 晴空闲云
5个月前
css中box-sizing解放盒子实际宽高计算
我们知道传统的盒子模型,如果增加内边距padding和边框border,那么会撑大整个盒子,造成盒子的宽度不好计算,在实务中特别不方便。boxsizing可以设置盒模型的方式,可以很好的设置固定宽高的盒模型。盒子宽高计算假如我们设置如下盒子:宽度和高度均为200px,那么这会这个盒子实际的宽高就都是200px。但是当我们设置这个盒子的边框和内间距的时候,那
艾木酱 艾木酱
5个月前
快速入门|使用MemFire Cloud构建React Native应用程序
MemFireCloud是一款提供云数据库,用户可以创建云数据库,并对数据库进行管理,还可以对数据库进行备份操作。它还提供后端即服务,用户可以在1分钟内新建一个应用,使用自动生成的API和SDK,访问云数据库、对象存储、用户认证与授权等功能,可专
Stella981 Stella981
1年前
SpringBoot2.0 基础案例分类目录,附源码地址
本文源码GitHub:知了一笑https://gitee.com/cicadasmile/springbootbaseSpringBoot2基础文章分类1、入门基础SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口(http
NVIDIA安培架构下MIG技术分析
关键词:NVIDIA、MIG、安培一什么是MIG2020年5月,NVIDIA发布了最新的GPU架构:安培,以及基于安培架构的最新的GPU:A100。安培提供了许多新的特性,MIG是其中一项非常重要的新特性。MIG的全名是MultiInstanceGPU。NVIDIA安培架构中的MIG模式可以在A100GPU上并行运行七个作业。多实
helloworld_28799839 helloworld_28799839
5个月前
常用知识整理
Javascript判断对象是否为空jsObject.keys(myObject).length0经常使用的三元运算我们经常遇到处理表格列状态字段如status的时候可以用到vue