diff --git a/spring-boot-modules/spring-boot-swagger/generated/swagger-ui/swagger.json b/spring-boot-modules/spring-boot-swagger/generated/swagger-ui/swagger.json new file mode 100644 index 0000000000..4ebe165384 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/generated/swagger-ui/swagger.json @@ -0,0 +1,57 @@ +{ + "swagger" : "2.0", + "info" : { + "description" : "This is a Baeldung Document Enum Sample Code", + "version" : "v1", + "title" : "Baeldung - Document Enum", + "contact" : { + "name" : "Parikshit Murria", + "email" : "pmurria@baeldung.com" + }, + "license" : { + "name" : "Apache 2.0", + "url" : "https://www.apache.org/licenses/LICENSE-2.0.html" + } + }, + "host" : "baeldung.com", + "basePath" : "/api", + "schemes" : [ "http", "https" ], + "paths" : { + "/hire" : { + "post" : { + "summary" : "This method is used to hire employee with a specific role", + "description" : "", + "operationId" : "hireEmployee", + "produces" : [ "application/json" ], + "parameters" : [ { + "in" : "body", + "name" : "body", + "description" : "role", + "required" : true, + "schema" : { + "$ref" : "#/definitions/Employee" + } + } ], + "responses" : { + "200" : { + "description" : "successful operation", + "schema" : { + "type" : "string" + } + } + } + } + } + }, + "definitions" : { + "Employee" : { + "type" : "object", + "properties" : { + "role" : { + "type" : "string", + "enum" : [ "Engineer", "Clerk", "Driver", "Janitor" ] + } + } + } + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger/pom.xml b/spring-boot-modules/spring-boot-swagger/pom.xml index d6b62bce3c..2c0e52b671 100644 --- a/spring-boot-modules/spring-boot-swagger/pom.xml +++ b/spring-boot-modules/spring-boot-swagger/pom.xml @@ -25,6 +25,11 @@ springfox-boot-starter ${springfox.version} + + com.github.kongchen + swagger-maven-plugin + ${swagger-maven-plugin.version} + @@ -33,11 +38,50 @@ org.springframework.boot spring-boot-maven-plugin + + com.github.kongchen + swagger-maven-plugin + ${swagger-maven-plugin.version} + + + + false + com.baeldung.swaggerenums.controller + http,https + baeldung.com + /api + + Baeldung - Document Enum + v1 + This is a Baeldung Document Enum Sample Code + + pmurria@baeldung.com + Parikshit Murria + + + https://www.apache.org/licenses/LICENSE-2.0.html + Apache 2.0 + + + generated/swagger-ui + + + + + + compile + + generate + + + + 3.0.0 + 3.1.1 diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/SwaggerEnumsApplication.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/SwaggerEnumsApplication.java new file mode 100644 index 0000000000..0bb80df65f --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/SwaggerEnumsApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.swaggerenums; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SwaggerEnumsApplication { + + public static void main(String[] args) { + SpringApplication.run(SwaggerEnumsApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/controller/HireController.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/controller/HireController.java new file mode 100644 index 0000000000..7b7bfdc2c5 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/controller/HireController.java @@ -0,0 +1,22 @@ +package com.baeldung.swaggerenums.controller; + +import com.baeldung.swaggerenums.model.Employee; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; + +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +@Api +@Path(value="/hire") +@Produces({"application/json"}) +public class HireController { + + @POST + @ApiOperation(value = "This method is used to hire employee with a specific role") + public String hireEmployee(@ApiParam(value = "role", required = true) Employee employee) { + return String.format("Hired for role: %s", employee.role.name()); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/model/Employee.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/model/Employee.java new file mode 100644 index 0000000000..ce9e5089c9 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/model/Employee.java @@ -0,0 +1,17 @@ +package com.baeldung.swaggerenums.model; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +@ApiModel +public class Employee { + @ApiModelProperty + public Role role; + + public Role getRole() { + return role; + } + public void setRole(Role role) { + this.role = role; + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/model/Role.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/model/Role.java new file mode 100644 index 0000000000..fa62b2d0b5 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerenums/model/Role.java @@ -0,0 +1,8 @@ +package com.baeldung.swaggerenums.model; + +import io.swagger.annotations.ApiModel; + +@ApiModel +public enum Role { + Engineer, Clerk, Driver, Janitor; +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger/src/test/java/com/baeldung/swaggerenums/controller/HireControllerUnitTest.java b/spring-boot-modules/spring-boot-swagger/src/test/java/com/baeldung/swaggerenums/controller/HireControllerUnitTest.java new file mode 100644 index 0000000000..b2ec5ccaa5 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/test/java/com/baeldung/swaggerenums/controller/HireControllerUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.swaggerenums.controller; + +import com.baeldung.swaggerenums.model.Employee; +import com.baeldung.swaggerenums.model.Role; +import org.junit.Assert; +import org.junit.Test; + +public class HireControllerUnitTest { + + @Test + public void givenRoleEngineer_whenHireEmployee_thenReturnsRoleInString() { + //Arrange + Role testRole = Role.Engineer; + Employee employee = new Employee(); + employee.setRole(testRole); + + //Act + HireController hireController = new HireController(); + String response = hireController.hireEmployee(employee); + + //Assert + Assert.assertEquals(String.format("Hired for role: %s", testRole), + response); + } +}