Swagger REST API注释不适用于接口,但适用于实现类
这是我的接口ClassA.javaSwagger REST API注释不适用于接口,但适用于实现类,swagger,swagger-ui,Swagger,Swagger Ui,这是我的接口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(va
@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();
}
}
当我将这些条目从接口移动到实现类时。我能够在swagger中访问rest端点。swagger正在工作。但当我将该注释放置在接口本身时,它不工作。这是一个未决问题:
@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();
}
}
还有一个悬而未决的问题需要记录如何覆盖默认扫描仪:
我还没有尝试过这一点,但我认为这两个问题的解决方案都可以在这个解决方案中找到:
诀窍是让Swagger使用ReflectiveJaxrScanner而不是DefaultJaxrScanner。在Swagger-core-1.3.10中,注释中添加了@Inherited,因此我相信现在可以使用了。这是如何工作的?你只是在实现方法上加了一个@Inherited?
<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"),
})