Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

java - Swagger2 Change Base Path for Swagger Ui

Was able to setup Swagger 2 to my SpringBoot app by just adding this SwaggerConfig file and adding the following dependencies:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(Predicates.not(PathSelectors.regex("/error"))).build().apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo("Motorcycle Shop - Restful Services", "Rest based API for Motorcycle Shop", "1.0", "",
                new Contact("", "", ""), "", "");
        return apiInfo;
    }
}

pom.xml

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
 </parent>

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- Swagger -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.1</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

Despite fact that my controller class looks like this:

@RestController
@RequestMapping("/motorcycles")
public class ProductController { 

// GET method

}

... am still able to invoke that controller by doing this:

curl -X GET http://localhost:8080/motorcycles

I have to open up the Swagger-ui.html file using the following URL path:

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

How can I make my Spring Boot app to show something like this (the actual app name or the default RequestMapping specified in a controller - if the controller is the only one in the app):

http://localhost:8080/motorcycles/swagger-ui.html 

Basically, how can I prefix the swagger-ui.html with the app name?

So, lets say my app is called motorboy, this is what I want:

http://localhost:8080/motorboy/swagger-ui.html

And the Curl -X GET for the REST Endpoint looks like this:

http://localhost:8080/motorboy/motorcycles

It seems that spring-boot is just using plain old http://localhost:8080 for the default app name in the browser for swagger.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I solved this problem setting context path to spring boot application in the application.properties (you can set this variable with different ways, see this spring doc):

server.servlet.context-path=/user (or motorboy in your case)

After set context path I can access swagger-ui or swagger docs from http://localhost:8080/user/swagger-ui.html and http://localhost:8080/user/v2/api-docs respectively.

If you want I can make a simple project and update to github just for clarify and explain this configuration.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...