Python 官方文档:入门教程 => 点击学习
目录一、SpringBoot整合FastJSON1.1、引入Fastjson依赖包1.2、创建一个WEB mvc的配置类,并放在springboot扫描包路径下。1.3、测试fast
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
gradle项目:
compile 'com.alibaba:fastjson:1.2.78' // 引入fastjson
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);
}
}
创建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引入成功:
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'
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;
}
@RequestMapping("/echo")
public Object echo(Message message) {
message.setTitle("【echo】" + message.getTitle());
message.setContent("【echo】" + message.getContent());
return message;
}
PDF是Portable Document Format的简称,意为“可携带文档格式”,是由Adobe Systems用于与应用程序、操作系统、硬件无关的方式进行文件交换所发展出的文件格式。PDF文件以PostScript语言图象模型为基础,无论在哪种打印机上都可保证精确的颜色和准确的打印效果,即PDF会忠实地再现原稿的每一个字符、颜色以及图象。
在java项目中,itextpdf组件是比较常见的pdf创建工具、如果想要让SpringBoot程序以PDF的形式进行相应,那么需要引入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'
将pdf打印需要用到的字体放到项目资源路径:src/main/resources/fonts
下(windows系统字体库路径:C:\Windows\Fonts)
在src/main/resources/images
下存放一张图片pic.jpg。
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文件,提供了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'
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;
}
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());
}
}
在进行前后端分离设计的时候,需要进行一些资源的加载,一般会有两种做法:
程序在进行图像流返回的时候只需要将返回类型设置为图片即可。
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.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可以直接通过输出流的方式实现文件下载,例如下载resources/files/banner.rar
文件:
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返回多种格式的数据的实现示例的文章就介绍到这了,更多相关SpringBoot返回多种格式内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringBoot返回多种格式的数据的实现示例
本文链接: https://lsjlt.com/news/155869.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0