生成PDF / XLSX文件作为REST API的附件

时间:2018-07-25 11:26:33

标签: spring-boot content-negotiation

要求是在点击REST API端点时生成pdf / excel文档。 即localhost:8080 / download.xlsx->生成具有.xlsx格式数据的文件 localhost:8080 / download.pdf->生成一个包含.pdf格式数据的文件

Spring Boot版本-2.0.2

当我使用Spring Boot的相对较新的版本时,我必须实现WebMvcConfigurer而不是扩展WebMvcConfigurerAdapter

我正在配置ContentNegotiatingViewResolver

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer 
 configurer) {
    configurer
            .favorPathExtension(true)
            .defaultContentType(MediaType.APPLICATION_JSON);
}

@Bean
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager 
    manager) {
    ContentNegotiatingViewResolver resolver = new 
    ContentNegotiatingViewResolver();
    resolver.setContentNegotiationManager(manager);
    // Define all possible view resolvers
    List<ViewResolver> resolvers = new ArrayList<>();

    resolvers.add(excelViewResolver());
    resolvers.add(pdfViewResolver());

    resolver.setViewResolvers(resolvers);
    return resolver;
}

@Bean
public ViewResolver excelViewResolver() {
    return new ExcelViewResolver();
}

@Bean
public ViewResolver pdfViewResolver() {
    return new PdfViewResolver();
}

在我的控制器中:-

  @Controller
  public class MyController{

  @PostMapping(value = "/download", produces = {"application/pdf", 
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})
public String downloadData(Model model, @RequestBody 
someRequestView requestView) {
    getSpringVersion();
    model.addAttribute("someResultList", 
    someService.extractDataForDownload(requestView));
    return "";
}
}

我尝试了所有方法,但是当我按localhost:8080 / download.pdf->时,它给了我404找不到。请提出解决此问题的建议。

1 个答案:

答案 0 :(得分:0)

ExcelViewResolver在最新版本中已被弃用。以下是以简单方式生成Excel文档的示例。

首先,创建一个Excel生成器类。根据要求配置列。并从数据库表中检索数据。

下面是示例excel生成器类。

public class StateExcelGenerator {

    public static ByteArrayInputStream statesToExcel(List<State> states) throws IOException {

        String[] COLUMNs = { "Id", "Name", "Desc" };

        try (Workbook workbook = new HSSFWorkbook(); ByteArrayOutputStream out = new ByteArrayOutputStream();) {
            CreationHelper createHelper = workbook.getCreationHelper();

            Sheet sheet = workbook.createSheet("StateInfo");

            Font headerFont = workbook.createFont();
            headerFont.setBold(true);
            headerFont.setColor(IndexedColors.BLUE.getIndex());

            CellStyle headerCellStyle = workbook.createCellStyle();
            headerCellStyle.setFont(headerFont);

            // Row for Header
            Row headerRow = sheet.createRow(0);

            // Header
            for (int col = 0; col < COLUMNs.length; col++) {
                Cell cell = headerRow.createCell(col);
                cell.setCellValue(COLUMNs[col]);
                cell.setCellStyle(headerCellStyle);
            }

            // CellStyle for Age
            CellStyle ageCellStyle = workbook.createCellStyle();
            ageCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("#"));

            int rowIdx = 1;
            for (State state : states) {
                Row row = sheet.createRow(rowIdx++);

                row.createCell(0).setCellValue(state.getId());
                row.createCell(1).setCellValue(state.getName());
                row.createCell(2).setCellValue(state.getDesc());

            }

            workbook.write(out);
            return new ByteArrayInputStream(out.toByteArray());

        }

    }
}

现在,如下所示修改控制器以处理下载文件。

@Controller
@RequestMapping("/admin/state")
public class StateController {
    @RequestMapping(value = "/download/statedata.xlsx", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> excelCustomersReport() throws IOException {
    List<State> states = stateService.getData();

    ByteArrayInputStream in = StateExcelGenerator.statesToExcel(states);
    // return IOUtils.toByteArray(in);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Disposition", "attachment; filename=customers.xlsx");

    return ResponseEntity.ok().headers(headers).body(new InputStreamResource(in));
}
}

现在在您的视图页面中创建一个超链接,例如jsp文件,以访问上述控制器方法。

<a href="/admin/state/download/statedata.xlsx">Click here to download the file</a>

下面是下载的excel文件。

Sample Excel Download File