22

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

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

How to do that?

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

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

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>
Flexo
  • 87,323
  • 22
  • 191
  • 272
LazyGuy
  • 715
  • 4
  • 12
  • 27

2 Answers2

40

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

You have 3 ways to hide a property:

  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.

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)
stites
  • 4,903
  • 5
  • 32
  • 43
Ron
  • 14,160
  • 3
  • 52
  • 39
  • 1
    Those fields are part of the DTO object. swagger-core doesn't invent fields ;) – Ron Jan 05 '15 at 13:36
  • 1
    I don't understand what you mean by 'inside model'. You're saying you manage to hide fields successfully so I'm not sure what you're trying to solve. – Ron Jan 06 '15 at 07:54
  • See my updated question..! Why it's not showing User object properties. Its showing EntityTag, MediaType etc.. instead of User object..did u got my question – LazyGuy Jan 06 '15 at 08:16
  • Thanks for the update, but it would help also if you shared the DTO. If you're not referring to User from the DTO (or any other class or operation that uses it), it will not be shown. – Ron Jan 06 '15 at 08:58
  • i already posted the DTO object in the updated code. i.e; User.java..check my edited question – LazyGuy Jan 06 '15 at 09:25
  • 2 more questions.. see my updated question..! it contains swagger config. can i remove license and license url from my production code and can i use swagger in production without those? 2) how to make the basepath, version and other config values dynamically..? – LazyGuy Jan 06 '15 at 11:50
  • 1
    You can remove the license and license url, but keep in mind that in Swagger 2.0, the license name is required. The other config values can be done dynamically by using code rather than a static bean. Follow this tutorial for more information - https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-JAX-RS-Project-Setup. – Ron Jan 06 '15 at 12:45
  • thanks webron..current my swaggerconfig is like . can i get the basepath value from properties file...! – LazyGuy Jan 06 '15 at 12:54
  • As said, you can instantiate the BeanConfig in your code, and there you can do whatever you want. – Ron Jan 06 '15 at 13:16
  • can i instantiate the BeanConfig in the normal java class..! I instantiated beanconfig inside a customlistener which implements servletcontextlistener.but still not able to read properties file .getting listener error.. by hardcoding values in java class. it was working.but i need to pass the values of beanconfig in java from properties file..what is the work around.when should i instantiate beanconfig and where shoudl i ? – LazyGuy Jan 06 '15 at 13:41
  • 1
    Please trying following the tutorial I gave earlier regarding the BeanConfig. There are a few samples in our repo that use it. Other than that, this is not related to the original question and we're making a mess here. Please use our mailing list for additional questions. – Ron Jan 06 '15 at 13:47
15

You can exclude fields like that :

@ApiModelProperty(position = 1, required = true, hidden=true, notes = "used to display user name")
slfan
  • 8,950
  • 115
  • 65
  • 78
Loran92
  • 151
  • 1
  • 2