Skip to content

Commit

Permalink
swagger 2 👟
Browse files Browse the repository at this point in the history
  • Loading branch information
loda-kun committed Dec 13, 2019
1 parent ae45d9b commit bbc7d2f
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 80 deletions.
27 changes: 13 additions & 14 deletions example-independent-maven-spring-project/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.junit.vintage</groupId>-->
<!-- <artifactId>junit-vintage-engine</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>

<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>
Expand All @@ -59,14 +66,6 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-jpamodelgen -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.4.9.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
10 changes: 10 additions & 0 deletions spring-boot-swagger2/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Source
Vào link để xem chi tiết có hình ảnh minh họa:

[Loda.me - RESTful API Document Tạo với Spring Boot + Swagger][loda-link]

[loda-link]: https://loda.me/res-tful-api-document-tao-voi-spring-boot-swagger-loda1576222065972

# Content without images

### Giới thiệu
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Copyright 2019 {@author Loda} (https://loda.me).
* This project is licensed under the MIT license.
*
* @since 2019-06-06
* Github: https://github.com/loda-kun
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
* Copyright 2019 {@author Loda} (https://loda.me).
* This project is licensed under the MIT license.
*
* @since 2019-06-06
* Github: https://github.com/loda-kun
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
Expand All @@ -20,8 +26,10 @@ public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("me.loda.spring.swagger.controller"))
.paths(PathSelectors.regex("/.*"))
.build().apiInfo(apiEndPointsInfo());
.build()
.apiInfo(apiEndPointsInfo());
}

private ApiInfo apiEndPointsInfo() {
return new ApiInfoBuilder().title("Spring Boot REST API")
.description("Employee Management REST API")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import me.loda.spring.swagger.model.User;
import me.loda.spring.swagger.repository.UserRepository;
/**
* Copyright 2019 {@author Loda} (https://loda.me).
* This project is licensed under the MIT license.
*
* @since 2019-06-06
* Github: https://github.com/loda-kun
*/
@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
@Api(value = "User APIs")
public class UserController {
private final UserRepository userRepository;

@ApiOperation(value = "Xem danh sách User", response = List.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Thành công"),
@ApiResponse(code = 401, message = "Chưa xác thực"),
@ApiResponse(code = 403, message = "Truy cập bị cấm"),
@ApiResponse(code = 404, message = "Không tìm thấy")
})
@GetMapping("/users")
public List<User> getAllUsers() {
return userRepository.findAll();
}

@GetMapping("/users/{id}")
public User getUser(@PathVariable("id") Long id) {
return userRepository.findById(id).orElse(new User());
}

@PostMapping("/users")
public User createUser(
@ApiParam(value = "Đối tượng User cần tạo mới", required = true) @Valid @RequestBody User user
) {
return userRepository.save(user);
}

@PutMapping("/users/{id}")
public User updateUser(@PathVariable("id") Long id, @Valid @RequestBody User user) {
user.setId(id);
return userRepository.save(user);
}

@DeleteMapping("/users/{id}")
public void deleteUser(@PathVariable("id") Long id) {
userRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@
import javax.persistence.Id;
import javax.persistence.Table;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Copyright 2019 {@author Loda} (https://loda.me).
* This project is licensed under the MIT license.
*
* @since 2019-06-06
* Github: https://github.com/loda-kun
*/
@Data
@Entity
@Table
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
@ApiModel(value = "User model")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty(notes = "The database generated User ID")
private Long id;

private String firstName;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.loda.spring.swagger.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import me.loda.spring.swagger.model.User;
/**
* Copyright 2019 {@author Loda} (https://loda.me).
* This project is licensed under the MIT license.
*
* @since 2019-06-06
* Github: https://github.com/loda-kun
*/
@Repository
public interface UserRepository extends JpaRepository<User, Long> {

}

0 comments on commit bbc7d2f

Please sign in to comment.