Skip to content

Commit

Permalink
Add swagger2
Browse files Browse the repository at this point in the history
  • Loading branch information
loda-kun committed Dec 11, 2019
1 parent 9d0fde8 commit ae45d9b
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 206 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
<module>spring-boot-5-Component-Scan</module>
<module>spring-boot-6-@configuration-@Bean</module>
<module>spring-boot-7-spring-application-properties-@Value</module>
<module>spring-boot-swagger</module>
<module>spring-boot-8-@Controller-web-helloworld</module>
<module>spring-boot-@Lazy-Anotation</module>
<module>spring-boot-9-thymeleaf</module>
Expand All @@ -93,6 +92,7 @@
<module>jpa-hibernate-pageable</module>
<module>jpa-hibernate-specifications</module>
<module>jpa-hibernate-criteria</module>
<module>spring-boot-swagger2</module>
</modules>

<properties>
Expand Down
31 changes: 0 additions & 31 deletions spring-boot-swagger/pom.xml

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Empty file removed spring-boot-swagger/src/readme.md
Empty file.
40 changes: 40 additions & 0 deletions spring-boot-swagger2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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">
<parent>
<artifactId>spring-boot-learning</artifactId>
<groupId>me.loda.spring</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-swagger2</artifactId>

<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

<!--spring jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--in memory database-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Swagger2Application {
public class App {
public static void main(String[] args) {
SpringApplication.run(Swagger2Application.class);
SpringApplication.run(App.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.loda.spring.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
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.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("me.loda.spring.swagger.controller"))
.paths(PathSelectors.regex("/.*"))
.build().apiInfo(apiEndPointsInfo());
}
private ApiInfo apiEndPointsInfo() {
return new ApiInfoBuilder().title("Spring Boot REST API")
.description("Employee Management REST API")
.contact(new Contact("loda", "https://loda.me/", "[email protected]"))
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
.version("1.0.0")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package me.loda.spring.swagger.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import me.loda.spring.swagger.model.Employee;
import me.loda.spring.swagger.repository.EmployeeRepository;

@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class EmployeeController {
private final EmployeeRepository employeeRepository;

@GetMapping("/employees")
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}

@GetMapping("/employees/{id}")
public Employee getEmployee(@PathVariable("id") Long id) {
return employeeRepository.findById(id).orElse(new Employee());
}

@PostMapping("/employees")
public Employee createEmployee(@Valid @RequestBody Employee employee) {
return employeeRepository.save(employee);
}

@PutMapping("/employees/{id}")
public Employee updateEmployee(@PathVariable("id") Long id, @Valid @RequestBody Employee employee) {
employee.setId(id);
return employeeRepository.save(employee);
}

@DeleteMapping("/employees/{id}")
public void deleteEmployee(@PathVariable("id") Long id) {
employeeRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.loda.spring.swagger.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Entity
@Table
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String firstName;
private String lastName;
private String email;
}
Loading

0 comments on commit ae45d9b

Please sign in to comment.