Swagger REST API 注释不适用于接口(interface)但适用于实现类

标签 swagger swagger-ui

这是我的接口(interface) ClassA .java

@Path("/"+Paths.STORIES)
@ApiModel(value = "Name API")
@Api(value = "/stories", description = "Name API")
public interface ClassA {
    @GET
    @Path("/"+Paths.STORYID)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Fetch Story by ID", notes = "More notes about this method")
    @ApiResponses(value = {
              @ApiResponse(code = 400, message = "Invalid ID supplied"),
              @ApiResponse(code = 200, message = "Invalid ID supplied"),
            })
    public Response getNameFromID(@PathParam("nameId") String nameId);
}

这是我的实现类。
@Singleton
@Component
public class ClassB implements ClassA,InitializingBean{
    @Override
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Response getNameFromID(final String nameId) {
        Map NameResponse = new HashMap<String,Object>();
        NameResponse.put("repsonseCode", "200");
        NameResponse.put("errorCode", "");
        return Response.status(200).entity(NameResponse).build();
    }
}

Application-context.xml 条目
<bean id="swaggerConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
        <property name="resourcePackage" value="com.razak.sample" />
        <property name="version" value="1.0.0" />
        <property name="basePath" value="http://localhost:8080/api" />
        <property name="title" value="Petstore sample app" />
        <property name="description" value="This is a app." />
        <property name="contact" value="apiteam@wordnik.com" />
        <property name="license" value="Apache 2.0 License" />
        <property name="licenseUrl"
            value="http://www.apache.org/licenses/LICENSE-2.0.html" />
        <property name="scan" value="true" />
    </bean>

web.xml 条目
    <param-name>swagger.version</param-name>

    <param-value>1.1</param-value>

</init-param>

<init-param>

    <param-name>swagger.api.basepath</param-name>

    <param-value>http://localhost:8080/api</param-value>

</init-param>

<init-param>

    <param-name>swagger.security.filter</param-name>

    <param-value>com.wordnik.swagger.sample.util.ApiAuthorizationFilterImpl</param-value>

</init-param>
@Api(value = "/stories", description = "Story API") at class level and 
    @GET
    @Path("/"+Paths.STORYID)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Fetch Story by ID", notes = "More notes about this method")
    @ApiResponses(value = {
              @ApiResponse(code = 400, message = "Invalid ID supplied"),
              @ApiResponse(code = 200, message = "Invalid ID supplied"),
            })

当我将这些条目从接口(interface)移动到实现类时。我能够访问 swagger 中的其余端点。swagger 正在工作。但是当我将该注释放在接口(interface)本身中时。它不起作用。

最佳答案

这是一个 Unresolved 问题:https://github.com/wordnik/swagger-core/issues/562

还有一个 Unresolved 问题来记录如何覆盖默认扫描仪:https://github.com/wordnik/swagger-core/issues/513

我还没有尝试过,但我认为这两个问题的解决方案都可以在这个解决方案中找到:https://github.com/wordnik/swagger-core/wiki/Java-Setup-for-Spring---Jersey---JAX-RS

诀窍是让 Swagger 使用 ReflectiveJaxrsScanner 而不是 DefaultJaxrsScanner。

关于Swagger REST API 注释不适用于接口(interface)但适用于实现类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23932409/

相关文章:

java - Quarkus Swagger-UI 授权

rest - Swagger - 调用返回文件的服务无法正常工作

spring-boot - Swagger 的 @ApiResponses 和 @ApiResponses

swagger - OpenAPI 重用部分定义而不定义新的

java - 忽略 Swagger 模型中的未知属性

c# - 来自枚举的 Swagger 预定义返回类型值

c# - Swashbuckle/Swagger 没有从配置中获取路由

java - Swagger-ui 不显示任何 api 详细信息

swagger - Swagger UI 上的 CORS 问题

python - 在 connexion 中链接 yaml 文件时出错