Springfox improvement code moved to spring-boot-mvc module

This commit is contained in:
Anshul BANSAL 2019-12-08 15:45:41 +02:00
parent 70190aff49
commit 1ddfc8769a
9 changed files with 48 additions and 214 deletions

View File

@ -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>

View File

@ -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();
}
} }

View File

@ -1,4 +1,4 @@
package com.baeldung.springfox.model; package com.baeldung.swagger2boot.model;
import javax.persistence.Id; import javax.persistence.Id;
import javax.validation.constraints.Email; import javax.validation.constraints.Email;

View File

@ -1,4 +1,4 @@
package com.baeldung.springfox.plugin; package com.baeldung.swagger2boot.plugin;
import static springfox.bean.validators.plugins.Validators.annotationFromBean; import static springfox.bean.validators.plugins.Validators.annotationFromBean;

View File

@ -1,9 +1,9 @@
package com.baeldung.springfox.repository; package com.baeldung.swagger2boot.repository;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import com.baeldung.springfox.model.User; import com.baeldung.swagger2boot.model.User;
@Repository @Repository
public interface UserRepository extends CrudRepository<User, Long> { public interface UserRepository extends CrudRepository<User, Long> {

View File

@ -1,94 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.web</groupId>
<artifactId>springfox</artifactId>
<name>springfox</name>
<description>SpringFox</description>
<packaging>war</packaging>
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<repositories>
<repository>
<id>jcenter</id>
<name>jcenter</name>
<url>https://jcenter.bintray.com/</url>
</repository>
<!-- Snapshot repository location -->
<repository>
<id>jcenter-snapshots</id>
<name>jcenter</name>
<url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- Springfox -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<!-- util -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<start-class>com.baeldung.springfox.SpringfoxApplication</start-class>
<guava.version>27.0.1-jre</guava.version>
</properties>
</project>

View File

@ -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<String> 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();
}
}

View File

@ -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<User> createUser(@RequestBody User user) {
userRepository.save(user);
return new ResponseEntity<>(user, HttpStatus.OK);
}
@GetMapping
public @ResponseBody ResponseEntity<User> getUser(@RequestParam Long id) {
Optional<User> user = userRepository.findById(id);
return new ResponseEntity<>(user.get(), HttpStatus.OK);
}
}

View File

@ -1,3 +0,0 @@
### Spring Boot default error handling configurations
#server.error.whitelabel.enabled=false
#server.error.include-stacktrace=always