easyexcel Servlet.service() for servlet [dispatcherServlet] in context with path [/gdw-StoredEMS] threw exception [Could not find acceptable representation] with root cause org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

sshcrbum  于 6个月前  发布在  Spring
关注(0)|答案(1)|浏览(113)

版本

<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.6</version> </dependency>

异常代码

private void writeExcel(HttpServletResponse response, String fileName, List<?> excelList) {
        //todo 优化
        //todo 解决异常
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        try {
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        } catch (UnsupportedEncodingException e) {
            log.info("【编码异常===== '{}'", e);
        }
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        try {
            EasyExcel.write(response.getOutputStream(), BmsComparisonExcelReportDto.class).sheet("模板").doWrite(excelList);
        } catch (IOException e) {
            log.info("【写入excel失败】");
        }
    }

postman

报错

Servlet.service() for servlet [dispatcherServlet] in context with path [/gdw-StoredEMS] threw exception [Could not find acceptable representation] with root cause

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

#描述
postman设置了请求头 Accept为“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”,换高版本的easyexcel也解决不了

hjzp0vay

hjzp0vay1#

import com.alibaba.excel.EasyExcel;
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.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/api/excel")
public class ExcelController {

    @GetMapping("/download")
    public void downloadExcel(HttpServletResponse response) {
        List<BmsComparisonExcelReportDto> excelList = fetchData(); // 假设这是获取数据的方法
        writeExcel(response, "template", excelList);
    }

    private void writeExcel(HttpServletResponse response, String fileName, List<?> excelList) {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        } catch (UnsupportedEncodingException e) {
            log.info("【编码异常】 '{}'", e);
        }
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        try {
            EasyExcel.write(response.getOutputStream(), BmsComparisonExcelReportDto.class).sheet("模板").doWrite(excelList);
        } catch (IOException e) {
            log.info("【写入excel失败】", e);
        }
    }
    
    private List<BmsComparisonExcelReportDto> fetchData() {
        // 获取数据的逻辑
        return List.of(
            new BmsComparisonExcelReportDto(1, "数据1"),
            new BmsComparisonExcelReportDto(2, "数据2")
        );
    }
}

@Data
@AllArgsConstructor
public class BmsComparisonExcelReportDto {
    private Integer id;
    private String name;
}

相关问题