从摇摇欲坠的响应中排除模型或属性 [英] Exclude Models or properties from swagger response

查看:755
本文介绍了从摇摇欲坠的响应中排除模型或属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在apache cxf项目中使用了swagger,使用了@Api和@ApiOperations以及@ApiParam注释,并为其余服务生成了一个api文档.

I used swagger in my apache cxf project , used @Api and @ApiOperations and @ApiParam annotations and generated a api doc for the rest services.

但是我想从模型"属性或完整的模块或属性属性中排除某些字段,例如EntityTag,StatusType和MediaType等.

But I want to exclude some of the fields like EntityTag, StatusType and MediaType etc from Models attribute or complete modules or properties attribute.

该怎么做?

我正在从db获取数据并将其设置为用户对象,然后将该用户对象传递给JAX-RS响应生成器.

I was fetching data from db and setting it to user object and passing that user object to JAX-RS response builder.

下面是我的DTO对象之一:

Below is one of my DTO Object:

  @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;
 }

现在我看不到Swagger返回的json格式内的User对象字段或属性.

Now I don't see the User object fields or properties inside the Swagger returned json format.

我的服务类方法响应是:

my service class method response is :

    @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>

推荐答案

首先,您应该升级到最新的swagger-core版本,当前为1.3.12(您使用的是旧版本).

First of all, you should upgrade to the latest swagger-core version, currently 1.3.12 (you're using a really old one).

您有3种隐藏属性的方式:

You have 3 ways to hide a property:

  1. 如果使用的是JAXB批注,则可以使用@XmlTransient.
  2. 如果您使用的是Jackson,则可以使用@JsonIgnore.
  3. 如果您既不使用模型,也不希望影响模型的一般反序列化,则可以使用Swagger的
  1. If you're using JAXB annotations, you can use @XmlTransient.
  2. If you're using Jackson, you can use @JsonIgnore.
  3. If you're not using either, or don't want to affect the general de/serialization of your models, you can use Swagger's @ApiModelProperty's hidden attribute.

请记住,您可能需要在获取器/设置器上设置这些属性,而不是在属性本身上进行设置.尝试定义,看看哪种对您有用.

Keep in mind you may need to set these on your getters/setters rather than on the property itself. Play with the definitions to see what works for you.

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

With regards to the issue with the User model, the problem is that you do not reference it from the @ApiOperation (you also don't need the httpMethod property). Try changing it as follows:

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

这篇关于从摇摇欲坠的响应中排除模型或属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆