Skip to content

Swagger ​

EasyApi supports Swagger (OpenAPI) annotations for enhanced API documentation.

Swagger 2 (io.swagger.annotations) ​

The swagger extension (enabled by default) provides support for Swagger 2.x annotations.

Supported Annotations ​

AnnotationRules Applied
@Apiclass.doc (value, tags), ignore (hidden)
@ApiModelclass.doc (value, description)
@ApiModelPropertyfield.name (name), field.doc (value, notes), field.required (required), field.ignore (hidden), field.demo (example)
@ApiOperationmethod.doc (value, notes), ignore (hidden), method.default.http.method (httpMethod)
@ApiParamparam.doc (value), param.default.value (defaultValue), param.required (required), param.ignore (hidden), param.demo (example)
@ApiImplicitParam / @ApiImplicitParamsParameter extraction for parameters with no Java signature, routed by paramType
@ApiResponse / @ApiResponsesAppends **response <code>**: <message> to the endpoint description

Swagger 2 Example ​

java
@Api(tags = "User Management")
@RestController
@RequestMapping("/api/users")
public class UserController {

    @ApiOperation(value = "Get user by ID", notes = "Returns a single user")
    @GetMapping("/{id}")
    public User getUser(
            @ApiParam(value = "User ID", required = true)
            @PathVariable Long id) {
        // ...
    }
}

@ApiModelProperty ​

java
public class User {
    @ApiModelProperty(value = "User name", required = true)
    private String name;

    @ApiModelProperty(value = "Email address", notes = "Must be unique")
    private String email;

    @ApiModelProperty(hidden = true)
    private String internalId;

    @ApiModelProperty(name = "user_name")
    private String username;
}

@ApiImplicitParam ​

@ApiImplicitParam (and its @ApiImplicitParams container) declares parameters that have no matching Java signature — request headers above all. Each entry is routed by its paramType onto the matching parameter list, and defaultValue is carried through as the parameter default:

java
@ApiOperation(value = "Get user by ID")
@ApiImplicitParams({
    @ApiImplicitParam(name = "X-Tenant", value = "Tenant ID", paramType = "header", required = true),
    @ApiImplicitParam(name = "verbose", value = "Include details", paramType = "query", defaultValue = "false")
})
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
    // ...
}
paramTypeExported as
query (default)Query parameter
formForm parameter
pathPath variable
headerRequest header

@ApiResponse ​

@ApiResponse and @ApiResponses append each declared code and message to the endpoint description, so every channel renders them:

java
@ApiOperation(value = "Update user")
@ApiResponses({
    @ApiResponse(code = 200, message = "User updated successfully"),
    @ApiResponse(code = 404, message = "User not found")
})
@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody UserRequest request) {
    // ...
}

Only code and message are carried — the response type still comes from the method's return type.

Swagger 3 / OpenAPI 3 (io.swagger.v3.oas.annotations) ​

The swagger3 extension (enabled by default) provides comprehensive support for OpenAPI 3.x annotations.

Supported Annotations ​

AnnotationRules Applied
@Operationapi.name (summary), method.doc (summary, description), method.default.http.method (method), deprecated marking
@Parameterparam.doc (description), param.required (required), param.ignore (hidden), deprecated marking, parameter extraction
@ParametersExtracts multiple parameters with type resolution
@Schemafield.name (name), field.doc (description), field.required (requiredMode), field.ignore (hidden), param.doc, param.required, param.ignore
@Hiddenignore, field.ignore, param.ignore
@ApiResponseResponse code, description, and headers
@ApiResponsesMultiple response resolution

Swagger 3 Example ​

java
@Tag(name = "User Management")
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Operation(summary = "Get user by ID", description = "Returns a single user")
    @GetMapping("/{id}")
    public User getUser(
            @Parameter(description = "User ID", required = true)
            @PathVariable Long id) {
        // ...
    }

    @Operation(summary = "Create user")
    @ApiResponses({
        @ApiResponse(responseCode = "201", description = "User created"),
        @ApiResponse(responseCode = "400", description = "Invalid input")
    })
    @PostMapping
    public User createUser(@RequestBody UserRequest request) {
        // ...
    }
}

@Schema with requiredMode ​

OpenAPI 3.1 uses requiredMode instead of required:

java
public class User {
    @Schema(description = "User name", requiredMode = Schema.RequiredMode.REQUIRED)
    private String name;

    @Schema(description = "Email address")
    private String email;
}

@Parameter Extraction ​

The @Parameter annotation is automatically extracted to set the correct parameter type:

@Parameter.in valueParameter Type
query (default)Query parameter
formForm parameter
pathPath variable
headerHeader parameter

@ApiResponse Resolution ​

@ApiResponse and @ApiResponses annotations are automatically resolved into response documentation:

java
@Operation(summary = "Update user")
@ApiResponses({
    @ApiResponse(responseCode = "200", description = "User updated successfully"),
    @ApiResponse(responseCode = "404", description = "User not found",
        headers = @Header(name = "X-Error", description = "Error code"))
})
@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody UserRequest request) {
    // ...
}

Loading Additional Swagger Configuration ​

You can load additional Swagger configuration from a remote source:

properties
properties.additional=https://raw.githubusercontent.com/tangcent/easy-api/master/third/swagger.config

Released under the Apache-2.0 License.