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. 深度学习实践-物体检测-faster-RCNN(原理和部分代码说明) 1.tf.image.resize_and_crop(根据比例取出特征层,进行维度变化) 2.tf.slice(数据切片) 3.x.argsort()(对数据进行排列,返回索引值) 4.np.empty(生成空矩阵) 5.np.meshgrid(生成二维数据) 6.np.where(符合条件的索引) 7.tf.gather取值

    1. tf.image.resize_and_crop(net, bbox, 256, [14, 14], name)  # 根据bbox的y1,x1,y2,x2获得net中的位置,将其转换为14*1 ...

  2. 微信小程序海报生成功能

    如果是H5页面的话给大家推荐一款不错的插件html2canvas,这个插件可以将html元素转为canvas并一键生成png图片,但是本文的重点是在小程序上如何实现生成图片的功能.因为小程序没有DOM ...

  3. linux 文件压缩与解压

    zip格式: zip -r(源文件是目录) [目标文件] [源文件] unzip -d [解压到的目录] [要解压的文件] gz格式: gzip [源文件]   #会删除源文件 gzip -c [源文 ...

  4. Java8 之stream

    总概述:Java 8 中的 Stream 是对集合(Collection)对象功能的增强,它专注于对集合对象进行各种非常便利.高效的聚合操作(aggregate operation),或者大批量数据操 ...

  5. 给统计人讲python(1)模拟城市_数据分析

    为让学校统计学社的同学了解python在数据处理方面的功能,将手游模拟城市的工厂生产进行建模,让同学在建模与处理非结构数据的过程中学习和了解python.将准备的内容放在此让更多需要的人特别是统计人( ...

  6. activemq stomp类

    此库用来增强ide,能对stomp类进行自动提示 <?php class Stomp { /** * 构造器 * Stomp constructor. * @param string $brok ...

  7. Android 代码判断是否有网络

    public void okGo() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemServic ...

  8. SQL Server数据库中的系统数据库?

    SQL Server的系统数据库分为:master,model,msdb和tempdb 1.Master数据库 Master数据库记录SQL Server系统的所有系统级别信息(表sysobjects ...

  9. Vue --1

    1.2 vue.js库的基本使用 在github下载:https://github.com/vuejs/vue/releases 在官网下载地址: https://cn.vuejs.org/v2/gu ...

  10. pg_dump 数据处理

    从数据库导出数据 -U 用户 -p 端口 -h 主机地址 -f 导出文件地址 -O 备份数据库结构和数据,不设置拥有者 -s  只导出数据库结构 最后是库名 (全部导出)pg_dump -U post ...