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. azkaban使用--传入动态参数

    转: azkaban的工作流中的参数可以分为如下几个类型:azkaban UI 页面输入参数, 环境变量参数, job作业文件中定义的参数,工作流的用户定义的属性文件,上游作业传递给下游的参数,工作流 ...

  2. window Linux 双系统安装

    我是先安装的win10,然后在其基础上又安装了Ubuntu 16.04,为了今后再次安装方便,这里记录一下安装过程. 我在安装时主要参考了文章:https://blog.csdn.net/flyyuf ...

  3. 将Python脚本打包成可执行文件——转载

    Python是一个脚本语言,被解释器解释执行.它的发布方式: py文件:对于开源项目或者源码没那么重要的,直接提供源码,需要使用者自行安装Python并且安装依赖的各种库.(Python官方的各种安装 ...

  4. 20175213 2018-2019-2 《Java程序设计》第7周学习总结

    教材学习内容总结 (1)String (char a[])用一个字符数组a创建一个String对象. (2)String(char a[],int startIndex,int count) 提取字符 ...

  5. API权限设计总结

    最近在做API的权限设计这一块,做一次权限设计的总结. 1. 假设我们需要访问的API接口是这样的:http://xxxx.com/openapi/v1/get/user/?key=xxxxx& ...

  6. @ControllerAdvice详解

    @ControllerAdvice,是Spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强.让我们先看看@ControllerAdvice的实现:   package org.spri ...

  7. 深入Dockerfile(一): 语法指南(转)

    最近在学习K8S,发现这两篇文章还不错,转了过来 docker官方文档Dockerfile reference的笔记. 一.机制 1.1 构建 docker构建一个镜像,需要: Dockerfile文 ...

  8. ExpandableListView解析JSON数据

    效果图:                                       说明:刚开始使用这个控件我花费了3天的时间,但是一直都没有达到预期的效果,要么就是直接全部不显示,要么就是数据累加 ...

  9. vim常用配置 vimrc文件

    自从接触vim,自己瞎鼓捣.vimrc也有一段时间了.收集记录一下好用的配置. 一.奇技淫巧 1.折叠代码 折叠代码常常用在代码块较长的情况下,比如一个文件里定义了很多个函数,或者注释.括号影响的阅读 ...

  10. innodb_flush_log_at_trx_commit与sync_binlog理解

    innodb_flush_log_at_trx_commit该参数控制重做日志写入磁盘的过程.我们知道 InnoDB 使用“Write Ahead Log”策略来避免数据丢失问题,即依靠重做日志来保证 ...