Spring Exception Handler 返回部分响应 - 也许是错误?
Posted
技术标签:
【中文标题】Spring Exception Handler 返回部分响应 - 也许是错误?【英文标题】:Spring Exception Handler returns partial response - maybe bug? 【发布时间】:2019-03-20 22:35:37 【问题描述】:我正在使用 spring boot 2.0.4 来构建一个 REST 接口。
我正在使用ExceptionHandler
来处理错误。
问题:
当序列化时引发异常时,杰克逊会抛出JsonMappingException
。这个异常我用一个默认异常处理程序(捕获所有异常)来处理。
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
@ExceptionHandler(Throwable.class)
public String handleThrowable(final Throwable throwable)
return "SERVER ERROR";
现在,当我返回大量对象时出现奇怪的行为,并且由于 RuntimeException 而失败,我没有得到 500
返回。但是我得到了一个200
,其中包含第一个序列化对象,然后是错误消息。
示例:
我已经准备了一个完整的问题示例: https://github.com/stefan0001/spring-exception-test/tree/master/src/main/java/test
调用时:
GET http://localhost:8080/test
预期结果:
状态:500
正文:SERVER ERROR
实际结果:
状态:200
正文:
["name":"0","throwException":false,"foo":"bar","name":"1","throwException":false,"foo":"bar","name":"2","throwException":false,"foo":"bar","name":"3","throwException":false,"foo":"bar","name":"4","throwException":false,"foo":"bar","name":"5","throwException":false,"foo":"bar","name":"6","throwException":false,"foo":"bar","name":"7","throwException":false,"foo":"bar","name":"8","throwException":false,"foo":"bar","name":"9","throwException":false,"foo":"bar","name":"10","throwException":false,"foo":"bar","name":"11","throwException":false,"foo":"bar","name":"12","throwException":false,"foo":"bar","name":"13","throwException":false,"foo":"bar","name":"14","throwException":false,"foo":"bar","name":"15","throwException":false,"foo":"bar","name":"16","throwException":false,"foo":"bar","name":"17","throwException":false,"foo":"bar","name":"18","throwException":false,"foo":"bar","name":"19","throwException":false,"foo":"bar","name":"20","throwException":false,"foo":"bar","name":"21","throwException":false,"foo":"bar","name":"22","throwException":false,"foo":"bar","name":"23","throwException":false,"foo":"bar","name":"24","throwException":false,"foo":"bar","name":"25","throwException":false,"foo":"bar","name":"26","throwException":false,"foo":"bar","name":"27","throwException":false,"foo":"bar","name":"28","throwException":false,"foo":"bar","name":"29","throwException":false,"foo":"bar","name":"30","throwException":false,"foo":"bar","name":"31","throwException":false,"foo":"bar","name":"32","throwException":false,"foo":"bar","name":"33","throwException":false,"foo":"bar","name":"34","throwException":false,"foo":"bar","name":"35","throwException":false,"foo":"bar","name":"36","throwException":false,"foo":"bar","name":"37","throwException":false,"foo":"bar","name":"38","throwException":false,"foo":"bar","name":"39","throwException":false,"foo":"bar","name":"40","throwException":false,"foo":"bar","name":"41","throwException":false,"foo":"bar","name":"42","throwException":false,"foo":"bar","name":"43","throwException":false,"foo":"bar","name":"44","throwException":false,"foo":"bar","name":"45","throwException":false,"foo":"bar","name":"46","throwException":false,"foo":"bar","name":"47","throwException":false,"foo":"bar","name":"48","throwException":false,"foo":"bar","name":"49","throwException":false,"foo":"bar","name":"50","throwException":false,"foo":"bar","name":"51","throwException":false,"foo":"bar","name":"52","throwException":false,"foo":"bar","name":"53","throwException":false,"foo":"bar","name":"54","throwException":false,"foo":"bar","name":"55","throwException":false,"foo":"bar","name":"56","throwException":false,"foo":"bar","name":"57","throwException":false,"foo":"bar","name":"58","throwException":false,"foo":"bar","name":"59","throwException":false,"foo":"bar","name":"60","throwException":false,"foo":"bar","name":"61","throwException":false,"foo":"bar","name":"62","throwException":false,"foo":"bar","name":"63","throwException":false,"foo":"bar","name":"64","throwException":false,"foo":"bar","name":"65","throwException":false,"foo":"bar","name":"66","throwException":false,"foo":"bar","name":"67","throwException":false,"foo":"bar","name":"68","throwException":false,"foo":"bar","name":"69","throwException":false,"foo":"bar","name":"70","throwException":false,"foo":"bar","name":"71","throwException":false,"foo":"bar","name":"72","throwException":false,"foo":"bar","name":"73","throwException":false,"foo":"bar","name":"74","throwException":false,"foo":"bar","name":"75","throwException":false,"foo":"bar","name":"76","throwException":false,"foo":"bar","name":"77","throwException":false,"foo":"bar","name":"78","throwException":false,"foo":"bar","name":"79","throwException":false,"foo":"bar","name":"80","throwException":false,"foo":"bar","name":"81","throwException":false,"foo":"bar","name":"82","throwException":false,"foo":"bar","name":"83","throwException":false,"foo":"bar","name":"84","throwException":false,"foo":"bar","name":"85","throwException":false,"foo":"bar","name":"86","throwException":false,"foo":"bar","name":"87","throwException":false,"foo":"bar","name":"88","throwException":false,"foo":"bar","name":"89","throwException":false,"foo":"bar","name":"90","throwException":false,"foo":"bar","name":"91","throwException":false,"foo":"bar","name":"92","throwException":false,"foo":"bar","name":"93","throwException":false,"foo":"bar","name":"94","throwException":false,"foo":"bar","name":"95","throwException":false,"foo":"bar","name":"96","throwException":false,"foo":"bar","name":"97","throwException":false,"foo":"bar","name":"98","throwException":false,"foo":"bar","name":"99","throwException":false,"foo":"bar","name":"100","throwException":false,"foo":"bar","name":"101","throwException":false,"foo":"bar","name":"102","throwException":false,"foo":"bar","name":"103","throwException":false,"foo":"bar","name":"104","throwException":false,"foo":"bar","name":"105","throwException":false,"foo":"bar","name":"106","throwException":false,"foo":"bar","name":"107","throwException":false,"foo":"bar","name":"108","throwException":false,"foo":"bar","name":"109","throwException":false,"foo":"bar","name":"110","throwException":false,"foo":"bar","name":"111","throwException":false,"foo":"bar","name":"112","throwException":false,"foo":"bar","name":"113","throwException":false,"foo":"bar","name":"114","throwException":false,"foo":"bar","name":"115","throwException":false,"foo":"bar","name":"116","throwException":false,"foo":"bar","name":"117","throwException":false,"foo":"bar","name":"118","throwException":false,"foo":"bar","name":"119","throwException":false,"foo":"bar","name":"120","throwException":false,"foo":"bar","name":"121","throwException":false,"foo":"bar","name":"122","throwException":false,"foo":"bar","name":"123","throwException":false,"foo":"bar","name":"124","throwException":false,"foo":"bar","name":"125","throwException":false,"foo":"bar","name":"126","throwException":false,"foo":"bar","name":"127","throwException":false,"foo":"bar","name":"128","throwException":false,"foo":"bar","name":"129","throwException":false,"foo":"bar","name":"130","throwException":false,"foo":"bar","name":"131","throwException":false,"foo":"bar","name":"132","throwException":false,"foo":"bar","name":"133","throwException":false,"foo":"bar","name":"134","throwException":false,"foo":"bar","name":"135","throwException":false,"foo":"bar","name":"136","throwException":false,"foo":"bar","name":"137","throwException":false,"foo":"bar","name":"138","throwException":false,"foo":"bar","name":"139","throwException":false,"foo":"bar","name":"140","throwException":false,"foo":"bar","name":"141","throwException":false,"foo":"bar","name":"142","throwException":false,"foo":"bar","name":"143","throwException":false,"foo":"bar","name":"144","throwException":false,"foo":"bar","name":"145","throwException":false,"foo":"bar","name":"146","throwException":false,"foo":"bar","name":"147","throwException":false,"foo":"bar","name":"148","throwException":false,"foo":"bar","name":"149","throwException":false,"foo":"bar","name":"150","throwException":false,"foo":"bar","name":"151","throwException":false,"foo":"bar","name":"152","throwException":false,"foo":"bar","name":"153","throwException":false,"foo":"bar","name":"154","throwException":false,"foo":"bar","name":"155","throwException":false,"foo":"bar","name":"156","throwException":false,"foo":"bar","name":"157","throwException":false,"foo":"bar","name":"158","throwException":false,"foo":"bar","name":"159","throwException":false,"foo":"bar","name":"160","throwException":false,"foo":"bar","name":"161","throwException":false,"foo":"bar","name":"162","throwException":false,"foo":"bar","name":"163","throwException":false,"foo":"bar","name":"164","throwException":false,"foo":"bar","name":"165","throwException":false,"foo":"bar","name":"166","throwException":false,"foo":"bar","name":"167","throwException":false,"foo":"bar","name":"168","throwException":false,"foo":"bar","name":"169","throwException":false,"foo":"bar","name":"170","throwException":false,"foo":"bar","name":"171","throwException":false,"foo":"bar","name":"172","throwException":false,"foo":"bar","name":"173","throwException":false,"foo":"bar","name":"174","throwException":false,"foo":"bar","name":"175","throwException":false,"foo":"bar","name":"176","throwException":false,"foo":"bar","name":"177","throwException":false,"foo":"bar","name":"178","throwException":false,"foo":"bar","name":"179","throwException":false,"foo":"bar","name":"180","throwException":false,"foo":"bar","name":"181","throwException":false,"foo":"bar","name":"182","throwException":false,"foo":"bar","name":"183","throwException":false,"foo":"bar","name":"184","throwException":false,"foo":"bar","name":"185","throwException":false,"foo":"bar","name":"186","throwException":false,"foo":"bar","name":"187","throwException":false,"foo":"bar","name":"188","throwException":false,"foo":"bar","name":"189","throwException":false,"foo":"bar","name":"190","throwException":false,"foo":"bar","name":"191","throwException":false,"foo":"bar","name":"192","throwException":false,"foo":"bar","name":"193","throwException":false,"foo":"bar","name":"194","throwException":false,"foo":"bar","name":"195","throwException":false,"foo":"bar","name":"196","throwException":false,"foo":"bar","name":"197","throwException":false,"foo":"bar","name":"198","throwException":false,"foo":"bar","name":"199","throwException":false,"foo":"bar","name":"200","throwException":false,"foo":"bar","name":"201","throwException":false,"foo":"bar","name":"202","throwException":false,"foo":"bar","name":"203","throwException":false,"foo":"bar","name":"204","throwException":false,"foo":"bar","name":"205","throwException":false,"foo":"bar","name":"206","throwException":false,"foo":"bar","name":"207","throwException":false,"foo":"bar","name":"208","throwException":false,"foo":"bar","name":"209","throwException":false,"foo":"bar","name":"210","throwException":false,"foo":"bar","name":"211","throwException":false,"foo":"bar","name":"212","throwException":false,"foo":"bar","name":"213","throwException":false,"foo":"bar","name":"214","throwException":false,"foo":"bar","name":"215","throwException":false,"foo":"bar","name":"216","throwException":false,"foo":"bar","name":"217","throwException":false,"foo":"bar","name":"218","throwException":false,"foo":"bar","name":"219","throwException":false,"foo":"bar","name":"220","throwException":false,"foo":"bar","name":"221","throwException":false,"foo":"bar","name":"222","throwException":false,"foo":"bar","name":"223","throwException":false,"foo":"bar","name":"224","throwException":false,"foo":"bar","name":"225","throwException":false,"foo":"bar","name":"226","throwException":false,"foo":"bar","name":"227","throwException":false,"foo":"bar","name":"228","throwException":false,"foo":"bar","name":"229","throwException":false,"foo":"bar","name":"230","throwException":false,"foo":"bar","name":"231","throwException":false,"foo":"bar","name":"232","throwException":false,"foo":"bar","name":"233","throwException":false,"foo":"bar","name":"234","throwException":false,"foo":"bar","name":"235","throwException":false,"foo":"bar","name":"236","throwException":false,"foo":"bar","name":"237","throwException":false,"foo":"bar","name":"238","throwException":false,"foo":"bar","name":"239","throwException":false,"foo":"bar","name":"240","throwException":false,"foo":"bar","name":"241","throwException":false,"foo":"bar","name":"242","throwException":false,"foo":"bar","name":"243","throwException":false,"foo":"bar","name":"244","throwException":false,"foo":"bar","name":"245","throwException":false,"foo":"bar","name":"246","throwException":false,"foo":"bar","name":"247","throwException":false,"foo":"bar","name":"248","throwException":false,"foo":"bar","name":"249","throwException":false,"foo":"bar","name":"250","throwException":false,"foo":"bar","name":"251","throwException":false,"foo":"bar","name":"252","throwException":false,"foo":"bar","name":"253","throwException":false,"foo":"bar","name":"254","throwException":false,"foo":"bar","name":"255","throwException":false,"foo":"bar","name":"256","throwException":false,"foo":"bar","name":"257","throwException":false,"foo":"bar","name":"258","throwException":false,"foo":"bar","name":"259","throwException":false,"foo":"bar","name":"260","throwException":false,"foo":"bar","name":"261","throwException":false,"foo":"bar","name":"262","throwException":false,"foo":"bar","name":"263","throwException":false,"foo":"bar","name":"264","throwException":false,"foo":"bar","name":"265","throwException":false,"foo":"bar","name":"266","throwException":false,"foo":"bar","name":"267","throwException":false,"foo":"bar","name":"268","throwException":false,"foo":"bar","name":"269","throwException":false,"foo":"bar","name":"270","throwException":false,"foo":"bar","name":"271","throwException":false,"foo":"bar","name":"272","throwException":false,"foo":"bar","name":"273","throwException":false,"foo":"bar","name":"274","throwException":false,"foo":"bar","name":"275","throwException":false,"foo":"bar","name":"276","throwException":false,"foo":"bar","name":"277","throwException":false,"foo":"bar","name":"278","throwException":false,"foo":"bar","name":"279","throwException":false,"foo":"bar","name":"280","throwException":false,"foo":"bar","name":"281","throwException":false,"foo":"bar","name":"282","throwException":false,"foo":"bar","name":"283","throwException":false,"foo":"bar","name":"284","throwException":false,"foo":"bar","name":"285","throwException":false,"foo":"bar","name":"286","throwException":false,"foo":"bar","name":"287","throwException":false,"foo":"bar","name":"288","throwException":false,"foo":"bar","name":"289","throwException":false,"foo":"bar","name":"290","throwException":false,"foo":"bar","name":"291","throwException":false,"foo":"bar","name":"292","throwException":false,"foo":"bar","name":"293","throwException":false,"foo":"bar","name":"294","throwException":false,"foo":"bar","name":"295","throwException":false,"foo":"bar","name":"296","throwException":false,"foo":"bar","name":"297","throwException":false,"foo":"bar","name":"298","throwException":false,"foo":"bar","name":"299","throwException":false,"foo":"bar","name":"300","throwException":false,"foo":"bar","name":"301","throwException":false,"foo":"bar","name":"302","throwException":false,"foo":"bar","name":"303","throwException":false,"foo":"bar","name":"304","throwException":false,"foo":"bar","name":"305","throwException":false,"foo":"bar","name":"306","throwException":false,"foo":"bar","name":"307","throwException":false,"foo":"bar","name":"308","throwException":false,"foo":"bar","name":"309","throwException":false,"foo":"bar","name":"310","throwException":false,"foo":"bar","name":"311","throwException":false,"foo":"bar","name":"312","throwException":false,"foo":"bar","name":"313","throwException":false,"foo":"bar","name":"314","throwException":false,"foo":"bar","name":"315","throwException":false,"foo":"bar","name":"316","throwException":false,"foo":"bar","name":"317","throwException":false,"foo":"bar","name":"318","throwException":false,"foo":"bar","name":"319","throwException":false,"foo":"bar","name":"320","throwException":false,"foo":"bar","name":"321","throwException":false,"foo":"bar","name":"322","throwException":false,"foo":"bar","name":"323","throwException":false,"foo":"bar","name":"324","throwException":false,"foo":"bar","name":"325","throwException":false,"foo":"bar","name":"326","throwException":false,"foo":"bar","name":"327","throwException":false,"foo":"bar","name":"328","throwException":false,"foo":"bar","name":"329","throwException":false,"foo":"bar","name":"330","throwException":false,"foo":"bar","name":"331","throwException":false,"foo":"bar","name":"332","throwException":false,"foo":"bar","name":"333","throwException":false,"foo":"bar","name":"334","throwException":false,"foo":"bar","name":"335","throwException":false,"foo":"bar","name":"336","throwException":false,"foo":"bar","name":"337","throwException":false,"foo":"bar","name":"338","throwException":false,"foo":"bar","name":"339","throwException":false,"foo":"bar","name":"340","throwException":false,"foo":"bar","name":"341","throwException":false,"foo":"bar","name":"342","throwException":false,"foo":"bar","name":"343","throwException":false,"foo":"bar","name":"344","throwException":false,"foo":"bar","name":"345","throwException":false,"foo":"bar","name":"346","throwException":false,"foo":"bar","name":"347","throwException":false,"foo":"bar","name":"348","throwException":false,"foo":"bar","name":"349","throwException":false,"foo":"bar","name":"350","throwException":false,"foo":"bar","name":"351","throwException":false,"foo":"bar","name":"352","throwException":false,"foo":"bar","name":"353","throwException":false,"foo":"bar","name":"354","throwException":false,"foo":"bar","name":"355","throwException":false,"foo":"bar","name":"356","throwException":false,"foo":"bar","name":"357","throwException":false,"foo":"bar","name":"358","throwException":false,"foo":"bar","name":"359","throwException":false,"foo":"bar","name":"360","throwException":false,"foo":"bar","name":"361","throwException":false,"foo":"bar","name":"362","throwException":false,"foo":"bar","name":"363","throwException":false,"foo":"bar","name":"364","throwException":false,"foo":"bar","name":"365","throwException":false,"foo":"bar","name":"366","throwException":false,"foo":"bar","name":"367","throwException":false,"foo":"bar","name":"368","throwException":false,"foo":"bar","name":"369","throwException":false,"foo":"bar","name":"370","throwException":false,"foo":"bar","name":"371","throwException":false,"foo":"bar","name":"372","throwException":false,"foo":"bar","name":"373","throwException":false,"foo":"bar","name":"374","throwException":false,"foo":"bar","name":"375","throwException":false,"foo":"bar","name":"376","throwException":false,"foo":"bar","name":"377","throwException":false,"foo":"bar","name":"378","throwException":false,"foo":"bar","name":"379","throwException":false,"foo":"bar","name":"380","throwException":false,"foo":"bar","name":"381","throwException":false,"foo":"bar","name":"382","throwException":false,"foo":"bar","name":"383","throwException":false,"foo":"bar","name":"384","throwException":false,"foo":"bar","name":"385","throwException":false,"foo":"bar","name":"386","throwException":false,"foo":"bar","name":"387","throwException":false,"foo":"bar","name":"388","throwException":false,"foo":"bar","name":"389","throwException":false,"foo":"bar","name":"390","throwException":false,"foo":"bar","name":"391","throwException":false,"foo":"bar","name":"392","throwException":false,"foo":"bar","name":"393","throwException":false,"foo":"bar","name":"394","throwException":false,"foo":"bar","name":"395","throwException":false,"foo":"bar","name":"396","throwException":false,"foo":"bar","name":"397","throwException":false,"foo":"bar","name":"398","throwException":false,"foo":"bar","name":"399","throwException":false,"foo":"bar","name":"400","throwException":false,"foo":"bar","name":"401","throwException":false,"foo":"bar","name":"402","throwException":false,"foo":"bar","name":"403","throwException":false,"foo":"bar","name":"404","throwException":false,"foo":"bar","name":"405","throwException":false,"foo":"bar","name":"406","throwException":false,"foo":"bar","name":"407","throwException":false,"foo":"bar","name":"408","throwException":false,"foo":"bar","name":"409","throwException":false,"foo":"bar","name":"410","throwException":false,"foo":"bar","name":"411","throwException":false,"foo":"bar","name":"412","throwException":false,"foo":"bar","name":"413","throwException":false,"foo":"bar","name":"414","throwException":false,"foo":"bar","name":"415","throwException":false,"foo":"bar","name":"416","throwException":false,"foo":"bar","name":"417","throwException":false,"foo":"bar","name":"418","throwException":false,"foo":"bar","name":"419","throwException":false,"foo":"bar","name":"420","throwException":false,"foo":"bar","name":"421","throwException":false,"foo":"bar","name":"422","throwException":false,"foo":"bar","name":"423","throwException":false,"foo":"bar","name":"424","throwException":false,"foo":"bar","name":"425","throwException":false,"foo":"bar","name":"426","throwException":false,"foo":"bar","name":"427","throwException":false,"foo":"bar","name":"428","throwException":false,"foo":"bar","name":"429","throwException":false,"foo":"bar","name":"430","throwException":false,"foo":"bar","name":"431","throwException":false,"foo":"bar","name":"432","throwException":false,"foo":"bar","name":"433","throwException":false,"foo":"bar","name":"434","throwException":false,"foo":"bar","name":"435","throwException":false,"foo":"bar","name":"436","throwException":false,"foo":"bar","name":"437","throwException":false,"foo":"bar","name":"438","throwException":false,"foo":"bar","name":"439","throwException":false,"foo":"bar","name":"440","throwException":false,"foo":"bar","name":"441","throwException":false,"foo":"bar","name":"442","throwException":false,"foo":"bar","name":"443","throwException":false,"foo":"bar","name":"444","throwException":false,"foo":"bar","name":"445","throwException":false,"foo":"bar","name":"446","throwException":false,"foo":"bar","name":"447","throwException":false,"foo":"bar","name":"448","throwException":false,"foo":"bar","name":"449","throwException":false,"foo":"bar","name":"450","throwException":false,"foo":"bar","name":"451","throwException":false,"foo":"bar","name":"452","throwException":false,"foo":"bar","name":"453","throwException":false,"foo":"bar","name":"454","throwException":false,"foo":"bar","name":"455","throwException":false,"foo":"bar","name":"456","throwException":false,"foo":"bar","name":"457","throwException":false,"foo":"bar","name":"458","throwException":false,"foo":"bar","name":"459","throwException":false,"foo":"bar","name":"460","throwException":false,"foo":"bar","name":"461","throwException":false,"foo":"bar","name":"462","throwException":false,"foo":"bar","name":"463","throwException":false,"foo":"bar","name":"464","throwException":false,"foo":"bar","name":"465","throwException":false,"foo":"bar","name":"466","throwException":false,"foo":"bar","name":"467","throwException":false,"foo":"bar","name":"468","throwException":false,"foo":"bar","name":"469","throwException":false,"foo":"bar","name":"470","throwException":false,"foo":"bar","name":"471","throwException":false,"foo":"bar","name":"472","throwException":false,"foo":"bar","name":"473","throwException":false,"foo":"bar","name":"474","throwException":false,"foo":"bar","name":"475","throwException":false,"foo":"bar","name":"476","throwException":false,"foo":"bar","name":"477","throwException":false,"foo":"bar","name":"478","throwException":false,"foo":"bar","name":"479","throwException":false,"foo":"bar","name":"480","throwException":false,"foo":"bar","name":"481","throwException":false,"foo":"bar","name":SERVER ERROR
查看响应正文末尾的SERVER ERROR
。
我不知道这是错误还是故意。 看起来回复已经写好了或类似的东西......
参考资料:
SPRING JIRA ISSUE
GITHUB EXAMPLE
【问题讨论】:
你能分享控制器并请求有效载荷吗? 你可以在 git repo 中找到控制器。他们没有请求有效负载:github.com/stefan0001/spring-exception-test/blob/master/src/… 【参考方案1】:一旦提交了 HTTP 响应,就无法更改 HTTP 状态或标头。那时,该响应可能位于网络缓冲区中,甚至已经发送到客户端。到那时,就没有办法收回了。
所以在这种情况下,Spring 无法更改响应。
从客户端的角度来看,如果响应不完整(例如,它的长度反映了Content-Length
标头),则为then the RFC says the client should close the connection and consider the response incomplete。
【讨论】:
以上是关于Spring Exception Handler 返回部分响应 - 也许是错误?的主要内容,如果未能解决你的问题,请参考以下文章
Spring Exception Handler 返回部分响应 - 也许是错误?
HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: jav
spring boot GlobalExceptionHandler @RestControllerAdvice @ExceptionHandler