Springboot & Mybatis 构建restful 服务五

1 前置条件

  • 成功执行完Springboot & Mybatis 构建restful 服务四

2 restful service 整合 swigger ui 自动生成 API

1)修改 POM.xml文件

添加依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>
 

2)修改 application.properties

src/main/resources/application.properties

server.context-path=/accountbalance
 

3)新建Swagger2Configuration.java

​ src/main/java/com/serena/config/Swagger2Configuration.java

package com.serena.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
    @Bean
    public Docket buildDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.serena.controller"))//要扫描的API(Controller)基础包
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo buildApiInf() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2 UI构建AccountBalance API文档")
                .contact("psj")
                .version("1.0")
                .build();
    }
}
 

4)修改 SY,添加注解

​ src/main/java/com/serena/controller/SY.java

package com.serena.controller;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.serena.entity.SettleAccount;
import com.serena.service.ISY;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@Api("查找账户余额")
@RestController
public class SY {
    @Autowired
    private ISY iSY;

    private static Logger logg = LoggerFactory.getLogger(SY.class);

    @ApiOperation("通过账户编号查询账户余额等信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name="accountCode",required=true,dataType="String",paramType="Path")
    })
    @RequestMapping(value="/account/{accountCode}",method = RequestMethod.GET)
    public SettleAccount selectOne(@PathVariable("accountCode")String accountCode){
        logg.info("select account by accountCode,accountCode = {}",accountCode); 
        SettleAccount settleAccount  = iSY.selectByAccountCode(accountCode);
        if(settleAccount == null)
            logg.warn("not found account");
            return settleAccount;
    }

    @ApiOperation("查询所有账户余额等信息")
    @RequestMapping(value="/accounts",method = RequestMethod.GET)
    public List<SettleAccount> selectAll(){
        List<SettleAccount> list = null;
        logg.info("select all accounts"); 
        list = iSY.selectAccounts();
        if(list == null)
            logg.warn("not found accounts");
        return list;
    }

    @ApiOperation(value="查询所有账户余额等信息,并导出在 excel 表格中")
    @ApiImplicitParams({
        @ApiImplicitParam(name="fileName",required=true,dataType="String",paramType="Path")
    })
    @RequestMapping(value="/accountsfile/{fileName}",method = RequestMethod.GET)
    public boolean wSelectAll(@PathVariable("fileName")String fileName){
        logg.info("write a file that select accounts "); 
        List<SettleAccount> list = null;
        boolean flag = false;
        list = iSY.selectAccounts();
        if(list == null)
            logg.warn("not found accounts");
        else{
            flag = iSY.createExcel(list, fileName);
        }
        return flag;
    }
}
 

5)在终端输入如下测试指令:

#cd 项目所在目录
cd /Users/psj/Documents/pro/xm/AccountBalance
mvn clean package
cd target
mkdir /Users/psj/Desktop/t/
#将 tar 包复制到自己指定目录(/Users/psj/Desktop/t/)
cp AccountBalance-0.0.1-SNAPSHOT.tar /Users/psj/Desktop/t/
#cd 到上个操作指定的目录
cd /Users/psj/Desktop/t
#解压 tar 包
tar -xvf AccountBalance-0.0.1-SNAPSHOT.tar
#此时可查看目录结构如要求所示
ll
#运行 可执行jar,测试结果
java -jar AccountBalance-0.0.1-SNAPSHOT.jar
#
#打开浏览器,输入如下网址:
http://localhost:8101/accountbalance/swagger-ui.html
#
http://localhost:8999/accountsfile
#在下载对话框中指定文件名和保存路径
#
#打开新的iterm 窗口(command+n)
http localhost:8101/accountbalance/accounts
http localhost:8101/accountbalance/account/U00001
#返回上个 iterm 窗口,control+c 结束服务
 

Springboot & Mybatis 构建restful 服务五的更多相关文章

  1. Springboot & Mybatis 构建restful 服务四

    Springboot & Mybatis 构建restful 服务四 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务三 2 restful ...

  2. Springboot & Mybatis 构建restful 服务三

    Springboot & Mybatis 构建restful 服务三 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务二 2 restful ...

  3. Springboot & Mybatis 构建restful 服务二

    Springboot & Mybatis 构建restful 服务二 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务一 2 restful ...

  4. Springboot & Mybatis 构建restful 服务

    Springboot & Mybatis 构建restful 服务一 1 前置条件 jdk测试:java -version maven测试:命令行之行mvn -v eclipse及maven插 ...

  5. 基于SpringBoot开发一个Restful服务,实现增删改查功能

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

  6. jersey+maven构建restful服务

    一.新建一个Maven Web项目 a) 新建一个简单的Maven项目 b) 将简单的Maven项目转成Web项目 (若没出现further configuration available--或里面的 ...

  7. 用Jersey构建RESTful服务7--Jersey+SQLServer+Hibernate4.3+Spring3.2

    一.整体说明 本例执行演示了用 Jersey 构建 RESTful 服务中.怎样集成 Spring3 二.环境 1.上文的项目RestDemo 2.Spring及其它相关的jar ,导入项目 三.配置 ...

  8. JAX-RS(基于Jersey) + Spring 4.x + MyBatis构建REST服务架构

    0. 大背景 众所周知,REST架构已经成为现代服务端的趋势. 很多公司,已经采用REST作为App, H5以及其它客户端的服务端架构. 1. 什么是JAX-RS? JAX-RS是JAVA EE6 引 ...

  9. SpringBoot 快速构建微服务体系 知识点总结

    可以通过http://start.spring.io/构建一个SpringBoot的脚手架项目 一.微服务 1.SpringBoot是一个可使用Java构建微服务的微框架. 2.微服务就是要倡导大家尽 ...

随机推荐

  1. MTK6261 11C之Init Analysis【手记】

    初始化流程 Application_Initialize systemInitialization(); HWDInitialization(); USC_Start(); OSTD_Init(); ...

  2. 地址栏输入url按回车发生了什么

    浏览器向DNS服务器(先查找缓存)查找输入URL对应的IP地址 DNS服务器返回对应的IP地址 浏览器根据IP地址与目标web服务器在80端口上建立TCP连接 浏览器获取请求页面的html代码 浏览器 ...

  3. Java线程池应用及原理分析(JDK1.8)

    目录 一.线程池优点 二.线程池创建 三.任务处理流程 四.任务缓存队列及排队策略 五.任务拒绝策略 六.线程池关闭 七.线程池实现原理 八.静态方法创建线程池 九.如何确定线程池大小 一.线程池优点 ...

  4. css样式的书写顺序及原理——很重要!

    记得刚开始学习前端的时候,每次写css样式都是用到什么就在样式表后添加什么,完全没有考虑到样式属性的书写顺序对网页加载代码的影响.后来逐渐才知道正确的样式顺序不仅易于查看,并且也属于css样式优化的一 ...

  5. MM-分割评估

    SAP MM分割评估 https://blog.csdn.net/kangliujie/article/details/76681333 SAP MM批次管理分割评估 https://blog.csd ...

  6. C# 异步机制

    Delegate.Invoke Delegate.Invoke is used to execute a delegate on the current thread. A delegate is j ...

  7. 软件工程小组讨论设计NABCD

    项目名称:失物招领平台 项目工作小组:冰淇淋队 项目简介:目前同学们丢了东西都qq空间转发或者某个特定的qq群发消息,qq空间转发浪费了别人的时间,qq群发消息也浪费了别人的时间.怎么样才能浪费最少的 ...

  8. python中的函数和变量

    本节内容 函数的定义方法 函数功能 函数的返回值 函数的形参与实参 全局变量与局部变量 递归 函数的作用域 匿名函数lambda 函数式编程 常用内置函数 其他内置函数 函数 函数的定义方法 函数就相 ...

  9. ES match match_phrase term willcard的查询原理

    比如:要求实现SQL中like “%xxxx%”的匹配效果. wildcard通配 这种效果在ES中最匹配的做法是用wildcard query通配,这种情况不会对query分词,而是直接遍历倒排索引 ...

  10. python 写入数据

    import sys reload(sys) sys.setdefaultencoding('utf8') import xlrd import xlwt book = xlrd.open_workb ...