带有查询参数的弹簧支架控制器的返回响应

时间:2019-02-08 14:17:29

标签: java spring controller query-parameters

我想发送带有redirectUri和查询参数的302响应。我可以填写位置,但不确定如何响应控制器将值包括为查询参数。

我尝试将值添加到正文,但我想通过查询参数发送它们。

public ResponseEntity<Void> redirect(@RequestBody MultiValueMap<String,String> formVars, HttpServletResponse response) {
    String clientId = formVars.getFirst(CLIENT_ID);
    String redirectUri = formVars.getFirst(REDIRECT_URI);
    String redirectToken = formVars.getFirst(REDIRECTION_TOKEN);

    ResponseHandler<RedirectDto> redirectDtoResponseHandler = redirectService.redeemRedirectToken(redirectToken);
    if (!redirectDtoResponseHandler.isPresent()) {
        //handle error
    }

    redirectDtoResponseHandler.map(redirectDto -> toCookie(redirectDto.getCustomerSessionId()))
        .ifPresent(response::addCookie);

    URI uri = UriComponentsBuilder.fromHttpUrl(redirectUri).build().toUri();

    //I'd like to add clientId as query param in this response?
    return ResponseEntity.status(HttpStatus.FOUND).location(uri).build();
}

我想在此响应中将clientId添加为查询参数吗?

1 个答案:

答案 0 :(得分:1)

UriComponents uriComponents = UriComponentsBuilder.newInstance()
      .scheme("http").host(redirectUri)
      .path("/").query("clientId={keyword}").buildAndExpand(clientId);