BAEL-3456 - Springfox

This commit is contained in:
Anshul BANSAL 2019-11-25 13:28:54 +02:00
parent e7f1074006
commit 7d2ec82d78
8 changed files with 336 additions and 0 deletions

97
springfox/pom.xml Normal file
View File

@ -0,0 +1,97 @@
<?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>
<!-- Snapshot repository location -->
<repository>
<id>jcenter</id>
<name>jcenter</name>
<url>https://jcenter.bintray.com/</url>
</repository>
<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

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

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

View File

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

View File

@ -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> email = annotationFromBean(context, Email.class);
if (email.isPresent()) {
context.getBuilder().pattern(email.get().regexp());
context.getBuilder().example("email@email.com");
}
}
}

View File

@ -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<User, Long> {
}

View File

@ -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<User, Long> {
}

View File

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