SpringBoot - 从MVCControllers中分离RestControllers

时间:2018-01-23 17:23:44

标签: rest spring-mvc spring-boot

我正在尝试使用2个separeted路径创建一个应用程序,一个用于API,另一个用于MVC。

我创建了2个ServletRegistrationBean。 /*的第一个和/api/v1/*的第二个

这两种情况都运作良好,问题是答复所有控制器。

我正在测试两条路径:

  • /home用于MVC控制器
  • /health用于Rest Controller

但是下面的所有路径都有效。

  • /api/v1/home
  • /api/v1/health
  • /home
  • /health

我只想要下面的路径

  • /home
  • /api/v1/health

    @Configuration
    @EnableWebMvc
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
            @Bean
            public ServletRegistrationBean dispatcherAPIServletRegistration() {
                AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
                context.scan(HealthController.class.getPackage().getName());
    
                DispatcherServlet servlet = new DispatcherServlet();
                servlet.setApplicationContext(context);
    
                ServletRegistrationBean registration = new ServletRegistrationBean(servlet, "/api/v1/*");
    
                registration.setLoadOnStartup(1);
                registration.setName("api-v1");
    
                return registration;
            }
    
            @Bean
            public ServletRegistrationBean dispatcherServletRegistration() {
                AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
                context.scan(HomeController.class.getPackage().getName());
    
                DispatcherServlet servlet = new DispatcherServlet();
                servlet.setApplicationContext(context);
    
                ServletRegistrationBean registration = new ServletRegistrationBean(servlet, "/*");
    
                registration.setLoadOnStartup(1);
                registration.setName("web-mvc");
    
                return registration;
            }
    
    }
    

0 个答案:

没有答案
相关问题