美文网首页
No converter for XXX with preset

No converter for XXX with preset

作者: 全新的牙膏 | 来源:发表于2023-08-08 11:43 被阅读0次

    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);

    }

    相关文章

      网友评论

          本文标题:No converter for XXX with preset

          本文链接:https://www.haomeiwen.com/subject/cnwomrtx.html