반응형
스웨거 (Swagger)란?
- 스웨거(Swagger)는 개발자가 REST API 서비스를 설계, 빌드, 문서화할 수 있도록 하는 프로젝트 이다.
- 스웨거는REST API를 문서화하는 도구이다.
- API에 대한명세(Spec)을 관리하기 위한 프로젝트이다.
- API가 수정되더라도문서가 자동으로 갱신된다
pom.xml
- swagger 관련 라이브러리 추가
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Class
- Project에 config pakage추가후 SwaggerConfig추가 함
- PathSelectors.ant("/employees/**") 이 부분은 내가 원하는 api를 넣어주면됌
package com.dursuneryilmaz.employeemanagement.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@SuppressWarnings("desprecation")
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("TEST EMP API")
.description("Spring Boot - GSN SA TEST EMP API")
.version("V1")
.build();
}
@Bean
public Docket api() {
String version = "V1";
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.apiInfo(apiInfo())
.groupName(version)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/employees/**"))
.build();
}
}
Controller
package com.dursuneryilmaz.employeemanagement.controller;
import com.dursuneryilmaz.employeemanagement.domain.Employee;
import com.dursuneryilmaz.employeemanagement.service.IEmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("")
@CrossOrigin()
public class EmployeeController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private IEmployeeService employeeService;
//get all employees
@GetMapping("/employees")
@ApiOperation(value ="emp list", notes = "회원 리스트")
public List<Employee> getAll() {
logger.debug("employ_list");
return employeeService.getAll();
}
//add employee
@PostMapping("/employees")
@ApiOperation(value ="emp Add", notes = "회원 추가")
public Employee createEmployee(@RequestBody Employee employee) {
logger.trace("employ_add");
logger.debug("employ_add");
logger.info("employ_add");
logger.warn("employ_add");
logger.error("employ_add");
return employeeService.add(employee);
}
//get employee by id
@GetMapping("/employees/{id}")
@ApiOperation(value ="emp Get", notes = "회원정보 확인")
public ResponseEntity<Employee> getById(@PathVariable Long id) {
logger.debug("employ_getid");
Employee employee = employeeService.findById(id);
return ResponseEntity.ok(employee);
}
//update employee
@PutMapping("/employees")
@ApiOperation(value ="emp Update", notes = "회원 업데이트")
public ResponseEntity<Employee> update(@RequestBody Employee employee) {
logger.debug("employ_update");
Employee employeeUpdated = employeeService.update(employee);
return ResponseEntity.ok(employeeUpdated);
}
//delete employee
@PostMapping("/employees/delete")
@ApiOperation(value ="emp Delete", notes = "회원 삭제")
public ResponseEntity<String> delete(@RequestBody Employee employee) {
logger.debug("employ_delet ");
employeeService.delete(employee);
return ResponseEntity.ok("Employee deleted.");
}
// deleteById
@DeleteMapping("employees/{id}")
@ApiOperation(value ="emp Delete Byid", notes = "회원 삭제")
public ResponseEntity<Map<String,Boolean>> deleteById(@PathVariable Long id){
logger.debug("employ_delet_byid");
employeeService.deleteById(id);
Map<String,Boolean> response = new HashMap<>();
response.put("Deleted", Boolean.TRUE);
return ResponseEntity.ok(response);
}
}
반응형
'IT' 카테고리의 다른 글
맥북 사용자를 위한 해피해킹 세팅(딥스위치, 한영키/물결키) 방법 (10) | 2021.09.02 |
---|---|
Linux/Windows EOS, EOL 정리 (0) | 2021.08.04 |
Visual Studio Code 유용한 단축키 모음 (0) | 2021.08.04 |
Message Qeue란?(Kafka vs RabbitMQ vs ActiveMQ) (0) | 2021.08.03 |
[DevOps]Docker(도커)란? (0) | 2021.05.21 |