软件编程
位置:首页>> 软件编程>> java编程>> SpringBoot返回多种格式的数据的实现示例

SpringBoot返回多种格式的数据的实现示例

作者:NicholasGUB  发布时间:2023-11-24 14:22:52 

标签:SpringBoot,返回,格式

一、SpringBoot整合FastJson

1.1、引入FastJson依赖包

maven项目:


<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.78</version>
</dependency>

gradle项目:


compile 'com.alibaba:fastjson:1.2.78'    // 引入fastjson

1.2、创建一个Web MVC的配置类,并放在springboot扫描包路径下。


package com.it.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
   public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
       // 1.springboot默认使用Jaskson组件,需要先移除Jaskson组件
       for (HttpMessageConverter<?> converter : converters) { // 循环所有的转换器
           if (converter instanceof MappingJackson2HttpMessageConverter) {
               converters.remove(converter); // 删除Jaskson转换器
           }
       }
       // 2. 项目中添加fastJson转换器
       FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
       // 3. 配置fastJson转换器
       FastJsonConfig fastJsonConfig = new FastJsonConfig();
       fastJsonConfig.setSerializerFeatures( // 配置序列化相关操作
               SerializerFeature.WriteMapNullValue,              // 允许Map内容为null
               SerializerFeature.WriteNullListAsEmpty,           // list集合为null使用[]代替
               SerializerFeature.WriteNullStringAsEmpty,         // String内容为null使用空文字代替
               SerializerFeature.WriteDateUseDateFormat,         // 日期格式化输出
               SerializerFeature.WriteNullNumberAsZero,          // 数字为空使用0代替
               SerializerFeature.DisableCircularReferenceDetect  // 禁用循环引用
       );
       fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); // 配置fastjson转换处理
       // 4. 配置响应的头信息
       List<MediaType> fastJsonMediaTypes = new ArrayList<>(); // 所有的响应类型
       fastJsonMediaTypes.add(MediaType.APPLICATION_JSON); // 使用JSON类型进行相应
       fastJsonHttpMessageConverter.setSupportedMediaTypes(fastJsonMediaTypes);
       // 5. 转换器列表中添加配置好的fastjson组件
       converters.add(fastJsonHttpMessageConverter);
   }
}

1.3、测试fastjson是否引入成功。

创建Message类:


import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;

@Data
public class Message {
   private String title;
   @JsonFormat(pattern = "yyyy年MM月dd日")
   private Date pubDate;
   private String content;
}

RestController中添加测试方法:


@RequestMapping("/echo")
public Object echo(Message message) {
   message.setTitle("【echo】" + message.getTitle());
   message.setContent("【echo】" + message.getContent());
   return message;
}

访问echo发现fastjson引入成功:

SpringBoot返回多种格式的数据的实现示例

二、SpringBoot返回XML数据

2.1、引入jackson组件依赖

jackson组件既支持json操作,也支持xml操作。


<dependency>
   <groupId>com.fasterxml.jackson.dataformat</groupId>
   <artifactId>jackson-dataformat-xml</artifactId>
   <version>2.12.2</version>
</dependency>

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.12.2</version>
</dependency>

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-annotations</artifactId>
   <version>2.12.2</version>
</dependency>

如果使用的是gradle构建项目:


compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.12.2'
compile 'com.fasterxml.jackson.core:jackson-databind:2.12.2'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.12.2'

2.2、新建vo类,引入jackson-xml注解


package com.it.vo;

import lombok.Data;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;

@Data
@XmlRootElement  // 定义XML根元素
public class Message {
   @XmlElement  // xml元素
   private String title;
   @XmlElement
   private Date pubDate;
   @XmlElement
   private String content;
}

2.3、建立RestController测试返回数据


@RequestMapping("/echo")
public Object echo(Message message) {
   message.setTitle("【echo】" + message.getTitle());
   message.setContent("【echo】" + message.getContent());
   return message;
}

SpringBoot返回多种格式的数据的实现示例

三、SpringBoot返回PDF数据

PDF是Portable Document Format的简称,意为“可携带文档格式”,是由Adobe Systems用于与应用程序、操作系统、硬件无关的方式进行文件交换所发展出的文件格式。PDF文件以PostScript语言图象模型为基础,无论在哪种打印机上都可保证精确的颜色和准确的打印效果,即PDF会忠实地再现原稿的每一个字符、颜色以及图象。

在java项目中,itextpdf组件是比较常见的pdf创建工具、如果想要让SpringBoot程序以PDF的形式进行相应,那么需要引入ITextPdf创建组件依赖。

3.1、引入ITextPdf组件依赖


<dependency>
   <groupId>com.itextpdf</groupId>
   <artifactId>itextpdf</artifactId>
   <version>5.5.13.2</version>
</dependency>

如果使用的是gradle构建的项目:


compile 'com.itextpdf:itextpdf:5.5.13.2'

3.2、引入系统字体库

将pdf打印需要用到的字体放到项目资源路径:src/main/resources/fonts下(windows系统字体库路径:C:\Windows\Fonts)

SpringBoot返回多种格式的数据的实现示例

3.3、在pdf中存入图片

src/main/resources/images下存放一张图片pic.jpg。

SpringBoot返回多种格式的数据的实现示例

3.4、创建pdf生成控制器


package com.it.action;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/pdf")
public class PDFAction {

@GetMapping("/create")
   public void createPDF(HttpServletResponse response) throws Exception { // 使用response处理响应
       response.setHeader("Content-Type", "application/pdf"); // 设置相应类型
       // 强制开启下载,并配置下载名称
       response.setHeader("Content-Disposition", "attachment;filename=a.pdf");
       // 使用iTextPdf在内存生成pdf
       Document document = new Document(PageSize.A4, 10, 10, 50, 20); // 设置页面大小、边距
       // 获取pdf的输出流配置
       PdfWriter.getInstance(document, response.getOutputStream());
       // 开始构建pdf文档内容
       document.open();
       Resource imageResource = new ClassPathResource("/images/pic.jpg"); // Spring提供的资源访问
       Image image = Image.getInstance(imageResource.getFile().getAbsolutePath()); // 通过指定路径加载图片
       // PDF在生成文件的时候是基于坐标的方式进行绘制
       image.scaleToFit(PageSize.A4.getWidth() / 2, PageSize.A4.getHeight());
       float printX = (PageSize.A4.getWidth() - image.getScaledWidth()) / 2;
       float printY = PageSize.A4.getHeight() - image.getHeight() - 100;
       image.setAbsolutePosition(printX, printY); // 设置图片绘制坐标
       document.add(image);
       document.add(new Paragraph("\n\n\n")); //图片之后换三行输出文字
       // 加载字库
       Resource fontResource = new ClassPathResource("/fonts/FZSTK.TTF");
       BaseFont baseFont = BaseFont.createFont(fontResource.getFile().getAbsolutePath(),
               BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
       Font font = new Font(baseFont, 20, Font.NORMAL); // 引用字库
       // pdf上绘制文本信息
       String[] titles = new String[]{"springboot test"};
       for (String title : titles) { // 循环输出
           PdfPTable table = new PdfPTable(2); // 定义表格
           PdfPCell cell = new PdfPCell(); //创建单元格
           cell.setPhrase(new Paragraph(title, font)); // 单元格内容
           table.addCell(cell); // 追加单元格
           document.add(table); // 追加文档
       }
       document.close();
   }
}

四、SpringBoot返回Excel数据

springboot为了便于用户生成Excel文件,提供了easypoi-spring-boot-starter依赖库。

4.1、引入easypoi-spring-boot-starter依赖库


<dependency>
   <groupId>cn.afterturn</groupId>
   <artifactId>easypoi-spring-boot-starter</artifactId>
   <version>4.4.0</version>
</dependency>

如果是gradle项目:


compile 'cn.afterturn:easypoi-spring-boot-starter:4.4.0'

4.2、新建Message类

excel表格可以通过java bean转换生成。


package com.it.vo;

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;

import java.util.Date;

@Data
public class Message {
   @Excel(name = "信息标题", orderNum = "0", width = 30)
   private String title;
   @Excel(name = "信息日期", orderNum = "1", width = 50)
   private Date pubDate;
   @Excel(name = "信息内容", orderNum = "2", width = 100)
   private String content;
}

4.3、新建ExcelAction负责生成Excel


package com.it.action;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.export.ExcelExportService;
import com.it.vo.Message;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/excel")
public class ExcelAction {

@GetMapping("/create")
   public void createExcel(HttpServletResponse response) throws Exception { // 使用response处理响应
       response.setHeader("Content-Type",
               "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // 设置响应类型
       // 强制开启下载,并配置下载名称
       response.setHeader("Content-Disposition", "attachment;filename=test.xls");
       List<Message> messageList = new ArrayList<>();
       messageList.add(new Message("重大消息", new Date(), "xxx厂喜迎重大改革"));
       messageList.add(new Message("首届稀土开发者大会全日程公布", new Date(), "27-28日直播两天精彩不停!"));
       ExportParams exportParams = new ExportParams("消息管理", "最新消息", ExcelType.XSSF);
       XSSFWorkbook workbook = new XSSFWorkbook();
       new ExcelExportService().createSheet(workbook, exportParams, Message.class, messageList);
       workbook.write(response.getOutputStream());
   }
}

SpringBoot返回多种格式的数据的实现示例 

五、SpringBoot返回资源流

在进行前后端分离设计的时候,需要进行一些资源的加载,一般会有两种做法:

  • 直接通过远程文件服务器进行资源的加载。

  • 通过程序进行加载。

5.1、返回图像流

程序在进行图像流返回的时候只需要将返回类型设置为图片即可。

5.1.1、创建ImageAction负责返回图像流


package com.it.action;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/image")
public class ImageAction {

@GetMapping(value = "/download", produces =
           {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_GIF_VALUE, MediaType.IMAGE_PNG_VALUE}) // 设置返回类型
   public byte[] createImage() throws IOException {
       Resource imageResource = new ClassPathResource("/images/dog.jpg");
       InputStream inputStream = imageResource.getInputStream();
       byte[] bytes = new byte[inputStream.available()];
       inputStream.read(bytes, 0, imageResource.getInputStream().available());// 实现文件加载
       return bytes;
   }
}

5.1.2、输入访问路径

http://localhost:8080/image/download

SpringBoot返回多种格式的数据的实现示例 

5.2、返回视频流

SpringBoot可以实现对视频流的控制。

5.2.1、提供视频资源的请求处理器


package com.it.handler;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
* 请求处理器
*/
@Component
public class VideoResourceHttpRequestHandler extends ResourceHttpRequestHandler {
   @Override
   public Resource getResource(HttpServletRequest request) throws IOException {
       return new ClassPathResource("/videos/study.mp4");
   }
}

5.2.2、定义视频响应Action类


package com.it.action;

import com.it.handler.VideoResourceHttpRequestHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/video")
public class VideoAction {

private final VideoResourceHttpRequestHandler videoResourceHttpRequestHandler;

public VideoAction(VideoResourceHttpRequestHandler videoResourceHttpRequestHandler) {
       this.videoResourceHttpRequestHandler = videoResourceHttpRequestHandler;
   }

@GetMapping("/download")
   public void createVideo(HttpServletRequest request, HttpServletResponse response) throws Exception {
       this.videoResourceHttpRequestHandler.handleRequest(request, response);
   }
}

5.2.3、输入访问路径

http://localhost:8080/video/download

SpringBoot返回多种格式的数据的实现示例

六、SpringBoot文件下载

SpringBoot可以直接通过输出流的方式实现文件下载,例如下载resources/files/banner.rar文件:

SpringBoot返回多种格式的数据的实现示例


package com.it.action;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/file")
public class DownloadAction {

@GetMapping("/download")
   public void fileDownload(HttpServletResponse response) throws IOException {
       response.setContentType("application/force-download");// 强制性下载
       response.setHeader("Content-Disposition", "attachment;filename=banner.rar");
       Resource fileResource = new ClassPathResource("/files/banner.rar"); // 要下载的文件
       // 通过IO流读取文件内容
       InputStream input = fileResource.getInputStream();
       byte[] data = new byte[1024]; // 每次最多读取1024字节
       int len = 0; // 每次读取的字节数
       while ((len = input.read(data)) != -1) {
           response.getOutputStream().write(data, 0, len);
       }
   }
}

访问:http://localhost:8080/file/download:

SpringBoot返回多种格式的数据的实现示例

来源:https://blog.csdn.net/Nicholas_GUB/article/details/120992824

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com