Exclude models or properties from swagger response

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

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

How to do it?

I extracted data from db and attached it to the user object and passed that user object to the JAX-RS responder.

Below is one of my DTO objects:

@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 do not see the fields or properties of the user object inside the returned json format of Swagger.

The answer of my service class method 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> 
+6
source share
1 answer

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

You have 3 ways to hide the property:

  • If you use JAXB annotations, you can use @XmlTransient .
  • If you use Jackson, you can use @JsonIgnore .
  • If you are not using any of them or do not want to influence the general de-serialization of your models, you can use Swagger @ApiModelProperty hidden .

Keep in mind that you may have to install them on your recipient devices, not on the property itself. Play with definitions to see what works for you.

As for the problem with the user model, the problem is that you are not referencing it with @ApiOperation (you also do not need the httpMethod property). Try changing it as follows:

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

Source: https://habr.com/ru/post/980518/


All Articles