We'll define a Bean named Docket in the configuration class for this purpose.

Docket is a builder which is intended to be the primary interface into the swagger-springmvc framework. It provides sensible defaults and convenience methods for configuration.

After the Docket bean is defined, calling its select() method returns an instance of ApiSelectorBuilder, which provides control over the endpoints exposed by Swagger.

We can define a base package for our REST API classes as well if we wish to, using RequestHandlerSelectors.basePackage(). It will scan the base package and create APIs for all of the classes within it.

On the other hand, we can use RequestHandlerSelectors.any() to generate documentation for all packages.

In our case, it's the com.demo.resource package, where we defined the HelloResource and UserResource classes.

The paths() method further defines for which paths in our APIs do we want to create documentation for. All of our endpoints have "/v1", so in our case it includes all of the endpoints. However, this may not always be the case.

If you'd wish to include all endpoints - you can easily do so by using PathSelectors.any().

Swagger UI

Let's use Swagger UI to observe all of our REST endpoints that Swagger created.

To utilize Swagger UI, we need to add a dependency for it to our pom.xml file:

Upon expanding hello-resource we can see that the right side of the documentation got updated. Also, the response messages updated with code that we provided and return type from the @Api annotation on class level.

Model Properties

Swagger2 provides us with a set of annotations to manipulate models with a lot of control:

There's a wide range of things you can define using @ApiModelProperty. For more information and a list of methods, visit the official documentation.

Expanding GET/v1/user then clicking on the Model property, we can notice the descriptions on each field.

"Example Value" shows just default values.

Conclusion

Each day, companies and individuals are starting to use Swagger as their tool of choice for exposing REST APIs to third parties.

Utilizing Swagger's tools, you can generate code based on the documentation of an API, as well as create beautiful, interactive documentation. This both saves time and effort and offers a standard for people to work with.