11. Response 코드 및 메시지 인터페이스 작성
1. Http Status Code & Message 설명
200
- 성공 : "SU" / "Success."
400
- 유효성 검증 실패 : "VF" / "Validation failed."
- 중복된 이메일 : "DE" / "Duplicated Email."
- 중복된 전화번호 : "DT" / "Duplicate tel_number."
- 중복된 닉네임 : "DN" / "Duplicate nickname."
- 존재하지 않는 유저 : "NU" / "This user does not exist."
- 존재하지 않는 게시물 : "NB" / "This board does not exist."
401
- 로그인 실패 : "SF" / "Login information mismatch."
- 인증 실패 : "AF" / "Authorization Failed."
403
- 권한 없음 : "NP" / "Do not have permission."
500
- 데이터베이스 실패 : "DBE" / "Database Error."
2. ResponseCode.java 작성 (board-backend / src / main / java 하위)
package kr.co.sorin.board_backend.common;
public interface ResponseCode {
// interface 필드는 무조건 public static final로 해야함, 생략해도 public static final로 인식함
// HTTP STATUS 200
String SUCCESS = "SU";
// HTTP STATUS 400
String VALIDATION_FAILED = "VF";
String DUPLICATE_EMAIL = "DE";
String DUPLICATE_TEL_NUMBER = "DT";
String DUPLICATE_NICKNAME = "DN";
String NOT_EXISTED_USER = "NU";
String NOT_EXISTED_BOARD = "NB";
// HTTP STATUS 401
String SIGN_IN_FAILED = "SF";
String AUTHORIZATION_FAILED = "AF";
// HTTP STATUS 403
String NO_PERMISSION = "NP";
// HTTP STATUS 500
String DATABASE_ERROR = "DBE";
}
3. ResponseMessage.java 작성
package kr.co.sorin.board_backend.common;
public interface ResponseMessage {
// HTTP STATUS 200
String SUCCESS = "Success.";
// HTTP STATUS 400
String VALIDATION_FAILED = "Validation failed.";
String DUPLICATE_EMAIL = "Duplicated Email.";
String DUPLICATE_TEL_NUMBER = "Duplicate tel_number.";
String DUPLICATE_NICKNAME = "Duplicate nickname.";
String NOT_EXISTED_USER = "This user does not exist.";
String NOT_EXISTED_BOARD = "This board does not exist.";
// HTTP STATUS 401
String SIGN_IN_FAILED = "Login information mismatch.";
String AUTHORIZATION_FAILED = "Authorization Failed.";
// HTTP STATUS 403
String NO_PERMISSION = "Do not have permission.";
// HTTP STATUS 500
String DATABASE_ERROR = "Database Error.";
}
4. WebSecurityConfig.java 수정
class FailedAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.getWriter().write("{ \"code\": \"NP\", \"message\": \"Do not have Permission.\" }");
}
}
-->
class FailedAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().write("{ \"code\": \"AF\", \"message\": \"Do not have Permission.\" }");
}
}
5. ResponseDto.java 작성
package kr.co.sorin.board_backend.dto.response;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import kr.co.sorin.board_backend.common.ResponseCode;
import kr.co.sorin.board_backend.common.ResponseMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class ResponseDto {
private String code;
private String message;
// 상속 및 확장하여 사용
public static ResponseEntity<ResponseDto> databaseError() {
ResponseDto responseBody = new ResponseDto(ResponseCode.DATABASE_ERROR, ResponseMessage.DATABASE_ERROR);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseBody);
}
}
6. board-frontend / src / tsconfig.json 수정 : "baseUrl": "./src", 추가
{
"compilerOptions": {
"target": "es5",
"baseUrl": "./src",
"lib": ["dom", "dom.iterable", "esnext"],
...
}
}
7. board-frontend / src / apis / response / index.ts 작성
import ResponseDto from "./Response.dto";
export type { ResponseDto }; // interface는 내보낼 때, export 다음에, type을 써줘야함
8. board-frontend / src / apis / response / Response.dto.ts 작성
import { ResponseCode } from "types/enum";
export default interface ResponseDto {
code: ResponseCode;
message: string;
}
9. board-frontend / src / types / enum / index.ts 작성
import ResponseCode from "./response-code.enum";
export { ResponseCode }; // enum은 export 다음에 type 안써줘도 됨
10. board-frontend / src / types / enum / response-code.enum.ts 작성
enum ResponseCode {
// HTTP STATUS 200
SUCCESS = "SU",
VALIDATION_FAILED = "VF",
DUPLICATE_EMAIL = "DE",
DUPLICATE_TEL_NUMBER = "DT",
DUPLICATE_NICKNAME = "DN",
NOT_EXISTED_USER = "NU",
NOT_EXISTED_BOARD = "NB",
// HTTP STATUS 401
SIGN_IN_FAILED = "SF",
AUTHORIZATION_FAILED = "AF",
// HTTP STATUS 403
NO_PERMISSION = "NP",
// HTTP STATUS 500
DATABASE_ERROR = "DBE",
}
export default ResponseCode;
'Programming > Web Projects' 카테고리의 다른 글
SPRING+REACT+MYSQL 프로젝트 (10) (2) | 2024.09.08 |
---|---|
SPRING+REACT+MYSQL 프로젝트 (5, 6, 7, 8, 9) (2) | 2024.09.07 |
SPRING+REACT+MYSQL 프로젝트 (3, 4) (1) | 2024.09.07 |
SPRING+REACT+MYSQL 프로젝트 (2) (1) | 2024.09.03 |
SPRING+REACT+MYSQL 프로젝트 (1) (1) | 2024.09.03 |