springboot API throws 500 error but no error is logged
Asked Answered
T

1

9

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

Trophoblast answered 27/5, 2020 at 0:9 Comment(11)
Can you post endpoint codePurely
edited the detail with the codeTrophoblast
can you add a client request that logs the headers as well? like: curl -v http://localhost:8080/fooAdlai
curl -X GET "host:port/api/details/id/111" -H "accept: application/json;charset=UTF-8"Trophoblast
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..Adlai
please check nowTrophoblast
the method return type is ResponseEntity<Details> and the return type of responseEntity is ResponseEntity<FullDetails>, 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: #33745375Adlai
I'm having the same issue. Have you found a solution?Calvinna
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.Trophoblast
That doesnt answer why the log wasnt show in the first placeColugo
I stepped through the compiled Spring code and put a breakpoint by the exceptionColugo
B
1

I had this problem while declaring a @PathVariable with no correspondence on the @RequestMapping url (thanks to a typo...)

Its not exactly your case (your @PathVariable exists on the url), but may be a the similar thing (two configurations that should match to work are not matching...)

Burdened answered 22/9, 2023 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.