1

I am having a spring boot project and need to update my swagger path from

http://localhost:9012/swagger-ui.html

to

http://localhost:9012/myservice/swagger-ui.html

I am not using any context path for my application I have tried all the below approach but none of them worked.

Change location to call swagger-ui in Spring

https://github.com/springfox/springfox/issues/1443

How to change swagger-ui.html default path

https://github.com/springfox/springfox/issues/1080

How to change Swagger-ui URL prefix?

private static final String PATH = "/myservice";

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    final String apiDocs = "/v2/api-docs";
    final String configUi = "/swagger-resources/configuration/ui";
    final String configSecurity = "/swagger-resources/configuration/security";
    final String resources = "/swagger-resources";
    final String swaggerUI = "/swagger-ui.html";

    registry.addRedirectViewController(apiDocs, PATH +apiDocs).setKeepQueryParams(true);
    registry.addRedirectViewController(resources, PATH +resources);
    registry.addRedirectViewController(configUi, PATH +configUi);
    registry.addRedirectViewController(configSecurity, PATH +configSecurity);
    registry.addRedirectViewController(swaggerUI, PATH + swaggerUI);
    registry.addRedirectViewController("/",PATH);
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry){
    registry.addResourceHandler(PATH + "/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
    registry.addResourceHandler(PATH + "/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  }

I am using swagger version 2.9.2 Is there any other way to resolve the issue.

Kashyap
  • 385
  • 3
  • 13
  • Hi @Kashyap, can you check https://howtodoinjava.com/swagger2/swagger-spring-mvc-rest-example/ – semicolon Jul 22 '20 at 05:56
  • Try adding a context path `server.servlet.contextPath=/myservice` in your application.properties and see if it works. – Sudoss Jul 22 '20 at 06:14
  • Hi LUC1F3R and Sudoss, both the ways suggest adding context path if I add context path to my application all the other APIs URLs will also change which I don't want. – Kashyap Jul 22 '20 at 06:26
  • @Kashyap got it...looks like redirect/forward is what you are looking for. Please try the code snippet [here](https://stackoverflow.com/a/63028959/8938332) and let us know if that works for you. – Sudoss Jul 22 '20 at 08:20

3 Answers3

2

Try this redirect method in your controller, I have tested it and it works

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

You can use either of redirect or forward based on your requirement.

Sudoss
  • 347
  • 2
  • 13
1

Please use the following swagger dependency. All you have to do is just add in the pom.xml, it will work like charm. Remove any swagger related annotation like @EnableSwagger2.

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

Swagger URL: http://{base_url}/swagger-ui/index.html

Alexpandiyan Chokkan
  • 1,025
  • 1
  • 10
  • 30
1

You can try below code it requires @Controller not a @RestController

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@ApiIgnore
public class SwaggerMappingController {

    @RequestMapping("/swagger-ui.html")
    public String greeting() {
        return "redirect:/swagger-ui/index.html";
    }
}
SSK
  • 3,444
  • 6
  • 32
  • 59