当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口

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

2019年06月08日  | 移动技术网IT编程  | 我要评论

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

2、一张图形容学springboot的心情

img

二、搭建springboot的环境

1、创建一个maven项目

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 如何区分环境

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网