Spring Boot 2.1.3.RELEASE,我希望将swagger-ui.html作为主页/欢迎页面和处理ROOT上下文(/)

时间:2019-05-08 02:32:28

标签: spring spring-boot swagger swagger-ui embedded-tomcat-8

我在http://localhost:8448/portalapi/swagger-ui.html上有spring boot 2和swagger API页面。但是我想映射http://localhost:8448/http://localhost:8448/portalapi/以将请求转发到http://localhost:8448/portalapi/swagger-ui.html。但这是行不通的。它提供HTTP状态404 –找不到(tomcat默认响应)。任何帮助或指针,我们将不胜感激。

appliacation.properties

##
# tomcat server port
server.port=8448
#server.address=0.0.0.0

server.servlet.contextPath=/portalapi/

这是要上下文转发的webconfig类:

   @Configurable
    @Component
    public class WebConfig implements WebMvcConfigurer {

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            // forward requests to /admin and /user to their index.html
            registry.addViewController("/").setViewName(
                    "forward:/portalapi/swagger-ui.html");
            registry.addViewController("/portalapi/").setViewName(
                    "forward:/portalapi/swagger-ui.html");
        }

    }

2 个答案:

答案 0 :(得分:1)

更改以下内容:

@Configurable
@Component
public class WebConfig implements WebMvcConfigurer {

收件人:

@Configuration
public class WebConfig implements WebMvcConfigurer {

答案 1 :(得分:0)

尝试重定向控制器

@Controller
public class RedirectController {

    @RequestMapping(value = {"/","/portalapi/"})
    public String redirectToSwagger() {
        return "redirect:/portalapi/swagger-ui.html";
    }

}