No converter for XXX with preset Content-Type 'application/octet-stream'

org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class ReturnObj] with preset Content-Type 'application/octet-stream'

参考:https://blog.csdn.net/qq_44137046/article/details/116092565

本次的接口设计为导入解析接口,如果导入信息不符合,返回一个下载文件(application/octet-stream),符合则返回货物集合信息(application/json)。

参考上面的链接内容,确实是return null 就能解决问题。

原代码将return null写在service层,到controller层会仍然return ReturnObj,仍然会报错,所以将代码搬到controller层不会报错:

@ApiOperation(value = "货物信息批量导入")

@PostMapping(value = "/readCargosExcel")

public ReturnObj<List<TCargoVO>> readExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException

{

return ReturnObj.success(tCargoService.readCargosExcel(file,request,response));

}

修改后:

@ApiOperation(value = "货物信息批量导入")

@PostMapping(value = "/readCargosExcel")

public ReturnObj<List<TCargoVO>> readExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws IOException, MyException {

    ExcelImport<TCargoVO> excelImport = ExcelImport.create(file, TCargoVO::new);

    List<TCargoVO> list = excelImport.readAll();

    if (false) {

        excelImport.response(response);

        return null;

    }

    return ReturnObj.success(list);

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容