【问题标题】:Swagger + Spring boot doesn't show API end points in http://localhost:8080/home/swagger-ui.htmlSwagger + Spring boot 未在 http://localhost:8080/home/swagger-ui.html 中显示 API 端点
【发布时间】:2020-05-17 19:07:39
【问题描述】:

我有一个 Spring Boot 应用程序在 jetty + swagger 上运行 当我转到http://localhost:8080/home/swagger-ui.html(见附件)时,我没有看到任何端点规范,即使我写了一个控制器并在那里有一个端点。

我的应用程序代码是:

package com.MyService;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class MyServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GcmAuthenticationServiceApplication.class, args);
    }


}

在 pom.xml 我使用:

    <!-- Swagger 2.0 support -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.8.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.8.0</version>
            </dependency>
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>2.3.0</version>
            </dependency>

我的控制器:

package com.MyService.controller;

import com.MyService.model.RegistrationData;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;

@Api(value = "register", description = "the registration API")
public interface RegistrationApi {



    @ApiOperation(value = "Register user", nickname = "register", notes = "", authorizations = {
            @Authorization(value = "petstore_auth", scopes = {
                    @AuthorizationScope(scope = "write:pets", description = "modify pets in your account"),
                    @AuthorizationScope(scope = "read:pets", description = "read your pets")
            })
    }, tags={ "register", })
    @ApiResponses(value = {
            @ApiResponse(code = 405, message = "Invalid input") })
    @RequestMapping(value = "/register",
            produces = { "application/json", "application/xml" },
            consumes = { "application/json", "application/xml" },
            method = RequestMethod.POST)
    default ResponseEntity<Void> registerUser(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true )
                                              @Valid @RequestBody RegistrationData body) {

        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }

}



------

package com.MyService.controller;


import com.MyService.model.RegistrationData;
import com.MyService.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
public class RegistrationController implements  RegistrationApi{


    private RegistrationService registrationService;


    @Autowired
    public void setProductService(RegistrationService registrationService) {
        this.registrationService = registrationService;
    }


    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseEntity registerUser(@RequestBody RegistrationData registrationData){
        //todo call service to rgeister
        return new ResponseEntity("user register successfully", HttpStatus.OK);
    }

}

我的 SwaggerConfig:

package com.MyService.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static springfox.documentation.builders.PathSelectors.regex;
@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select().apis(RequestHandlerSelectors.basePackage("guru.springframework.controllers"))
                .paths(regex("/product.*")).build();

    }

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

    }

}

我的招摇文档如下所示:

【问题讨论】:

  • 检查这个答案 - stackoverflow.com/a/64333853/410439

标签: java spring spring-boot swagger


【解决方案1】:

您可以使用以下内容更改现有代码。基础包必须指向您的控制器基础包,并且 URL 上下文应该是您在控制器中添加的内容之一。

将基础包修改为“com.MyService.controller”,并将“/api/v1”和“/register”添加到路径正则表达式中,

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2).select()
            .apis(RequestHandlerSelectors.basePackage("com.MyService.controller"))
            .paths(regex("(/api/v1.*)|(/register.*)")).build();
}

【讨论】:

    【解决方案2】:

    您可以显示/product.* URL,但在您的端点中,没有任何与/product.* 匹配的URL。这就是它不显示任何端点的原因。

    如下更改您的代码

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select().apis(RequestHandlerSelectors.basePackage("guru.springframework.controllers"))
                .paths(or(regex("/register.*"),regex("/api/v1.*"))).build();
    
    }
    

    您可以使用regex("/.*") 允许所有端点。

    【讨论】:

    • 谢谢,但此代码未编译:错误消息:预期 1 个参数,但找到 2 个
    • 谢谢,但我尝试了这两个示例,但仍然看不到端点。收到一条消息:规范中没有定义任何操作!
    • 删除authorizations 中的@ApiOperation 部分并尝试但我不确定。如果仍然无法正常工作请尝试在 git 中使用我的示例(也请检查控制器类)。
    • 我认为您需要更新 Docket bean 中的基本包以匹配您的包结构。应该是com.MyService.controller
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 2021-02-22
    • 1970-01-01
    • 2022-12-13
    • 2019-08-06
    • 1970-01-01
    • 2018-06-08
    相关资源
    最近更新 更多