springboot API 抛出 500 错误但没有记录错误

np1*_*p10 9 internal-server-error spring-boot

我有一个 springboot api。从 swagger 调用端点之一会抛出 500 错误,但没有记录任何内容。当我尝试调试时,我可以看到在从控制器返回之前填充了responseEntity。因此我觉得代码工作得很好。即便如此,我还是看到 500 神气十足。缺乏日志使得调试变得困难。有人遇到类似的问题吗?你是怎么解决的?

\n\n

端点看起来像这样:我添加了日志语句以供参考

\n\n
@RequestMapping(value = "/details/id/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})\n    public @ResponseBody\n    ResponseEntity<Details> getDetailsById(@PathVariable(name="id")  @ApiParam(value ="id", example = "1234", required = true) String id) {\n        inputValidation.validateInputAndThrowErrorIfNecessary(id);\n        if(isBlank(id)) throw new IllegalArgumentException();\n\n        Details details = detailsService.getDetailsById(id);\n\n        if(details == null){\n            logger.debug("Not found response..................");\n            return new ResponseEntity<>(HttpStatus.NOT_FOUND);\n        }\n        logger.debug("Just before posting the response for  : {}", id);\n        ResponseEntity<Details> responseEntity = new ResponseEntity<>(details, HttpStatus.OK);\n        logger.debug("JSON--->\\n"+responseEntity.toString()); // i see <200 OK OK,class Details { blah blah } at this point in logs\n\n        return responseEntity;\n    }\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我运行curl时,响应如下:

\n\n
curl -v GET "http://host:port/api/details/id/111" -H "accept: application/json;charset=UTF-8"\n* Rebuilt URL to: GET/\n* Could not resolve host: GET\n* Closing connection 0\ncurl: (6) Could not resolve host: GET\n*   Trying host...\n* TCP_NODELAY set\n* Connected to host (host) port port (#1)\n> GET /api/details/id/111 HTTP/1.1\n> Host: host:port\n> User-Agent: curl/7.55.1\n> accept: application/json;charset=UTF-8\n>\n< HTTP/1.1 500\n< Content-Type: text/html;charset=utf-8\n< Content-Language: en\n< Content-Length: 455\n< Date: Wed, 27 May 2020 00:49:11 GMT\n< Connection: close\n<\n<!doctype html><html lang="en"><head><title>HTTP Status 500 \xc3\x94\xc3\x87\xc3\xb4 Internal Server Error</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 500 \xc3\x94\xc3\x87\xc3\xb4 Internal Server Error</h1></body></html>* Closing connection 1\n\n
Run Code Online (Sandbox Code Playgroud)\n

小智 0

我在声明 @PathVariable 时遇到了这个问题,而 @RequestMapping url 上没有对应信息(由于拼写错误......)

这不完全是你的情况(你的@PathVariable存在于url上),但可能是类似的事情(应该匹配工作的两个配置不匹配......)