7

I have a springboot api. Call to one of the endpoint from swagger throws 500 error but nothing is getting logged. When I tried debugging, I can see that the responseEntity is populated just before returning from the controller. Hence I feel the code worked fine. Even then I see 500 in swagger. Lack of logs is making is difficult to debug. Did anyone face similar issue? How did you resolve it?

endpoint looks like this: I have added the log statements for reference

@RequestMapping(value = "/details/id/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public @ResponseBody
    ResponseEntity<Details> getDetailsById(@PathVariable(name="id")  @ApiParam(value ="id", example = "1234", required = true) String id) {
        inputValidation.validateInputAndThrowErrorIfNecessary(id);
        if(isBlank(id)) throw new IllegalArgumentException();

        Details details = detailsService.getDetailsById(id);

        if(details == null){
            logger.debug("Not found response..................");
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        logger.debug("Just before posting the response for  : {}", id);
        ResponseEntity<Details> responseEntity = new ResponseEntity<>(details, HttpStatus.OK);
        logger.debug("JSON--->\n"+responseEntity.toString()); // i see <200 OK OK,class Details { blah blah } at this point in logs

        return responseEntity;
    }

When I run the curl, response is as below :

curl -v GET "http://host:port/api/details/id/111" -H "accept: application/json;charset=UTF-8"
* Rebuilt URL to: GET/
* Could not resolve host: GET
* Closing connection 0
curl: (6) Could not resolve host: GET
*   Trying host...
* TCP_NODELAY set
* Connected to host (host) port port (#1)
> GET /api/details/id/111 HTTP/1.1
> Host: host:port
> User-Agent: curl/7.55.1
> accept: application/json;charset=UTF-8
>
< HTTP/1.1 500
< Content-Type: text/html;charset=utf-8
< Content-Language: en
< Content-Length: 455
< Date: Wed, 27 May 2020 00:49:11 GMT
< Connection: close
<
<!doctype html><html lang="en"><head><title>HTTP Status 500 ÔÇô 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 ÔÇô Internal Server Error</h1></body></html>* Closing connection 1

np10
  • 109
  • 1
  • 10
  • Can you post endpoint code – Nilanka Manoj May 27 '20 at 00:11
  • edited the detail with the code – np10 May 27 '20 at 00:26
  • can you add a client request that logs the headers as well? like: `curl -v http://localhost:8080/foo` – stringy05 May 27 '20 at 00:27
  • curl -X GET "http://host:port/api/details/id/111" -H "accept: application/json;charset=UTF-8" – np10 May 27 '20 at 00:29
  • What I meant was actually run the curl with -v and add the output to the question. It will help to show if there's anything odd between the request and the response you get.. – stringy05 May 27 '20 at 00:37
  • please check now – np10 May 27 '20 at 00:55
  • the method return type is `ResponseEntity
    ` and the return type of `responseEntity` is `ResponseEntity`, however I would have thought that would fail compilation if that's an issue. Clearly there's something weird going on, I would have thought there would be logging from Spring Boot with an exception, however if there isn't you can use something like this to force logging: https://stackoverflow.com/questions/33744875/spring-boot-how-to-log-all-requests-and-responses-with-exceptions-in-single-pl
    – stringy05 May 27 '20 at 03:11
  • 2
    I'm having the same issue. Have you found a solution? – palindrom Apr 24 '21 at 16:20
  • After debugging I found I was getting a null pointer at one line. For some reason it was not getting thrown. I verified it by commenting the code which was throwing the NPE and then the code would work fine. A null checked fixed the problem for me. – np10 Apr 25 '21 at 21:58
  • That doesnt answer why the log wasnt show in the first place – Philip Rego May 02 '22 at 22:52
  • I stepped through the compiled Spring code and put a breakpoint by the exception – Philip Rego May 03 '22 at 14:15

0 Answers0