Spring @RestController response is already committed










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.










share|improve this question
























  • can you please elaborate more or what is your expectation in details?

    – GauravRai1512
    Nov 13 '18 at 9: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 9: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 9: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 9:29











  • Did you Try this link 1 and link 2

    – Bala555
    Nov 13 '18 at 9:31















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.










share|improve this question
























  • can you please elaborate more or what is your expectation in details?

    – GauravRai1512
    Nov 13 '18 at 9: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 9: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 9: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 9:29











  • Did you Try this link 1 and link 2

    – Bala555
    Nov 13 '18 at 9:31













1












1








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.










share|improve this question
















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.







java spring spring-restcontroller spring-rest






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:30







D.Tomov

















asked Nov 13 '18 at 9:07









D.TomovD.Tomov

1048




1048












  • can you please elaborate more or what is your expectation in details?

    – GauravRai1512
    Nov 13 '18 at 9: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 9: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 9: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 9:29











  • Did you Try this link 1 and link 2

    – Bala555
    Nov 13 '18 at 9:31

















  • can you please elaborate more or what is your expectation in details?

    – GauravRai1512
    Nov 13 '18 at 9: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 9: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 9: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 9:29











  • Did you Try this link 1 and link 2

    – Bala555
    Nov 13 '18 at 9:31
















can you please elaborate more or what is your expectation in details?

– GauravRai1512
Nov 13 '18 at 9:11





can you please elaborate more or what is your expectation in details?

– GauravRai1512
Nov 13 '18 at 9: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 9:14





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 9:14




1




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 9:22





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 9: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 9:29





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 9:29













Did you Try this link 1 and link 2

– Bala555
Nov 13 '18 at 9:31





Did you Try this link 1 and link 2

– Bala555
Nov 13 '18 at 9:31












2 Answers
2






active

oldest

votes


















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);







share|improve this answer























  • 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 9:23


















0














you can change RequestMapping url and use it to refresh token






share|improve this answer


















  • 1





    Please structure your answer more clearly. I have no idea what you are saying.

    – D.Tomov
    Nov 13 '18 at 9:27











  • @RequestMapping(value = "/token/myrefresh"

    – Max
    Nov 13 '18 at 9:28










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277370%2fspring-restcontroller-response-is-already-committed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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);







share|improve this answer























  • 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 9:23















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);







share|improve this answer























  • 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 9:23













0












0








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);







share|improve this answer













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);








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 9:20









GauravRai1512GauravRai1512

58811




58811












  • 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 9:23

















  • 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 9:23
















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 9:23





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 9:23













0














you can change RequestMapping url and use it to refresh token






share|improve this answer


















  • 1





    Please structure your answer more clearly. I have no idea what you are saying.

    – D.Tomov
    Nov 13 '18 at 9:27











  • @RequestMapping(value = "/token/myrefresh"

    – Max
    Nov 13 '18 at 9:28















0














you can change RequestMapping url and use it to refresh token






share|improve this answer


















  • 1





    Please structure your answer more clearly. I have no idea what you are saying.

    – D.Tomov
    Nov 13 '18 at 9:27











  • @RequestMapping(value = "/token/myrefresh"

    – Max
    Nov 13 '18 at 9:28













0












0








0







you can change RequestMapping url and use it to refresh token






share|improve this answer













you can change RequestMapping url and use it to refresh token







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 9:24









MaxMax

1




1







  • 1





    Please structure your answer more clearly. I have no idea what you are saying.

    – D.Tomov
    Nov 13 '18 at 9:27











  • @RequestMapping(value = "/token/myrefresh"

    – Max
    Nov 13 '18 at 9:28












  • 1





    Please structure your answer more clearly. I have no idea what you are saying.

    – D.Tomov
    Nov 13 '18 at 9:27











  • @RequestMapping(value = "/token/myrefresh"

    – Max
    Nov 13 '18 at 9:28







1




1





Please structure your answer more clearly. I have no idea what you are saying.

– D.Tomov
Nov 13 '18 at 9:27





Please structure your answer more clearly. I have no idea what you are saying.

– D.Tomov
Nov 13 '18 at 9:27













@RequestMapping(value = "/token/myrefresh"

– Max
Nov 13 '18 at 9:28





@RequestMapping(value = "/token/myrefresh"

– Max
Nov 13 '18 at 9:28

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277370%2fspring-restcontroller-response-is-already-committed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto