Document APIs using Swagger

Use Swagger to document Spring boots porject’s APIs

Swagger instruction

Swagger is a set of API development tools from design, documentation, test and deployment.

Swagger UI is a built-in solution which makes user interaction with the Swagger-generated API documentation much easier.

Springfox also supports the bean validation (JSR-303) annotations through its springfox-bean-validators library.

Integrating Swagger with Spring Boot

Gradle and Maven configs in V3 nd V2


dependencies {
    implementation "io.springfox:springfox-boot-starter:3.0.0"




Default configuration

  1. The @EnableSwagger2 annotation is used to enable Swagger 2 support for the application.
  2. All the configuration is done through the Docket Bean. You can give a title and write a nice description for your apis using apiInfo configuration.
  3. Also, you can decide which apis to include in the documentation by selecting paths which matches your predicates.
  4. go to /swagger-ui.html endpoint to access swagger documentation.

Integrating Swagger with Spring (without Boot)

If you are not using Spring boot in your project, then /swagger-ui.html endpoint will give 404 not found error because no resource handler is configured for this endpoint.

Swagger UI adds a set of resources which you must configure as part of a class that extends WebMvcConfigurerAdapter, and is annotated with @EnableWebMvc.

create a file inside config package of your project with the following contents -

public class WebMvcConfig extends WebMvcConfigurerAdapter {
    public void addResourceHandlers(ResourceHandlerRegistry registry) {


A Simple Sagger Config File

To document the service we use a Docket. Docket stands for A summary or other brief statement of the contents of a document; an abstract. Docket helps configure a subset of the services to be documented and groups them by name. Significant changes to this is the ability to provide an expressive predicate based for api selection.

@EnableWebMvc //NOTE: Only needed in a non-springboot application
public class SwaggerConfig {
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Moss' Swagger doc")
                .description("very basic, very simple")
                .contact(new Contact("Moss", "", ""))

Swagger API annotations

Model Example

  • @ApiModel
  • @ApiModelProperty
    • value, String
    • name, String
    • dataType, String
    • required, boolean
    • example, String
    • hidden, boolean
    • allowEmptyValue, boolean
    • allowableValues, for Enum
@ApiModel(description = "this is a model for may be a user")
public class SomeModel {

    @ApiModelProperty(value = "ID", example = "100")
    private Integer id;

    @ApiModelProperty(value = "name", example = "Shown Mew")
    private String name;

    @ApiModelProperty(value = "some property blah~")
    private long someProperty;

Controller Example

  • @Api
    • tags, String[]
    • description, String
  • @ApiOperation
    • value, String
    • notes, String,
    • tags, String[]
    • response, Class<?>
    • httpMethod, String
  • ​@ApiParam
  • ​@ApiImplicitParams
  • ​@ApiImplicitParam
    • paramType, path, query, header, form
    • dataType, Long, String
    • name
    • value
    • required, true, false(default)
  • @ApiIgnore Works when you don’t want to expose some APIS via Swagger, e.g., /delete
@ApiOperation(value = "Find pet by Status",
     notes = "${SomeController.findPetsByStatus.notes}",
     response = Pet.class)
@RequestMapping(value = "/findByStatus", method = RequestMethod.GET, params = {"status"}, produces="application/json")
public Pet findPetsByStatus(
   @ApiParam(value = "${SomeController.findPetsByStatus.status}",
        required = true,...)
       defaultValue="${SomeController.findPetsByStatus.status.default}") String status) {

@ApiOperation(notes = "Operation 2", value = "${SomeController.operation2.value}"...)
    @ApiImplicitParam(name="header1", value="${SomeController.operation2.header1}", ...)
@RequestMapping(value = "operation2", method = RequestMethod.POST, produces="application/json")
public ResponseEntity<String> operation2() {
  return ResponseEntity.ok("");

Controllers grouping, tags

  • @Api
@Api(tags = "for teachers")
@RequestMapping(value = "/teacher")
class TeacherController {
    // ...

@Api(tags = "for students")
@RequestMapping(value = "/student")
static class StudentController {
    // ...

APIs filtering

You may want to filter some APIs out from Swagger because they’re sensitive, you can do this with either:

  1. add @ApiIgnore to the endpoint
  2. filter if out in Docket in apis() or paths()
    • apis() filter on packages
    • paths() filter for urls
public boolean delete(@PathVariable("id") int id)


Customisized response

Swagger allows us to overwirete the HTTP response using Docket globalResponseMessage(). This is an example that we overwrite all 500 and 403 errors in GET. In, we do

.globalResponseMessage(RequestMethod.GET, newArrayList(
new ResponseMessageBuilder()
              .message("Server error")
              .responseModel(new ModelRef("Error"))
       new ResponseMessageBuilder()
              .message("Resource unavailable")

API Versioning

Spring Boot does not provide any dedicated solutions for versioning APIs.

Swagger2 library, which provides a grouping mechanism from version 2.8.0, which is perfect for generating documentation of versioned REST API.

Swagger UI With an OAuth-secured API

Let’s see how we can allow Swagger to access an OAuth-secured API – using the Authorization Code grant type in this example.

We’ll configure Swagger to access our secured API using the SecurityScheme and SecurityContext support:

public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2).select()


NumberFormatException in Swagger upgrading

The issue

ava.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(
    at java.lang.Long.parseLong(
    at java.lang.Long.valueOf(
    at io.swagger.models.parameters.AbstractSerializableParameter.getExample(

The reason

In @ApiModelProperty, if your data type is Long, the default value is an empty string. It throws NumberFormatException while converting to a Long value.

The easiest way to fix it is to add a example=12.34. However, in most of the case, it’s not a proper solution.

@ApiModelProperty(value = "order Id",example = "1234")
private Long orderId;

The Fix

This bug is introduced in Swagger after 1.6.0, we would fix it by using the correct dependency in that version.

    <!--Fix the NumberFormatException in Swagger 2.9.2-->

Migrate from V2 to V3 in spring boot projects

  1. Remove library inclusions of earlier releases. Specifically remove springfox-swagger2 and springfox-swagger-ui inclusions.
  2. Remove the @EnableSwagger2 annotations
  3. Add the springfox-boot-starter