DCAMP FRONTEND API 개발 가이드 » 이력 » 버전 7
최시은, 2023-05-26 00:03
1 | 1 | 최시은 | h2. DCAMP FRONTEND API 개발 가이드 |
---|---|---|---|
2 | |||
3 | 3 | 최시은 | 1. SecurityConfig 설정(security/config/SecurityConfig.java) |
4 | 1 | 최시은 | * securityFilterChain 메소드 - 개발 진행할 URL 에 대한 hasAuthority 룰 추가 |
5 | <pre><code class="java"> |
||
6 | http.csrf().disable() |
||
7 | .authorizeHttpRequests((authorize) -> |
||
8 | 2 | 최시은 | authorize |
9 | // hasAuthority 사용: db에 ROLE_ 안붙여도 됨 |
||
10 | .requestMatchers("/projects/**").hasAuthority("ADMIN") // 관리자 : ADMIN | 작업자&검수자 : USER |
||
11 | .anyRequest().authenticated() |
||
12 | 1 | 최시은 | </code></pre> |
13 | 3 | 최시은 | |
14 | 2. Swagger 사용 |
||
15 | * 설정 파일 위치 : common/config/SwaggerConfig.java (이미 설정되어 있으므로 따로 추가사항 없음) |
||
16 | * 접속 URL : http://localhost:18080/swagger-ui/index.html |
||
17 | login-controller 에서 로그인 후, 응답 토큰을 우상단 Authorized 버튼 클릭해서 입력하면 인증 가능 |
||
18 | 4 | 최시은 | |
19 | 3. Request URL Naming |
||
20 | 5 | 최시은 | * RESTful API 기반 : https://restfulapi.net/resource-naming/ |
21 | * 1 Depth : 대메뉴(ex, project, user 등등) |
||
22 | <pre>http://api.example.com/user-management/users |
||
23 | http://api.example.com/user-management/users/{id}</pre> |
||
24 | 6 | 최시은 | |
25 | 4. DTO 폴더 |
||
26 | * 정의 : API 통신용 Request, Response 정의 |
||
27 | * 공통 DTO 클래스 |
||
28 | ** CommonRes : 공통 응답필드(result, message) 정의, 상황에 따른 payload 세팅 가능 |
||
29 | <pre><code class="java"> |
||
30 | @PostMapping |
||
31 | public ResponseEntity<CommonRes> save(@AuthenticationPrincipal UserDetails userDetails, @RequestBody PostProjectReq req) throws Exception { |
||
32 | Project project = projectService.createProject(req, userDetails); |
||
33 | CommonRes response = project != null ? new CommonRes() : CommonRes.builder().result(false).message("오류가 발생했습니다.").build(); |
||
34 | return new ResponseEntity<>(response, HttpStatus.OK); |
||
35 | } |
||
36 | </code></pre> |
||
37 | 7 | 최시은 | ** CommonListRes : 페이징 목록 조회 시 Response 객체로 사용 |
38 | <pre><code class="java"> |
||
39 | @GetMapping |
||
40 | public ResponseEntity<CommonListRes<GetProjectsRes>> list(GetProjectsReq req) throws Exception { |
||
41 | CommonListRes<GetProjectsRes> response = projectService.list(req); |
||
42 | return new ResponseEntity<>(response, HttpStatus.OK); |
||
43 | } |
||
44 | </code></pre> |
||
45 | ** CommonSearch : 페이징 목록의 검색 시 Request 객체가 상속 받아 사용(페이징 정보 담김) |
||
46 | <pre><code class="java"> |
||
47 | @AllArgsConstructor |
||
48 | @NoArgsConstructor |
||
49 | @Setter |
||
50 | @Getter |
||
51 | @EqualsAndHashCode(callSuper = false) |
||
52 | public class GetProjectsReq extends CommonSearch { |
||
53 | 1 | 최시은 | |
54 | 7 | 최시은 | private String projectStatusCcd; |
55 | private String searchType; |
||
56 | private String searchWord; |
||
57 | |||
58 | } |
||
59 | </code></pre> |
||
60 | ** CommonPagination : 페이징 처리를 위해 CommonListRes에서 사용(따로 사용할 일 없음) |