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. PSR-1 基本代码规范

    基本代码规范 本篇规范制定了代码基本元素的相关标准, 以确保共享的PHP代码间具有较高程度的技术互通性. 关键词 “必须”("MUST").“一定不可/一定不能”("MU ...

  2. V-REP与C++初步通信测试

    打开vrep,在上方操作栏找到help选项打开,选择help topics.此时浏览器打开了vrep的操作手册user manual. 在user manual左侧目录中找到writing code ...

  3. centos7 设置nginx和php开机自启动

    nginx开机自启动 首先我是源码安装的,需要手动建立nginx.service服务文件 cd /lib/systemd/system touch nginx.service nginx.servic ...

  4. Windows、Linux的环境变量

    Windows操作系统 什么是环境变量?环境变量一般是指在操作系统中用来指定操作系统运行环境的一些参数,比如临时文件夹位置和系统文件夹位置等. 这点有点类似于DOS时期的默认路径,当你运行某些程序时除 ...

  5. if __name__ == '__main__':用法

    这个博主写的很好,已经验证过了.https://blog.csdn.net/yjk13703623757/article/details/77918633

  6. Nginx配置跨域支持功能

    跨域是前端开发中经常会遇到的问题,前端调用后台服务时,通常会遇到 No 'Access-Control-Allow-Origin' header is present on the requested ...

  7. spring框架校验自定义注解

    起因: 项目开发时遇到一个问题是对于金额类型的字段,数据库中格式一般为BigDecimal类型,两位小数点,然后在接口定义中如果不定义成String类型的话,就不能使用@pattern注解限定格式,而 ...

  8. Jquery中小数取整

    var uu=Math.floor(5.36) 向下取整 结果为5 var uu=Math.floor(5.88) 结果为5 Math.ceil(5.33) 向上取整,结果为6 Math.round( ...

  9. Linux NGINX部署

    一.Nginx简介 Nginx是一个web服务器也可以用来做负载均衡及反向代理使用,目前使用最多的就是负载均衡,具体简介我就不介绍了百度一下有很多,下面直接进入安装步骤 二.Nginx安装 1.下载N ...

  10. 一个简单的struts2上传图片的例子

    https://www.cnblogs.com/yeqrblog/p/4398914.html 在我的大创项目中有对应的应用