Springboot & Mybatis 构建restful 服务四

1 前置条件

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

2 restful service 添加 Apache POI生成 Excel 文件

1)修改 POM.xml文件

添加 Apache POI 的依赖

        <dependency>
             <groupId>org.apache.poi</groupId>  
             <artifactId>poi-ooxml</artifactId>  
             <version>3.5-FINAL</version>  
        </dependency>
 

2)在SY里添加创建 excel 文件的方法

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

 
    @ApiOperation(value="查询所有账户余额等信息,并导出在 excel 表格中")
    @RequestMapping(value="/accountsfile",method = RequestMethod.GET)
    public boolean wSelectAll(HttpServletResponse response){
        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{
            // 创建表对象
            HSSFWorkbook workBook = new HSSFWorkbook();
            ServletOutputStream fileOut = null;
            try {
                // 获取输出流对象
                fileOut = response.getOutputStream();
                // 创建 sheet
                HSSFSheet sheet = workBook.createSheet();
                // 设置表格第一行的字段名
                HSSFRow row = sheet.createRow(0);
                int i =1;
                row.createCell(0).setCellValue("账户编号");
                row.createCell(1).setCellValue("账户名");
                row.createCell(2).setCellValue("客户编号");
                row.createCell(3).setCellValue("短信余额");
                for (SettleAccount acc : list) {
                    HSSFRow rowi = sheet.createRow(i++);
                    rowi.createCell(0).setCellValue(acc.getAccountcode());
                    rowi.createCell(1).setCellValue(acc.getAccountname());
                    rowi.createCell(2).setCellValue(acc.getCustomercode());
                    rowi.createCell(3).setCellValue(acc.getSmsnum().doubleValue());
                }
                workBook.write(fileOut);
                return true;
            } catch (FileNotFoundException e) {
                logg.error("create table error ---- " + e.getMessage());
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                logg.error("create table error ---- " + e.getMessage());
                e.printStackTrace();
                return false;
            } finally {
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        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:8999/accountsfile
#在下载对话框中指定文件名和保存路径
#
#返回上个 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. 使用spring mvc或者resteasy构建restful服务

    看到最近一个项目里用resteasy来构建restful接口,有点不明白,不少Spring mvc4.0以后也可以很方面的实现restful服务吗,为啥还要在Spring MVC的项目里还引入rest ...

随机推荐

  1. java开发人员win10配置

    1.让win10的cmd支持ll命令 新建ll.bat 编辑类容为: @echo off dir3.将该文件移动到C:\Windows下. CMD里就可以用ls来代替dir命令显示目录列表了. 2.i ...

  2. Java开发体系

    蓦然回首自己做开发已经十年了,这十年中我获得了很多,技术能力.培训.出国.大公司的经历,还有很多很好的朋友.但再仔细一想,这十年中我至少浪费了五年时间,这五年可以足够让自己成长为一个优秀的程序员,可惜 ...

  3. Java学习随笔(1)--groovy爬虫

    package com.fan import com.fission.source.httpclient.ApiLibraryimport com.fission.source.httpclient. ...

  4. Ubuntu17.10安装Docker

    Ubuntu环境下安装Docker 1. 系统要求 Docker目前只能运行在64位平台上,并且要求内核版本不低于3.10,实际上内核越新越好,过低的内核版本容易造成功能不稳定. 可以通过如下命令检查 ...

  5. IDE 设备(磁盘/CD-ROM)配置不正确。“ide1:1”上具有一个 IDE 从设备,但没有主设备。此配置在虚拟机中无法正常运行。请使用配置编辑器将磁盘/CD-ROM 从“ide1:1”移到“ide1:0”。

    开启vmware报这个错: IDE 设备(磁盘/CD-ROM)配置不正确.“ide1:1”上具有一个 IDE 从设备,但没有主设备.此配置在虚拟机中无法正常运行.请使用配置编辑器将磁盘/CD-ROM ...

  6. logback kafkaAppender输出日志到kafka

    官网地址https://github.com/danielwegener/logback-kafka-appender 本文以spring boot项目为基础,更多的信息,请参考官网 https:// ...

  7. TS和C#的差异

    1. TS中let a : () => void; 假设有个class  B,  B里有个方法 c; 不能像C#委托一样, a = B.c;...........如果这样的话方法c里调用的thi ...

  8. find 递归/不递归 查找子目录的方法

    1.递归查找(find 命令 是递归遍历文件夹的) 命令:find . -name “*.txt” //当前路径下递归查找以.txt结尾的文件夹 2.不递归查找 find . -name “*.txt ...

  9. html 自定义上传图片样式,并回显

    <div id="photoUpLoad"> <input type="file" id="photo" name=&qu ...

  10. CodeWarrior 10 配置Jlint初始化文件

    新建一个项目之后,飞思卡尔的仿真器的配置不如德州仪器那么简单.他需要一些配置. 当我们新建一个工程后,可以采取如下步骤配置Jlint: 1.右击工程名,选择属性. 2.在Run/Debug Setti ...