2015-01-05 27 views
6

我在apache cxf项目中使用了swagger,使用了@Api和@ApiOperations和@ApiParam注释,并为其余服务生成了api文档。从swagger响应中排除模型或属性

但我想从模型属性或完整的模块或属性属性中排除一些像EntityTag,StatusType和MediaType等字段。

如何做到这一点?

我从数据库中获取数据并将其设置为用户对象并将该用户对象传递给JAX-RS响应构建器。

下面是我的DTO对象之一:

@ApiModel 
    public class User{ 
    private String name; 
    private String email; 


@ApiModelProperty(position = 1, required = true, notes = "used to display user name") 
public int getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name= name; 
} 

@ApiModelProperty(position = 2, required = true, notes = "used to display user email") 
public int getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email= email; 
} 

现在我看不到里面扬鞭用户对象字段或属性返回的JSON格式。

我的服务类方法回应是:

所有的
@GET 
    @ApiOperation(value = "xxx", httpMethod = "GET", notes = "user details", response = Response.class) 
    public Response getUserInfo(){ 
     User userdto = userdaoimpl.getUserDetails(); 
     ResponseBuilder builder = Response.ok(user, Status.OK), MediaType.APPLICATION_JSON); 
     builder.build(); 
} 


<bean id="swaggerConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig"> 
    <property name="resourcePackage" value="com.services.impl" /> 
    <property name="version" value="1.0.0" /> 
    <property name="basePath" value="http://localhost:8080/api" /> 
    <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> 
+0

你使用哪个版本的swagger-core? – Ron

回答

15

首先,你应该升级到最新的招摇核心版本,目前1.3.12(您使用的是真的老了一个)。

你有3种方法来隐藏属性:

  1. 如果你使用JAXB注释,你可以使用@XmlTransient
  2. 如果您使用的是杰克逊,您可以使用@JsonIgnore
  3. 如果您不使用或不想影响模型的一般de /序列化,您可以使用Swagger的@ApiModelProperty's hidden attribute

请记住,您可能需要将这些设置在您的getters/setters而不是属性本身上。玩这些定义,看看什么适合你。

关于用户模型的问题,问题是您没有从@ApiOperation引用它(您也不需要httpMethod属性)。尝试改变它如下:

@ApiOperation(value = "xxx", notes = "user details", response = User.class) 
+0

这些字段是DTO对象的一部分。 swagger核心不发明领域;) – Ron

+0

我不明白你的意思是'内部模型'。你说你成功地隐藏了字段,所以我不确定你想要解决什么。 – Ron

+0

看到我更新的问题..!为什么它不显示用户对象属性。它显示EntityTag,媒体类型等..而不是用户对象..你是否得到了我的问题 – LazyGuy