Merge pull request #8248 from SmartyAnsh/BAEL-3456_Springfox
Bael 3456 springfox
This commit is contained in:
		
						commit
						7a749a5e88
					
				| @ -15,6 +15,15 @@ | |||||||
|         <relativePath>../parent-boot-2</relativePath> |         <relativePath>../parent-boot-2</relativePath> | ||||||
|     </parent> |     </parent> | ||||||
|      |      | ||||||
|  |     <repositories> | ||||||
|  |         <!-- Snapshot repository location --> | ||||||
|  |         <repository> | ||||||
|  |             <id>jcenter-snapshots</id> | ||||||
|  |             <name>jcenter</name> | ||||||
|  |             <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url> | ||||||
|  |         </repository> | ||||||
|  |     </repositories> | ||||||
|  | 
 | ||||||
|     <dependencies> |     <dependencies> | ||||||
| 
 | 
 | ||||||
|         <dependency> |         <dependency> | ||||||
| @ -34,7 +43,14 @@ | |||||||
|             <groupId>org.springframework.boot</groupId> |             <groupId>org.springframework.boot</groupId> | ||||||
|             <artifactId>spring-boot-starter-thymeleaf</artifactId> |             <artifactId>spring-boot-starter-thymeleaf</artifactId> | ||||||
|         </dependency> |         </dependency> | ||||||
| 
 |         <dependency> | ||||||
|  |             <groupId>org.springframework.boot</groupId> | ||||||
|  |             <artifactId>spring-boot-starter-data-jpa</artifactId> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>com.h2database</groupId> | ||||||
|  |             <artifactId>h2</artifactId> | ||||||
|  |         </dependency> | ||||||
| 
 | 
 | ||||||
|         <!--JSF --> |         <!--JSF --> | ||||||
|         <dependency> |         <dependency> | ||||||
| @ -83,6 +99,18 @@ | |||||||
|             <version>${spring.fox.version}</version> |             <version>${spring.fox.version}</version> | ||||||
|         </dependency> |         </dependency> | ||||||
|          |          | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>io.springfox</groupId> | ||||||
|  |             <artifactId>springfox-data-rest</artifactId> | ||||||
|  |             <version>${spring.fox.version}</version> | ||||||
|  |         </dependency> | ||||||
|  |          | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>io.springfox</groupId> | ||||||
|  |             <artifactId>springfox-bean-validators</artifactId> | ||||||
|  |             <version>${spring.fox.version}</version> | ||||||
|  |         </dependency> | ||||||
|  | 
 | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>org.apache.tomcat.embed</groupId> |             <groupId>org.apache.tomcat.embed</groupId> | ||||||
|             <artifactId>tomcat-embed-jasper</artifactId> |             <artifactId>tomcat-embed-jasper</artifactId> | ||||||
| @ -117,7 +145,7 @@ | |||||||
|     </build> |     </build> | ||||||
| 
 | 
 | ||||||
|     <properties> |     <properties> | ||||||
|         <spring.fox.version>2.9.2</spring.fox.version> |         <spring.fox.version>3.0.0-SNAPSHOT</spring.fox.version> | ||||||
|         <!-- ROME for RSS --> |         <!-- ROME for RSS --> | ||||||
|         <rome.version>1.10.0</rome.version> |         <rome.version>1.10.0</rome.version> | ||||||
|         <javax.faces.version>2.3.7</javax.faces.version> |         <javax.faces.version>2.3.7</javax.faces.version> | ||||||
|  | |||||||
| @ -1,22 +1,29 @@ | |||||||
| package com.baeldung.swagger2boot.configuration; | package com.baeldung.swagger2boot.configuration; | ||||||
| 
 | 
 | ||||||
|  | import org.springframework.boot.autoconfigure.domain.EntityScan; | ||||||
| import org.springframework.context.annotation.Bean; | import org.springframework.context.annotation.Bean; | ||||||
| import org.springframework.context.annotation.ComponentScan; | import org.springframework.context.annotation.ComponentScan; | ||||||
| import org.springframework.context.annotation.Configuration; | 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.PathSelectors; | ||||||
| import springfox.documentation.builders.RequestHandlerSelectors; | import springfox.documentation.builders.RequestHandlerSelectors; | ||||||
| import springfox.documentation.service.ApiInfo; | import springfox.documentation.service.ApiInfo; | ||||||
| import springfox.documentation.service.Contact; | import springfox.documentation.service.Contact; | ||||||
| import springfox.documentation.spi.DocumentationType; | import springfox.documentation.spi.DocumentationType; | ||||||
|  | import springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration; | ||||||
| import springfox.documentation.spring.web.plugins.Docket; | import springfox.documentation.spring.web.plugins.Docket; | ||||||
| import springfox.documentation.swagger.web.*; | import springfox.documentation.swagger.web.*; | ||||||
| import springfox.documentation.swagger2.annotations.EnableSwagger2; | import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; | ||||||
| 
 | 
 | ||||||
| import java.util.Collections; | import java.util.Collections; | ||||||
| 
 | 
 | ||||||
| @Configuration | @Configuration | ||||||
| @EnableSwagger2 | @EnableSwagger2WebMvc | ||||||
| @ComponentScan("com.baeldung.swaggerboot.controller") | @Import({SpringDataRestConfiguration.class, BeanValidatorPluginsConfiguration.class}) | ||||||
| public class SpringFoxConfig { | public class SpringFoxConfig { | ||||||
| 
 | 
 | ||||||
|     private ApiInfo apiInfo() { |     private ApiInfo apiInfo() { | ||||||
| @ -65,4 +72,8 @@ public class SpringFoxConfig { | |||||||
|                 .build(); |                 .build(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     @Bean | ||||||
|  |     public EmailAnnotationPlugin emailPlugin() { | ||||||
|  |         return new EmailAnnotationPlugin(); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -0,0 +1,58 @@ | |||||||
|  | package com.baeldung.swagger2boot.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; | ||||||
|  | 
 | ||||||
|  |     @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; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public int getAge() { | ||||||
|  |         return age; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setAge(int age) { | ||||||
|  |         this.age = age; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,39 @@ | |||||||
|  | package com.baeldung.swagger2boot.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> email = annotationFromBean(context, Email.class); | ||||||
|  |         if (email.isPresent()) { | ||||||
|  |             context.getBuilder().pattern(email.get().regexp()); | ||||||
|  |             context.getBuilder().example("email@email.com"); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
| @ -0,0 +1,11 @@ | |||||||
|  | package com.baeldung.swagger2boot.repository; | ||||||
|  | 
 | ||||||
|  | import org.springframework.data.repository.CrudRepository; | ||||||
|  | import org.springframework.stereotype.Repository; | ||||||
|  | 
 | ||||||
|  | import com.baeldung.swagger2boot.model.User; | ||||||
|  | 
 | ||||||
|  | @Repository | ||||||
|  | public interface UserRepository extends CrudRepository<User, Long> { | ||||||
|  |      | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user