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. [Go] 开始试探一门新语言的五点思考 - Golang

    1.如果在其他语言环境中写的代码很烂,那么换一门语言很可能情况更糟,因为是涉及到基本功.工程能力和心思逻辑. 2.一定要了解语言解决的问题(比如:多核并发机制性能高.省机器.简洁易学.资料少),优势是 ...

  2. layui表格点击排序按钮后,表格绑定事件失效解决方法

    最近项目使用layui较为频繁,遇到了一个麻烦的问题,网上搜索也没有看到同类型的问题,故此记下来. 需求是点击上图右侧表格中某一个单元格,会触发点击事件如下代码: $("table>t ...

  3. 阿里云消息队列的C#使用http接口发送消息实例

    app.config <appSettings> <clear/> <add key="Ons_Topic" value="XXX_Fini ...

  4. Nagios监控

    1.Nagios监控软件 Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警 ...

  5. Unity 读写文本 文件

    1. LitJson的使用 https://blog.csdn.net/qq_35669619/article/details/78928966 https://blog.csdn.net/qq_14 ...

  6. .net 语音,视频等格式转换

    最近在做微信公众号开发的时候遇到一个问题,就是微信接收到语音消息的格式为amr,在网页上通常不能直接播放,需要先转为mp3,于是找到了一个办法,使用ffmpeg.exe,网上可以搜一下ffmpeg.e ...

  7. Linux 基本使用

    1.mkdir 创建文件夹 mkdir filename; 2.touch 创建文件 touch file.txt; 3.chmod 变更文件或目录的权限 chmod -R u+x ./;

  8. Java框架spring Boot学习笔记(九):一个简单的RESTful API

    RESTful API设计需求如下: User.java package com.springboot.test; public class User { private Long id; priva ...

  9. svn 恢复删除文件

    参考这个 https://blog.csdn.net/qdujunjie/article/details/37766167

  10. HDU - 1695 GDU

    莫比乌斯反演基础. 用rep 去掉重复的对数,rep一定是奇数( 因为有(1,1 ) ) #include <bits/stdc++.h> using namespace std; #de ...