Spring @RestController response is already committed
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
add a comment |
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
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
add a comment |
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
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
java spring spring-restcontroller spring-rest
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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);
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
add a comment |
you can change RequestMapping url and use it to refresh token
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
you can change RequestMapping url and use it to refresh token
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
add a comment |
you can change RequestMapping url and use it to refresh token
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
add a comment |
you can change RequestMapping url and use it to refresh token
you can change RequestMapping url and use it to refresh token
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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