【发布时间】:2016-06-30 14:52:33
【问题描述】:
假设我有一个带有注释的下面的类 -
@Component
@Path("/somepath/")
@Api(value = "/somepath", description = "Operations")
public class SomeResourceClass {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create new Resource",
response = SomeResource.class)
@ApiResponses(value = { @ApiResponse(code = 401, message = "Some Message") })
public SomeResource createResource(@ApiParam(value="someResource", required = true)SomeResource resource) {
}
}
我正在尝试使用下面的代码访问上面类级别使用的注释列表
List<ClassResourceInfo> classResourceInfos = serverFactoryBean.getServiceFactory().getClassResourceInfo();
for (ClassResourceInfo classResourceInfo : classResourceInfos) {
for (Annotation annotation : classResourceInfo.getResourceClass().getAnnotations()) {
// work with each annotation
}
}
我从 classResourceInfo.getResourceClass().getAnnotations() 获得以下列表 -
[@org.springframework.stereotype.Component(value=),
@javax.ws.rs.Path(value=/somepath/),
@io.swagger.annotations.Api(basePath=, hidden=false, authorizations=[@io.swagger.annotations.Authorization(scopes=[@io.swagger.annotations.AuthorizationScope(scope=, description=)], value=)], produces=, description=Operations, position=0, protocols=, value=/somepath, tags=[], consumes=)]
所以在这里我得到了在类级别定义的完整注释列表。
但现在如果我使用 @PreAuthorize on 方法,我会得到不完整的列表。
@Component
@Path("/somepath/")
@Api(value = "/somepath", description = "Operations")
public class SomeResourceClass {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create new Resource",
response = SomeResource.class)
@ApiResponses(value = { @ApiResponse(code = 401, message = "Some Message") })
@PreAuthorize("hasRole('ADMIN')")
public SomeResource createResource(@ApiParam(value="someResource", required = true)SomeResource resource) {
}
}
我在这个场景中得到的类级别上定义的注释列表是 -
[@io.swagger.annotations.Api(basePath=, hidden=false, authorizations=[@io.swagger.annotations.Authorization(scopes=[@io.swagger.annotations.AuthorizationScope(scope=, description=)], value=)], produces=, description=Operations, position=0, protocols=, value=/somepath, tags=[], consumes=)]
我已经在 web-context xml 文件中定义了 SomeResourceClass
<jaxrs:server id="service-rest" address="/">
<jaxrs:serviceBeans>
<ref bean="someResourceClass"/>
</jaxrs:serviceBeans>
</jaxrs:server>
当我在其中一种方法上使用 @PreAuthorize 批注时,您能帮我指出为什么我得到的类级别批注列表不完整吗?
【问题讨论】:
标签: java xml spring annotations