解决java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

解决java.lang.IllegalStateException: Cannot call sendError() after the response has been committed!

  • 在用spring boot写restful风格的接口时特别是写文件下载或文件导出时会碰到java.lang.IllegalStateException: Cannot call sendError() after the response has been committed这样的bug,很多人可能一脸困惑,就好奇为什么我文件都已经可以正常导出了,为什么在日志中还是会出现这样的报错呢?有强迫症的人 怎么解决呢?我试过用try catch,试过拦截器拦截异常,试过异常控制器拦截,都不行。
  • 原因

    (因为我在类的头部使用了@RestController注解),因为第一个输出是流的方式输出,并且输出成功后又把输出流给关闭了,所以自然就对第二个输出造成了影响!

  • 解决方法

只需要在你下载或导出接口return null即可

猜你喜欢

转载自blog.csdn.net/weixin_42945597/article/details/115001705