From 7d2ec82d78d1a99274787d554b50d061fecd5a81 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Mon, 25 Nov 2019 13:28:54 +0200 Subject: [PATCH 01/13] BAEL-3456 - Springfox --- springfox/pom.xml | 97 +++++++++++++++++++ .../springfox/SpringfoxApplication.java | 65 +++++++++++++ .../springfox/controller/UserController.java | 42 ++++++++ .../com/baeldung/springfox/model/User.java | 68 +++++++++++++ .../plugin/EmailAnnotationPlugin.java | 39 ++++++++ .../springfox/repository/UserRepository.java | 11 +++ .../repository/UserRestRepository.java | 11 +++ .../src/main/resources/application.properties | 3 + 8 files changed, 336 insertions(+) create mode 100644 springfox/pom.xml create mode 100644 springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java create mode 100644 springfox/src/main/java/com/baeldung/springfox/controller/UserController.java create mode 100644 springfox/src/main/java/com/baeldung/springfox/model/User.java create mode 100644 springfox/src/main/java/com/baeldung/springfox/plugin/EmailAnnotationPlugin.java create mode 100644 springfox/src/main/java/com/baeldung/springfox/repository/UserRepository.java create mode 100644 springfox/src/main/java/com/baeldung/springfox/repository/UserRestRepository.java create mode 100644 springfox/src/main/resources/application.properties diff --git a/springfox/pom.xml b/springfox/pom.xml new file mode 100644 index 0000000000..cbea0baa39 --- /dev/null +++ b/springfox/pom.xml @@ -0,0 +1,97 @@ + + + 4.0.0 + com.baeldung.web + springfox + springfox + SpringFox + war + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + + + jcenter + jcenter + https://jcenter.bintray.com/ + + + jcenter-snapshots + jcenter + http://oss.jfrog.org/artifactory/oss-snapshot-local/ + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-rest + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + + + + + io.springfox + springfox-swagger2 + 3.0.0-SNAPSHOT + + + io.springfox + springfox-swagger-ui + 3.0.0-SNAPSHOT + + + io.springfox + springfox-data-rest + 3.0.0-SNAPSHOT + + + io.springfox + springfox-bean-validators + 3.0.0-SNAPSHOT + + + + + + com.google.guava + guava + ${guava.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + com.baeldung.springfox.SpringfoxApplication + 27.0.1-jre + + diff --git a/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java b/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java new file mode 100644 index 0000000000..20f560f9ef --- /dev/null +++ b/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java @@ -0,0 +1,65 @@ +package com.baeldung.springfox; + +import static springfox.documentation.builders.PathSelectors.regex; + +import java.util.Collections; +import java.util.function.Predicate; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Import; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import com.baeldung.springfox.plugin.EmailAnnotationPlugin; +import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; + + +@SpringBootApplication +@EnableSwagger2WebMvc +@EntityScan("com.baeldung.springfox.model") +@ComponentScan("com.baeldung.springfox.controller") +@EnableJpaRepositories("com.baeldung.springfox.repository") +@Import({SpringDataRestConfiguration.class, BeanValidatorPluginsConfiguration.class}) +public class SpringfoxApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringfoxApplication.class, args); + } + + @Bean + public Docket springfoxAppInfo() { + return new Docket(DocumentationType.SWAGGER_2) + .groupName("baeldung-springfox-api") + .select() + .paths(paths()) + .build() + .apiInfo(apiInfo()); + } + + private Predicate paths() { + return regex("/users.*").or(regex("/api.*")); + } + + private ApiInfo apiInfo() { + return new ApiInfo( + "Springfox API specification", + "User REST and Spring Data APIs", + "", + "", + null, + "License of API", "API license URL", Collections.emptyList()); + } + + @Bean + public EmailAnnotationPlugin emailPlugin() { + return new EmailAnnotationPlugin(); + } + +} diff --git a/springfox/src/main/java/com/baeldung/springfox/controller/UserController.java b/springfox/src/main/java/com/baeldung/springfox/controller/UserController.java new file mode 100644 index 0000000000..6a827bb757 --- /dev/null +++ b/springfox/src/main/java/com/baeldung/springfox/controller/UserController.java @@ -0,0 +1,42 @@ +package com.baeldung.springfox.controller; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; +import static org.springframework.web.bind.annotation.RequestMethod.POST; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.baeldung.springfox.model.User; +import com.baeldung.springfox.repository.UserRepository; + +@Controller +@RequestMapping(value = "/api/user", produces = MediaType.APPLICATION_JSON_VALUE) +public class UserController { + + @Autowired + private UserRepository userRepository; + + @RequestMapping(method = POST) + @ResponseBody + public ResponseEntity createUser(@RequestBody User user) { + userRepository.save(user); + return new ResponseEntity<>(user, HttpStatus.OK); + } + + @RequestMapping(method = GET) + @ResponseBody + public ResponseEntity getUser(@RequestParam Long id) { + Optional user = userRepository.findById(id); + return new ResponseEntity<>(user.get(), HttpStatus.OK); + } + +} diff --git a/springfox/src/main/java/com/baeldung/springfox/model/User.java b/springfox/src/main/java/com/baeldung/springfox/model/User.java new file mode 100644 index 0000000000..90de0deea5 --- /dev/null +++ b/springfox/src/main/java/com/baeldung/springfox/model/User.java @@ -0,0 +1,68 @@ +package com.baeldung.springfox.model; + +import javax.persistence.Id; +import javax.validation.constraints.Email; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; +import javax.persistence.Entity; + +@Entity +public class User{ + + @Id + private Long id; + + @NotNull(message = "First Name cannot be null") + private String firstName; + + private String lastName; + + @Min(value = 15, message = "Age should not be less than 15") + @Max(value = 65, message = "Age should not be greater than 65") + private int age; + + @Email(regexp=".@.\\..*", message = "Email should be valid") + private String email; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + } diff --git a/springfox/src/main/java/com/baeldung/springfox/plugin/EmailAnnotationPlugin.java b/springfox/src/main/java/com/baeldung/springfox/plugin/EmailAnnotationPlugin.java new file mode 100644 index 0000000000..2e5e57aec5 --- /dev/null +++ b/springfox/src/main/java/com/baeldung/springfox/plugin/EmailAnnotationPlugin.java @@ -0,0 +1,39 @@ +package com.baeldung.springfox.plugin; + +import static springfox.bean.validators.plugins.Validators.annotationFromBean; + +import java.util.Optional; + +import javax.validation.constraints.Email; + +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import springfox.bean.validators.plugins.Validators; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin; +import springfox.documentation.spi.schema.contexts.ModelPropertyContext; + +@Component +@Order(Validators.BEAN_VALIDATOR_PLUGIN_ORDER) +public class EmailAnnotationPlugin implements ModelPropertyBuilderPlugin { + + @Override + public boolean supports(DocumentationType delimiter) { + return true; + } + + /** + * read Email annotation + */ + @Override + public void apply(ModelPropertyContext context) { + Optional email = annotationFromBean(context, Email.class); + if (email.isPresent()) { + context.getBuilder().pattern(email.get().regexp()); + context.getBuilder().example("email@email.com"); + } + } + +} + diff --git a/springfox/src/main/java/com/baeldung/springfox/repository/UserRepository.java b/springfox/src/main/java/com/baeldung/springfox/repository/UserRepository.java new file mode 100644 index 0000000000..16303bc6d0 --- /dev/null +++ b/springfox/src/main/java/com/baeldung/springfox/repository/UserRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.springfox.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.springfox.model.User; + +@Repository +public interface UserRepository extends CrudRepository { + +} diff --git a/springfox/src/main/java/com/baeldung/springfox/repository/UserRestRepository.java b/springfox/src/main/java/com/baeldung/springfox/repository/UserRestRepository.java new file mode 100644 index 0000000000..9edce50af4 --- /dev/null +++ b/springfox/src/main/java/com/baeldung/springfox/repository/UserRestRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.springfox.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +import com.baeldung.springfox.model.User; + +@RepositoryRestResource(collectionResourceRel = "users", path = "users") +public interface UserRestRepository extends CrudRepository { + +} diff --git a/springfox/src/main/resources/application.properties b/springfox/src/main/resources/application.properties new file mode 100644 index 0000000000..3b4076297b --- /dev/null +++ b/springfox/src/main/resources/application.properties @@ -0,0 +1,3 @@ +### Spring Boot default error handling configurations +#server.error.whitelabel.enabled=false +#server.error.include-stacktrace=always From 6877619128942eea223bbb076a5983743b53184e Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Mon, 25 Nov 2019 13:33:22 +0200 Subject: [PATCH 02/13] BAEL-3456 - pom.xml identation --- springfox/pom.xml | 158 +++++++++++++++++++++++----------------------- 1 file changed, 79 insertions(+), 79 deletions(-) diff --git a/springfox/pom.xml b/springfox/pom.xml index cbea0baa39..f6b5c37911 100644 --- a/springfox/pom.xml +++ b/springfox/pom.xml @@ -1,97 +1,97 @@ - 4.0.0 - com.baeldung.web - springfox - springfox - SpringFox - war + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.baeldung.web + springfox + springfox + SpringFox + war + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + + - - parent-boot-2 - com.baeldung - 0.0.1-SNAPSHOT - ../parent-boot-2 - - - - + - jcenter - jcenter - https://jcenter.bintray.com/ - - - jcenter-snapshots - jcenter - http://oss.jfrog.org/artifactory/oss-snapshot-local/ - + jcenter + jcenter + https://jcenter.bintray.com/ + + + jcenter-snapshots + jcenter + http://oss.jfrog.org/artifactory/oss-snapshot-local/ + - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-data-rest - - - org.springframework.boot - spring-boot-starter-data-jpa - - - com.h2database - h2 - - - - - - io.springfox - springfox-swagger2 - 3.0.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-web - io.springfox - springfox-swagger-ui - 3.0.0-SNAPSHOT + org.springframework.boot + spring-boot-starter-data-rest - io.springfox - springfox-data-rest - 3.0.0-SNAPSHOT + org.springframework.boot + spring-boot-starter-data-jpa - io.springfox - springfox-bean-validators - 3.0.0-SNAPSHOT + com.h2database + h2 - - - - com.google.guava - guava - ${guava.version} - - + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + io.springfox + springfox-swagger2 + 3.0.0-SNAPSHOT + + + io.springfox + springfox-swagger-ui + 3.0.0-SNAPSHOT + + + io.springfox + springfox-data-rest + 3.0.0-SNAPSHOT + + + io.springfox + springfox-bean-validators + 3.0.0-SNAPSHOT + - - com.baeldung.springfox.SpringfoxApplication - 27.0.1-jre - + + + + com.google.guava + guava + ${guava.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + com.baeldung.springfox.SpringfoxApplication + 27.0.1-jre + From 43dc24c83f9d41d5d4f5b98cada2a985ae990281 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Mon, 25 Nov 2019 13:44:13 +0200 Subject: [PATCH 03/13] BAEL-3456 - pom.xml code identation with spaces --- springfox/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/springfox/pom.xml b/springfox/pom.xml index f6b5c37911..cbe29e6989 100644 --- a/springfox/pom.xml +++ b/springfox/pom.xml @@ -10,7 +10,7 @@ war - parent-boot-2 + parent-boot-2 com.baeldung 0.0.1-SNAPSHOT ../parent-boot-2 @@ -20,7 +20,7 @@ - jcenter + jcenter jcenter https://jcenter.bintray.com/ From 0218e6476e0934211fd1b6b81435a1002030f6aa Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Mon, 25 Nov 2019 15:34:18 +0200 Subject: [PATCH 04/13] Updated pom.xml --- springfox/pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/springfox/pom.xml b/springfox/pom.xml index cbe29e6989..da4066d382 100644 --- a/springfox/pom.xml +++ b/springfox/pom.xml @@ -1,8 +1,8 @@ - 4.0.0 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 com.baeldung.web springfox springfox From 1de0f7bf494fa934902a26534b8f28932912218e Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Mon, 25 Nov 2019 15:37:02 +0200 Subject: [PATCH 05/13] updated pom --- springfox/pom.xml | 166 +++++++++++++++++++++++----------------------- 1 file changed, 82 insertions(+), 84 deletions(-) diff --git a/springfox/pom.xml b/springfox/pom.xml index da4066d382..a47f27cde7 100644 --- a/springfox/pom.xml +++ b/springfox/pom.xml @@ -1,97 +1,95 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung.web - springfox - springfox - SpringFox - war + com.baeldung.web + springfox + springfox + SpringFox + war - + parent-boot-2 - com.baeldung - 0.0.1-SNAPSHOT - ../parent-boot-2 - - - - - - + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + jcenter - jcenter - https://jcenter.bintray.com/ - - - jcenter-snapshots - jcenter - http://oss.jfrog.org/artifactory/oss-snapshot-local/ - - + jcenter + https://jcenter.bintray.com/ + + + jcenter-snapshots + jcenter + http://oss.jfrog.org/artifactory/oss-snapshot-local/ + + - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-data-rest - - - org.springframework.boot - spring-boot-starter-data-jpa - - - com.h2database - h2 - + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-rest + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + - + - - io.springfox - springfox-swagger2 - 3.0.0-SNAPSHOT - - - io.springfox - springfox-swagger-ui - 3.0.0-SNAPSHOT - - - io.springfox - springfox-data-rest - 3.0.0-SNAPSHOT - - - io.springfox - springfox-bean-validators - 3.0.0-SNAPSHOT - + + io.springfox + springfox-swagger2 + 3.0.0-SNAPSHOT + + + io.springfox + springfox-swagger-ui + 3.0.0-SNAPSHOT + + + io.springfox + springfox-data-rest + 3.0.0-SNAPSHOT + + + io.springfox + springfox-bean-validators + 3.0.0-SNAPSHOT + - + + + com.google.guava + guava + ${guava.version} + + - - com.google.guava - guava - ${guava.version} - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - com.baeldung.springfox.SpringfoxApplication - 27.0.1-jre - + + com.baeldung.springfox.SpringfoxApplication + 27.0.1-jre + From 3e22f5aa3a4a503e4c7939e4b337377544124b69 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Mon, 25 Nov 2019 16:13:35 +0200 Subject: [PATCH 06/13] Updated springfox module in the pom.xml --- pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pom.xml b/pom.xml index 23b44fb285..f719d3b067 100644 --- a/pom.xml +++ b/pom.xml @@ -611,6 +611,7 @@ tensorflow-java spf4j + springfox spring-boot-configuration spring-boot-flowable spring-boot-mvc-2 @@ -1380,6 +1381,7 @@ rxjava-libraries oauth2-framework-impl spf4j + springfox spring-boot-performance spring-boot-properties From 3b4e9585d5d454bb44e5f82c93b7aa039d4aa22e Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Thu, 28 Nov 2019 17:14:05 +0200 Subject: [PATCH 07/13] Springfox - PR review fix --- .../springfox/SpringfoxApplication.java | 21 ++++++++----------- .../springfox/controller/UserController.java | 15 ++++++------- .../com/baeldung/springfox/model/User.java | 10 --------- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java b/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java index 20f560f9ef..364dcf61cd 100644 --- a/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java +++ b/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java @@ -12,7 +12,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + import com.baeldung.springfox.plugin.EmailAnnotationPlugin; + import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; @@ -36,11 +38,11 @@ public class SpringfoxApplication { @Bean public Docket springfoxAppInfo() { return new Docket(DocumentationType.SWAGGER_2) - .groupName("baeldung-springfox-api") - .select() - .paths(paths()) - .build() - .apiInfo(apiInfo()); + .groupName("baeldung-springfox-api") + .select() + .paths(paths()) + .build() + .apiInfo(apiInfo()); } private Predicate paths() { @@ -48,13 +50,8 @@ public class SpringfoxApplication { } private ApiInfo apiInfo() { - return new ApiInfo( - "Springfox API specification", - "User REST and Spring Data APIs", - "", - "", - null, - "License of API", "API license URL", Collections.emptyList()); + return new ApiInfo("Springfox API specification", "User REST and Spring Data APIs", + "", "", null, "License of API", "API license URL", Collections.emptyList()); } @Bean diff --git a/springfox/src/main/java/com/baeldung/springfox/controller/UserController.java b/springfox/src/main/java/com/baeldung/springfox/controller/UserController.java index 6a827bb757..d70cc3e7a5 100644 --- a/springfox/src/main/java/com/baeldung/springfox/controller/UserController.java +++ b/springfox/src/main/java/com/baeldung/springfox/controller/UserController.java @@ -1,8 +1,5 @@ package com.baeldung.springfox.controller; -import static org.springframework.web.bind.annotation.RequestMethod.GET; -import static org.springframework.web.bind.annotation.RequestMethod.POST; - import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; @@ -10,6 +7,8 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -25,16 +24,14 @@ public class UserController { @Autowired private UserRepository userRepository; - @RequestMapping(method = POST) - @ResponseBody - public ResponseEntity createUser(@RequestBody User user) { + @PostMapping + public @ResponseBody ResponseEntity createUser(@RequestBody User user) { userRepository.save(user); return new ResponseEntity<>(user, HttpStatus.OK); } - @RequestMapping(method = GET) - @ResponseBody - public ResponseEntity getUser(@RequestParam Long id) { + @GetMapping + public @ResponseBody ResponseEntity getUser(@RequestParam Long id) { Optional user = userRepository.findById(id); return new ResponseEntity<>(user.get(), HttpStatus.OK); } diff --git a/springfox/src/main/java/com/baeldung/springfox/model/User.java b/springfox/src/main/java/com/baeldung/springfox/model/User.java index 90de0deea5..3f94b8b3c8 100644 --- a/springfox/src/main/java/com/baeldung/springfox/model/User.java +++ b/springfox/src/main/java/com/baeldung/springfox/model/User.java @@ -16,8 +16,6 @@ public class User{ @NotNull(message = "First Name cannot be null") private String firstName; - private String lastName; - @Min(value = 15, message = "Age should not be less than 15") @Max(value = 65, message = "Age should not be greater than 65") private int age; @@ -41,14 +39,6 @@ public class User{ this.firstName = firstName; } - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - public String getEmail() { return email; } From af0d9788611267294cef0b4b9b91c5d20c9f6fbd Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Fri, 29 Nov 2019 10:50:28 +0200 Subject: [PATCH 08/13] updated pom.xml --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index 65e3c96704..4966706383 100644 --- a/pom.xml +++ b/pom.xml @@ -622,7 +622,6 @@ tensorflow-java spf4j springfox - spring-boot-configuration spring-boot-config-jpa-error spring-boot-flowable spring-boot-mvc-2 From 0be367c1a59a5bb8e2124669d6be987953aa146b Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Fri, 29 Nov 2019 23:38:39 +0200 Subject: [PATCH 09/13] Springfox - pom.xml updated, removed UserRestRepository --- springfox/pom.xml | 3 +-- .../springfox/repository/UserRestRepository.java | 11 ----------- 2 files changed, 1 insertion(+), 13 deletions(-) delete mode 100644 springfox/src/main/java/com/baeldung/springfox/repository/UserRestRepository.java diff --git a/springfox/pom.xml b/springfox/pom.xml index a47f27cde7..c620e49a3d 100644 --- a/springfox/pom.xml +++ b/springfox/pom.xml @@ -17,12 +17,12 @@ - jcenter jcenter https://jcenter.bintray.com/ + jcenter-snapshots jcenter @@ -49,7 +49,6 @@ - io.springfox springfox-swagger2 diff --git a/springfox/src/main/java/com/baeldung/springfox/repository/UserRestRepository.java b/springfox/src/main/java/com/baeldung/springfox/repository/UserRestRepository.java deleted file mode 100644 index 9edce50af4..0000000000 --- a/springfox/src/main/java/com/baeldung/springfox/repository/UserRestRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.springfox.repository; - -import org.springframework.data.repository.CrudRepository; -import org.springframework.data.rest.core.annotation.RepositoryRestResource; - -import com.baeldung.springfox.model.User; - -@RepositoryRestResource(collectionResourceRel = "users", path = "users") -public interface UserRestRepository extends CrudRepository { - -} From 848abce39636e2e939ac4c798047935c215679d1 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sun, 1 Dec 2019 13:08:53 +0200 Subject: [PATCH 10/13] Springfox - PR review fixes --- .../springfox/SpringfoxApplication.java | 10 +++++-- .../com/baeldung/springfox/model/User.java | 26 +++++++++---------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java b/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java index 364dcf61cd..aaf732a8bb 100644 --- a/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java +++ b/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java @@ -50,8 +50,14 @@ public class SpringfoxApplication { } private ApiInfo apiInfo() { - return new ApiInfo("Springfox API specification", "User REST and Spring Data APIs", - "", "", null, "License of API", "API license URL", Collections.emptyList()); + return new ApiInfo("Springfox API specification", + "User REST and Spring Data APIs", + "", + "", + null, + "License of API", + "API license URL", + Collections.emptyList()); } @Bean diff --git a/springfox/src/main/java/com/baeldung/springfox/model/User.java b/springfox/src/main/java/com/baeldung/springfox/model/User.java index 3f94b8b3c8..20e74016f3 100644 --- a/springfox/src/main/java/com/baeldung/springfox/model/User.java +++ b/springfox/src/main/java/com/baeldung/springfox/model/User.java @@ -8,41 +8,41 @@ import javax.validation.constraints.NotNull; import javax.persistence.Entity; @Entity -public class User{ - +public class User { + @Id private Long id; - + @NotNull(message = "First Name cannot be null") private String firstName; - + @Min(value = 15, message = "Age should not be less than 15") @Max(value = 65, message = "Age should not be greater than 65") private int age; - + @Email(regexp=".@.\\..*", message = "Email should be valid") private String email; - + public Long getId() { return id; } - + public void setId(Long id) { this.id = id; } - + public String getFirstName() { return firstName; } - + public void setFirstName(String firstName) { this.firstName = firstName; } - + public String getEmail() { return email; } - + public void setEmail(String email) { this.email = email; } @@ -54,5 +54,5 @@ public class User{ public void setAge(int age) { this.age = age; } - - } + +} From 70190aff497d4bad260eca96c63988a0426cd11e Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sun, 1 Dec 2019 18:00:55 +0200 Subject: [PATCH 11/13] PR fix --- .../main/java/com/baeldung/springfox/SpringfoxApplication.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java b/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java index aaf732a8bb..03f20c5d0a 100644 --- a/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java +++ b/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java @@ -50,7 +50,8 @@ public class SpringfoxApplication { } private ApiInfo apiInfo() { - return new ApiInfo("Springfox API specification", + return new ApiInfo( + "Springfox API specification", "User REST and Spring Data APIs", "", "", From 1ddfc8769a7d674d602ef8364eb007661b8eb252 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sun, 8 Dec 2019 15:45:41 +0200 Subject: [PATCH 12/13] Springfox improvement code moved to spring-boot-mvc module --- spring-boot-mvc/pom.xml | 32 ++++++- .../configuration/SpringFoxConfig.java | 17 +++- .../baeldung/swagger2boot}/model/User.java | 2 +- .../plugin/EmailAnnotationPlugin.java | 2 +- .../repository/UserRepository.java | 4 +- springfox/pom.xml | 94 ------------------- .../springfox/SpringfoxApplication.java | 69 -------------- .../springfox/controller/UserController.java | 39 -------- .../src/main/resources/application.properties | 3 - 9 files changed, 48 insertions(+), 214 deletions(-) rename {springfox/src/main/java/com/baeldung/springfox => spring-boot-mvc/src/main/java/com/baeldung/swagger2boot}/model/User.java (96%) rename {springfox/src/main/java/com/baeldung/springfox => spring-boot-mvc/src/main/java/com/baeldung/swagger2boot}/plugin/EmailAnnotationPlugin.java (96%) rename {springfox/src/main/java/com/baeldung/springfox => spring-boot-mvc/src/main/java/com/baeldung/swagger2boot}/repository/UserRepository.java (68%) delete mode 100644 springfox/pom.xml delete mode 100644 springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java delete mode 100644 springfox/src/main/java/com/baeldung/springfox/controller/UserController.java delete mode 100644 springfox/src/main/resources/application.properties diff --git a/spring-boot-mvc/pom.xml b/spring-boot-mvc/pom.xml index a414ed7bb2..a825a167ad 100644 --- a/spring-boot-mvc/pom.xml +++ b/spring-boot-mvc/pom.xml @@ -14,6 +14,15 @@ 0.0.1-SNAPSHOT ../parent-boot-2 + + + + + jcenter-snapshots + jcenter + http://oss.jfrog.org/artifactory/oss-snapshot-local/ + + @@ -34,7 +43,14 @@ org.springframework.boot spring-boot-starter-thymeleaf - + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + @@ -82,6 +98,18 @@ springfox-swagger-ui ${spring.fox.version} + + + io.springfox + springfox-data-rest + ${spring.fox.version} + + + + io.springfox + springfox-bean-validators + ${spring.fox.version} + org.apache.tomcat.embed @@ -117,7 +145,7 @@ - 2.9.2 + 3.0.0-SNAPSHOT 1.10.0 2.3.7 diff --git a/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/configuration/SpringFoxConfig.java b/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/configuration/SpringFoxConfig.java index 68e2c55deb..434a8d77cb 100644 --- a/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/configuration/SpringFoxConfig.java +++ b/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/configuration/SpringFoxConfig.java @@ -1,22 +1,29 @@ package com.baeldung.swagger2boot.configuration; +import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +import com.baeldung.swagger2boot.plugin.EmailAnnotationPlugin; + +import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger.web.*; -import springfox.documentation.swagger2.annotations.EnableSwagger2; +import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; import java.util.Collections; @Configuration -@EnableSwagger2 -@ComponentScan("com.baeldung.swaggerboot.controller") +@EnableSwagger2WebMvc +@Import({SpringDataRestConfiguration.class, BeanValidatorPluginsConfiguration.class}) public class SpringFoxConfig { private ApiInfo apiInfo() { @@ -65,4 +72,8 @@ public class SpringFoxConfig { .build(); } + @Bean + public EmailAnnotationPlugin emailPlugin() { + return new EmailAnnotationPlugin(); + } } diff --git a/springfox/src/main/java/com/baeldung/springfox/model/User.java b/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/model/User.java similarity index 96% rename from springfox/src/main/java/com/baeldung/springfox/model/User.java rename to spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/model/User.java index 20e74016f3..b724031536 100644 --- a/springfox/src/main/java/com/baeldung/springfox/model/User.java +++ b/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/model/User.java @@ -1,4 +1,4 @@ -package com.baeldung.springfox.model; +package com.baeldung.swagger2boot.model; import javax.persistence.Id; import javax.validation.constraints.Email; diff --git a/springfox/src/main/java/com/baeldung/springfox/plugin/EmailAnnotationPlugin.java b/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/plugin/EmailAnnotationPlugin.java similarity index 96% rename from springfox/src/main/java/com/baeldung/springfox/plugin/EmailAnnotationPlugin.java rename to spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/plugin/EmailAnnotationPlugin.java index 2e5e57aec5..22ca144fb4 100644 --- a/springfox/src/main/java/com/baeldung/springfox/plugin/EmailAnnotationPlugin.java +++ b/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/plugin/EmailAnnotationPlugin.java @@ -1,4 +1,4 @@ -package com.baeldung.springfox.plugin; +package com.baeldung.swagger2boot.plugin; import static springfox.bean.validators.plugins.Validators.annotationFromBean; diff --git a/springfox/src/main/java/com/baeldung/springfox/repository/UserRepository.java b/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/repository/UserRepository.java similarity index 68% rename from springfox/src/main/java/com/baeldung/springfox/repository/UserRepository.java rename to spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/repository/UserRepository.java index 16303bc6d0..0cbffa06d4 100644 --- a/springfox/src/main/java/com/baeldung/springfox/repository/UserRepository.java +++ b/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/repository/UserRepository.java @@ -1,9 +1,9 @@ -package com.baeldung.springfox.repository; +package com.baeldung.swagger2boot.repository; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; -import com.baeldung.springfox.model.User; +import com.baeldung.swagger2boot.model.User; @Repository public interface UserRepository extends CrudRepository { diff --git a/springfox/pom.xml b/springfox/pom.xml deleted file mode 100644 index c620e49a3d..0000000000 --- a/springfox/pom.xml +++ /dev/null @@ -1,94 +0,0 @@ - - - 4.0.0 - com.baeldung.web - springfox - springfox - SpringFox - war - - - parent-boot-2 - com.baeldung - 0.0.1-SNAPSHOT - ../parent-boot-2 - - - - - jcenter - jcenter - https://jcenter.bintray.com/ - - - - jcenter-snapshots - jcenter - http://oss.jfrog.org/artifactory/oss-snapshot-local/ - - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-data-rest - - - org.springframework.boot - spring-boot-starter-data-jpa - - - com.h2database - h2 - - - - - io.springfox - springfox-swagger2 - 3.0.0-SNAPSHOT - - - io.springfox - springfox-swagger-ui - 3.0.0-SNAPSHOT - - - io.springfox - springfox-data-rest - 3.0.0-SNAPSHOT - - - io.springfox - springfox-bean-validators - 3.0.0-SNAPSHOT - - - - - com.google.guava - guava - ${guava.version} - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - com.baeldung.springfox.SpringfoxApplication - 27.0.1-jre - - diff --git a/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java b/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java deleted file mode 100644 index 03f20c5d0a..0000000000 --- a/springfox/src/main/java/com/baeldung/springfox/SpringfoxApplication.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.baeldung.springfox; - -import static springfox.documentation.builders.PathSelectors.regex; - -import java.util.Collections; -import java.util.function.Predicate; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.domain.EntityScan; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Import; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; - -import com.baeldung.springfox.plugin.EmailAnnotationPlugin; - -import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration; -import springfox.documentation.spring.web.plugins.Docket; -import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; - - -@SpringBootApplication -@EnableSwagger2WebMvc -@EntityScan("com.baeldung.springfox.model") -@ComponentScan("com.baeldung.springfox.controller") -@EnableJpaRepositories("com.baeldung.springfox.repository") -@Import({SpringDataRestConfiguration.class, BeanValidatorPluginsConfiguration.class}) -public class SpringfoxApplication { - - public static void main(String[] args) { - SpringApplication.run(SpringfoxApplication.class, args); - } - - @Bean - public Docket springfoxAppInfo() { - return new Docket(DocumentationType.SWAGGER_2) - .groupName("baeldung-springfox-api") - .select() - .paths(paths()) - .build() - .apiInfo(apiInfo()); - } - - private Predicate paths() { - return regex("/users.*").or(regex("/api.*")); - } - - private ApiInfo apiInfo() { - return new ApiInfo( - "Springfox API specification", - "User REST and Spring Data APIs", - "", - "", - null, - "License of API", - "API license URL", - Collections.emptyList()); - } - - @Bean - public EmailAnnotationPlugin emailPlugin() { - return new EmailAnnotationPlugin(); - } - -} diff --git a/springfox/src/main/java/com/baeldung/springfox/controller/UserController.java b/springfox/src/main/java/com/baeldung/springfox/controller/UserController.java deleted file mode 100644 index d70cc3e7a5..0000000000 --- a/springfox/src/main/java/com/baeldung/springfox/controller/UserController.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.springfox.controller; - -import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; - -import com.baeldung.springfox.model.User; -import com.baeldung.springfox.repository.UserRepository; - -@Controller -@RequestMapping(value = "/api/user", produces = MediaType.APPLICATION_JSON_VALUE) -public class UserController { - - @Autowired - private UserRepository userRepository; - - @PostMapping - public @ResponseBody ResponseEntity createUser(@RequestBody User user) { - userRepository.save(user); - return new ResponseEntity<>(user, HttpStatus.OK); - } - - @GetMapping - public @ResponseBody ResponseEntity getUser(@RequestParam Long id) { - Optional user = userRepository.findById(id); - return new ResponseEntity<>(user.get(), HttpStatus.OK); - } - -} diff --git a/springfox/src/main/resources/application.properties b/springfox/src/main/resources/application.properties deleted file mode 100644 index 3b4076297b..0000000000 --- a/springfox/src/main/resources/application.properties +++ /dev/null @@ -1,3 +0,0 @@ -### Spring Boot default error handling configurations -#server.error.whitelabel.enabled=false -#server.error.include-stacktrace=always From e768874083649688c53f7c6c98f71af1a1ac0e41 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Sun, 8 Dec 2019 15:47:14 +0200 Subject: [PATCH 13/13] springfox module removed --- pom.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pom.xml b/pom.xml index 4966706383..05999f0926 100644 --- a/pom.xml +++ b/pom.xml @@ -621,7 +621,6 @@ tensorflow-java spf4j - springfox spring-boot-config-jpa-error spring-boot-flowable spring-boot-mvc-2 @@ -1249,7 +1248,6 @@ rxjava-libraries oauth2-framework-impl spf4j - springfox spring-boot-performance spring-boot-properties