forked from TranDatk/spring-boot-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
141 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 0 additions & 51 deletions
51
spring-boot-swagger2/src/main/java/me/loda/spring/swagger/controller/EmployeeController.java
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
spring-boot-swagger2/src/main/java/me/loda/spring/swagger/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
spring-boot-swagger2/src/main/java/me/loda/spring/swagger/repository/EmployeeRepository.java
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
spring-boot-swagger2/src/main/java/me/loda/spring/swagger/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
|
||
} |