Springboot with Swagger

Swagger is an open source project used to generate the REST API documents for RESTful web services.

Add swagger and swagger-ui dependencies


Add Swagger Config class

public class SwaggerConfig {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    public Docket api() {
        log.info("setting up Docket..");
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
        log.info("docket setup!");
        return docket;

    private ApiInfo getApiInfo() {
        Contact contact = new Contact("Folau Kaveinga", "folaukaveinga.org", "folaukaveinga@gmail.com");
        return new ApiInfoBuilder()
                .title("Spring Data Tutorial")
                .description("Spring Data API")


Anotate controller classes

  1. Anotate Controller class with @Api
  2. Anotate methods with @ApiOperation
  3. Use @ApiParam to describe parameters
@Api(value = "users", produces = "Rest API for User operations", tags = "User Controller")
public class UserController {

    private Logger      log = LoggerFactory.getLogger(this.getClass());

    private UserService userService;

    @ApiOperation(value = "Get Member By Id")
    public ResponseEntity<User> getUserById(@ApiParam(name = "id", required = true, value = "id of member") @PathVariable("id") Long id) {
        log.info("get user by id: {}", id);

        return new ResponseEntity<>(userService.getById(id), HttpStatus.OK);


Source code on Github

Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!


Leave a Reply

Your email address will not be published. Required fields are marked *