首页 > 解决方案 > DispatcherServlet:在 localhost 的页面上重定向

问题描述

我正在尝试测试我的@RestController 重定向能力。

对于测试目标,它必须在 localhost 上重定向。

@RestController
@RequestMapping(value = RegistrationRestController.CONFIRM_REGISTRATION_URL, produces = 
MediaType.APPLICATION_JSON_VALUE)
public class RegistrationRestController {

public static final String CONFIRM_REGISTRATION_URL = "/confirmRegistration";

@Autowired
UserService userService;

@GetMapping
public RedirectView confirmRegistration(@RequestParam("token") String token) {
    RedirectView redirectView;
    Token verificationToken = userService.getToken(token);
    if ((verificationToken == null) || (verificationToken.getExpiryDateTime().isAfter(LocalDateTime.now()))) {
        redirectView = new RedirectView("access_denied");
    } else redirectView = new RedirectView("access_approved");

    return redirectView;
    }

 }

Servlet 映射为:

  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

我运行应用程序并输入:

 http://localhost:8080/test/confirmRegistration?token=a5582f4c-db3c-4d7c-9fab-7fd82ffb5d89

但我收到 404 错误。

当我使用真实站点的“access_approved”绝对路径(例如“ https://stackoverflow.com/ ”)代替它时,它运行良好。

因此,问题集中在位于 my-project-structure 的页面上。此外,我的 wabapp 的屏幕已附加

webapp-结构

标签: javaspringrestredirectlocalhost

解决方案


用于MockMvc测试控制器。这是一个例子: https ://howtodoinjava.com/spring-boot2/testing/spring-boot-mockmvc-example/


推荐阅读