1

I have the following code

@RestController
public class TokenRefreshController {
    @Autowired
    TokenAuthenticationService tokenAuthenticationService;

    @ApiOperation(value = "Generate new authentication token using a refresh token")
    @RequestMapping(value = "/token/refresh",
            method = RequestMethod.POST)
    public void refreshToken(@RequestParam("refresh") String refreshToken, HttpServletResponse response) throws IOException {
        System.out.println("WHEN REFRESHING TOKEN THE RESPONSE IS COMMITTED :" + response.isCommitted());
        tokenAuthenticationService.refreshAuthToken(refreshToken,response);
    }
}

On the line where i am printing to console it is always true. So the response is already committed before even entering my Controller. How can I get a response that is not committed, because I am setting some headers and returning errors when needed in the refreshAuthToken method.

As it was mentioned in the comments below one of the filters might be committing the response, but i still have no idea how to stop the filter from doing that.

D.Tomov
  • 1,018
  • 2
  • 15
  • 36
  • can you please elaborate more or what is your expectation in details? – GauravRai1512 Nov 13 '18 at 09:11
  • I want to receive a refresh parameter and return a response with header inside it if successful or return an error if anything fails along the way. – D.Tomov Nov 13 '18 at 09:14
  • 1
    Perhaps one of the filters/interceptors that is invoked before your controller commits the response, i.e. tries to write something to the output stream. Do you know what filters get invoked before the request reaches the controller? – kkamenev Nov 13 '18 at 09:22
  • That is probably it, but i thought i had it setup so it ignores this endpoint. No idea how to fix it – D.Tomov Nov 13 '18 at 09:29
  • Did you Try this [link 1](https://stackoverflow.com/q/14392361/6650476) and [link 2](https://stackoverflow.com/questions/39725888/what-does-httpservletresponse-is-committed-mean) – Bala555 Nov 13 '18 at 09:31

2 Answers2

0

can you please try below approach but that is applicable for spring boot version.

You should use ResponseEntity object to return your data and createYourObject keyword which will consist an object which you will set.

@RestController
public class TokenRefreshController {
    @Autowired
    TokenAuthenticationService tokenAuthenticationService;

    @ApiOperation(value = "Generate new authentication token using a refresh token")
    @RequestMapping(value = "/token/refresh",
            method = RequestMethod.POST)
    public ResponseEntity<createYourObject> refreshToken(@RequestParam("refresh") String refreshToken, HttpServletResponse response) throws IOException {
        System.out.println("WHEN REFRESHING TOKEN THE RESPONSE IS COMMITTED :" + response.isCommitted());
        tokenAuthenticationService.refreshAuthToken(refreshToken,response);
        return new ResponseEntity<createYourObject>(yourObjectResponse,HttpStatus.CREATED);
    }
}
GauravRai1512
  • 834
  • 6
  • 14
  • I am pretty sure that would work, but my main idea is to be using HttpServletResponse because refreshAuthToken method is written in a such way to report errors. If you are telling me there is no other way ill have to rewrite the whole TokenAuthenticationService class. – D.Tomov Nov 13 '18 at 09:23
0

you can change RequestMapping url and use it to refresh token

Max
  • 1