【问题标题】:DispatcherServlet: redirect on page at localhostDispatcherServlet:在 localhost 的页面上重定向
【发布时间】:2020-06-11 17:16:41
【问题描述】:

我正在尝试测试我的 @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 的屏幕已附加

【问题讨论】:

    标签: java spring rest redirect localhost


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 2020-09-20
      • 2017-07-08
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      相关资源
      最近更新 更多