Kashyap
Kashyap

Reputation: 477

Need to update swagger UI Path for spring boot project

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.

Upvotes: 1

Views: 9478

Answers (3)

SSK
SSK

Reputation: 3766

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

Upvotes: 2

Alexpandiyan Chokkan
Alexpandiyan Chokkan

Reputation: 1075

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

Upvotes: 1

Sudoss
Sudoss

Reputation: 357

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.

Upvotes: 2

Related Questions