user8575781
user8575781

Reputation:

Redirecting by setting the location in the header

I have a method in the REST API which, after a call, should set the address for forwarding

 UriComponents uriComponents = uriComponentsBuilder.path("/signIn").build();

This is not the case, however. This is my method

@RestController
@PreAuthorize("permitAll()")
@RequestMapping(value = "/register")
@Api(value = "Register API", description = "Provides a list of methods for registration")
public class RegisterRestController {

     @ApiOperation(value = "Activate the user with token")
     @RequestMapping(value = "/thanks", method = RequestMethod.GET)
     public
     ResponseEntity<?> confirmAccount(
        @RequestParam("token") String token,
        UriComponentsBuilder uriComponentsBuilder
     ) throws URISyntaxException {
          Optional<User> userOptional = userService.findByActivationToken(token);

          if(userOptional.isPresent()) {
             User user = userOptional.get();

             user.setActivationToken(null);
             user.setEnabled(true);

             userService.saveUser(user);
          } else {
             throw new ActivationTokenNotFoundException();
          }

          UriComponents uriComponents = uriComponentsBuilder.path("/signIn").build();

          return ResponseEntity.created(uriComponents.toUri()).build();
    }
}

There is no redirection after calling the page address https://zapodaj.net/fa61bfc5732f3.png.html I did it exactly the same as it is on this topic on Stack Overflow enter link description here. Address calls a link from the mailbox https://zapodaj.net/e0e5eb8ad52bf.png.html.

Upvotes: 1

Views: 3765

Answers (1)

Strelok
Strelok

Reputation: 51461

Browser will not redirect on 201 Created response. You should use a permanent redirect (301 Moved Permanently).

return ResponseEntity.status(301).location(uriComponents.toUri()).build();

201 response code also does not make a lot of sense for GET method, it is usually used as a response to an Ajax PUT method request to an API endpoint.

Upvotes: 1

Related Questions