diff --git a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/config/Swagger2Config.java b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/config/Swagger2Config.java index a0048eb505..6f3557d597 100644 --- a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/config/Swagger2Config.java +++ b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/config/Swagger2Config.java @@ -16,11 +16,12 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; public class Swagger2Config { @Bean public Docket api() { - return new Docket(DocumentationType.SWAGGER_2).select() - .apis(RequestHandlerSelectors.basePackage("com.baeldung.swagger2boot.controller")) - .paths(PathSelectors.regex("/.*")) - .build() - .apiInfo(apiEndPointsInfo()); + return new Docket(DocumentationType.SWAGGER_2) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build() + .apiInfo(apiEndPointsInfo()); } private ApiInfo apiEndPointsInfo() { diff --git a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/controller/UserController.java b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/controller/UserController.java new file mode 100644 index 0000000000..d8102a8407 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/controller/UserController.java @@ -0,0 +1,39 @@ +package com.baeldung.swagger2boot.controller; + +import com.baeldung.swagger2boot.model.Foo; +import com.baeldung.swagger2boot.model.User; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import javax.websocket.server.PathParam; + +import static org.apache.commons.lang3.RandomStringUtils.randomNumeric; + +@Controller +public class UserController { + + public UserController() { + super(); + } //@formatter:off + + @RequestMapping(method = RequestMethod.POST, value = "/createUser", produces = "application/json; charset=UTF-8") + @ResponseStatus(HttpStatus.CREATED) + @ResponseBody + @ApiOperation(value = "Create user", + notes = "This method creates a new user") + public User createUser(@ApiParam( + name = "firstName", + type = "String", + value = "First Name of the user", + example = "Vatsal", + required = true) @RequestParam String firstName) { //@formatter:on + User user = new User(firstName); + return user; + } +} diff --git a/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/model/User.java b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/model/User.java new file mode 100644 index 0000000000..d4a6479f4e --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-2/src/main/java/com/baeldung/swagger2boot/model/User.java @@ -0,0 +1,28 @@ +package com.baeldung.swagger2boot.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel +public class User { + + @ApiModelProperty(value = "first name of the user", name = "firstName", dataType = "String", example = "Vatsal") + String firstName; + + public User() { + super(); + } + + public User(final String firstName) { + super(); + this.firstName = firstName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } +}