1

I have spring boot 2 and swagger API page working at http://localhost:8448/portalapi/swagger-ui.html. But I wanted to map http://localhost:8448/ and http://localhost:8448/portalapi/ to forward the request to http://localhost:8448/portalapi/swagger-ui.html. But it is not working. It gives HTTP Status 404 – Not Found (tomcat default response). Any help or pointer is greatly appreciated.

appliacation.properties

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

server.servlet.contextPath=/portalapi/

This is a webconfig class to context to forward:

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

    }
Bmis13
  • 550
  • 1
  • 8
  • 27

2 Answers2

1

Change the below piece from :

@Configurable
@Component
public class WebConfig implements WebMvcConfigurer {

to:

@Configuration
public class WebConfig implements WebMvcConfigurer {
snmaddula
  • 1,111
  • 1
  • 7
  • 21
  • It did not work. As "/" is not mapped to dispatcherServlet. – Bmis13 May 09 '19 at 21:35
  • In your application.properties, `server.servlet.contextPath=/portalapi/` means you will not be having any mapping to `/`, instead you should be using `/portalapi` which is your context root. – snmaddula May 10 '19 at 05:43
0

Try redirect controller

@Controller
public class RedirectController {

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

}
Tan mally
  • 572
  • 1
  • 5
  • 11