Java Swagger不显示/记录我的RESTful端点(JAX-RS、Spring boot)

Java Swagger不显示/记录我的RESTful端点(JAX-RS、Spring boot),java,rest,spring-boot,jax-rs,swagger,Java,Rest,Spring Boot,Jax Rs,Swagger,我已经使用Jax-RS在Java和Spring-boot中开发了一个RESTful web服务,我想用Swagger记录它。到目前为止,我已经成功地将swagger-ui.html页面映射到http:8080/localhost//swagger ui.html不幸的是,我的RESTful端点没有出现在任何地方 我使用的是: pom.xml <dependency> <groupId>io.springfox</groupId>

我已经使用
Jax-RS
Java
Spring-boot
中开发了一个RESTful web服务,我想用
Swagger
记录它。到目前为止,我已经成功地将swagger-ui.html页面映射到http:8080/localhost//swagger ui.html不幸的是,我的RESTful端点没有出现在任何地方

我使用的是:

pom.xml

 <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
JAX-RS端点的示例

@Configuration
@EnableSwagger2
public class SwaggerConfiguration
{
    @Autowired
    private TypeResolver typeResolver;

    @Bean
    public Docket api()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.nick.java.webservice.services"))
                .paths(PathSelectors.any())
                .build()
                .enable(true)
                .apiInfo(getApiInfo())
                .tags(
                        new Tag("My web service", "Methods for my RESTful service")
                );
    }

    private ApiInfo getApiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("API Documentation")
                .description("API")
                .version("1.0")
                .contact(new Contact("mycompany", "", "nickath@mycompany.com"))
                .build();

        return apiInfo;
    }
package org.nick.java.webservice.services;

@Path("/contextsapi")
@Consumes("application/json")
@Produces("application/json")
@Api(value = "Contexts API", produces = "application/json")
public interface ContextAPI {

    @Path("/contexts/contexts")
    @GET
    @ApiOperation( value = "get contexts",
                   response = List.class)
    List<Context> getContexts();
<cxf.version>3.1.15</cxf.version>
<swagger-ui.version>3.9.2</swagger-ui.version>

 <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
        <version>${cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-service-description-swagger</artifactId>
        <version>${cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>swagger-ui</artifactId>
        <version>${swagger-ui.version}</version>
    </dependency>
package org.nick.java.webservice.services;
@路径(“/contextsapi”)
@使用(“应用程序/json”)
@生成(“应用程序/json”)
@Api(value=“Contexts-Api”,products=“application/json”)
公共接口ContextAPI{
@路径(“/contexts/contexts”)
@得到
@ApiOperation(value=“获取上下文”,
响应=List.class)
列出getContexts();
swagger-ui.html页面的屏幕截图

如您所见,没有生成“获取上下文”方法

知道我做错了什么吗

=======更新-服务实施========

package  org.nick.java.webservice.services.impl;
@Service
@Api(value = "Contexts Api Impl", produces = "application/json", description = "desc")
@Path("/contextsapi")
public class ContextAPIImpl implements ContextAPI {

   @Override
   @GET
   @ApiOperation( value = "get contexts", response = List.class)
   public List<Context> getContexts(){
     //code ommitted
   }
}
package org.nick.java.webservice.services.impl;
@服务
@Api(value=“Contexts Api Impl”,products=“application/json”,description=“desc”)
@路径(“/contextsapi”)
公共类ContextAPI实现ContextAPI{
@凌驾
@得到
@ApiOperation(value=“获取上下文”,response=List.class)
公共列表getContexts(){
//编码
}
}

Swagger假设不显示任何
API客户端的文档
。如果有任何带有Swagger注释的文档,它将为您的服务生成文档

要确认这一点,请尝试创建一个Spring
@service
并使用招摇过市的注释进行注释。如果所有其他方面都得到了处理,则将生成文档。由于您可以看到UI,我假设依赖项是正确的

这里的想法是,您的任务是记录您的服务,而swagger有助于这一点。为您的服务使用的API生成/发布文档不是您的责任。因为您不维护服务,所以维护文档也没有意义


当我第一次使用Rest客户端时,我对这一点也有点困惑。但如果你真的想一想,这是意料之中的,也是有道理的。

解决了

最后,我按照下面的示例使用了
Swagger2Feature
解决了我的问题

Maven依赖关系

@Configuration
@EnableSwagger2
public class SwaggerConfiguration
{
    @Autowired
    private TypeResolver typeResolver;

    @Bean
    public Docket api()
    {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.nick.java.webservice.services"))
                .paths(PathSelectors.any())
                .build()
                .enable(true)
                .apiInfo(getApiInfo())
                .tags(
                        new Tag("My web service", "Methods for my RESTful service")
                );
    }

    private ApiInfo getApiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("API Documentation")
                .description("API")
                .version("1.0")
                .contact(new Contact("mycompany", "", "nickath@mycompany.com"))
                .build();

        return apiInfo;
    }
package org.nick.java.webservice.services;

@Path("/contextsapi")
@Consumes("application/json")
@Produces("application/json")
@Api(value = "Contexts API", produces = "application/json")
public interface ContextAPI {

    @Path("/contexts/contexts")
    @GET
    @ApiOperation( value = "get contexts",
                   response = List.class)
    List<Context> getContexts();
<cxf.version>3.1.15</cxf.version>
<swagger-ui.version>3.9.2</swagger-ui.version>

 <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
        <version>${cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-service-description-swagger</artifactId>
        <version>${cxf.version}</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>swagger-ui</artifactId>
        <version>${swagger-ui.version}</version>
    </dependency>
3.1.15
3.9.2
org.apache.cxf
//swagger/api文档?url=//swagger/swagger.json


要自定义端点的UI,请查看手册

,我建议使用Swagger 2我也遇到了同样的问题。 问题在于您所实现的摘要,正确的正则表达式可能会有所帮助。 例如:

@Configuration
@EnableSwagger2
public class SwaggerConfig {                                    
@Bean
public Docket api() { 
    return new Docket(DocumentationType.SWAGGER_2)  
      .select()                                  
      .apis(RequestHandlerSelectors.any())              
      .paths(PathSelectors.any())                          
      .build();                                           
}
}
你可以参考上面的链接
源代码示例也来自上面的链接。

hi shakhawat,谢谢你的回答:)。我试着按照你说的做(用虚张声势注释我的@service类,但没有生成文档),实际上我在我的服务中添加了虚张声势注释,实现了getContexts函数@service@Api(value=“Contexts-Api”,products=“application/json”,description=“description”,hidden=false)公共类contextapimpl实现ContextAPI{@Override@ApiOperation(value=“get contexts”,response=List.class)公共列表getContexts(){//code}我遗漏了什么吗?你能在这里发布你的服务实现吗?同时保留包名。请检查问题中的更新,我在那里添加了更新以便于阅读:)你能从接口中删除招摇过市注释并将api选择器更改为
.api(RequestHandlerSelectors.any())
?我按照您所说的做了,并添加了.path(谓词.not(PathSelectors.regex(“/error.*”));以避免显示基本的错误控制器,但仍然不走运:/