Spring @RestController响应已经提交

时间:2018-11-13 09:07:34

标签: java spring spring-restcontroller spring-rest

我有以下代码

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

在我要打印到控制台的那一行上,它总是正确的。因此,即使在进入我的控制器之前,响应已经提交。我如何获得未提交的响应,因为我在refreshAuthToken方法中设置了一些标头并在需要时返回错误。

正如在下面的评论中提到的那样,其中一个过滤器可能正在提交响应,但是我仍然不知道如何阻止过滤器执行该操作。

2 个答案:

答案 0 :(得分:0)

可以请您尝试以下方法,但这仅适用于Spring Boot版本。

您应该使用ResponseEntity对象返回数据并使用createYourObject关键字,该关键字将包含要设置的对象。

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

答案 1 :(得分:0)

您可以更改RequestMapping网址并将其用于刷新令牌