通过在标题

时间:2017-09-10 13:54:55

标签: spring rest spring-mvc spring-boot

我在REST API中有一个方法,在调用之后,应该设置转发地址

 UriComponents uriComponents = uriComponentsBuilder.path("/signIn").build();
但是,情况并非如此。这是我的方法

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

调用页面地址https://zapodaj.net/fa61bfc5732f3.png.html后没有重定向 我在Stack Overflow enter link description here上完成了与此主题完全相同的操作。地址从邮箱https://zapodaj.net/e0e5eb8ad52bf.png.html调用链接。

1 个答案:

答案 0 :(得分:1)

浏览器不会重定向201 Created响应。您应该使用永久重定向(301 Moved Permanently)。

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

201响应代码对GET方法也没有多大意义,它通常用作对API端点的Ajax PUT方法请求的响应。