๊ฐ์
API ๋ฅผ ๊ฐ๋ฐํ๊ฑฐ๋, ํน์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ์ดํ๊ธฐ ์ํด ํ์ ๊ฐ์ ์ง์ ํด์ผํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. (ํ๋ผ๋ฏธํฐ์ ๋ํ ์ ํจ์ฑ)
๋ฐ๋ก Rule ์ด ์๋ค๋ฉด ๋ฐฉ๋ฒ์ ๋ฌด๊ถ๋ฌด์งํ๋ค. ๋ชจ๋ ์์ฒญ์ if๋ฅผ ๊ฑธ์ด ๊ฒ์ฌํ๊ฑฐ๋.. ๊ณตํต์ผ๋ก ํ๋์ Class ๋ฅผ ๋ง๋ค์ด์ ๊ฒฝ์ ํ๊ฒ ํ๊ฑฐ๋..
์ด ๊ธ์์๋ ๊น๋ํ ์ฝ๋๋ฅผ ์ํด ํ๋ก์ ํธ ๋ด์์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉํ Exception ์ฒ๋ฆฌ๋ฅผ ํ๊ณ ์ ํ๋ค. ๊ทธ๋์ผ ๊น๋ํ๋ค.
๊ธฐ์กด Spring ์ด๋ Spring Boot์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ์๋์ ๊ฐ์ ๋ด์ฉ์ผ๋ก ๋ฐ์ํ๋ค.
์ฐ๋ฆฌ๋ Spring Boot์์ ์ ๊ณตํ๋ ๊ธฐ๋ณธ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๋ฉ์์ง๋ก ์ ์ดํ ์์ ์ด๋ค.
*์๋ ๊ธ์์๋ ์ฟ ํฐ์ ๋ฐํํ๊ธฐ ์ํ ๋ด์ฉ์ ์๋๋ฆฌ์ค๋ก @Valid ๋ฅผ ๋ฃ์ด ๊ตฌํํ์๋ค.
1. ์ฌ์ ์ค๋น
๐Maven ์ ๊ธฐ์ค์ผ๋ก Validator ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ Lib ๋ฅผ ๋ฏธ๋ฆฌ ๋ฐ์๋๋๋ก ํ๋ค.
์๋๋ pom.xml ์ dependency ๋ด์ฉ์ด๋ค.
<!-- pom.xml -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.2.0.Final</version>
</dependency>
2. ๊ณตํต Exception Contoller ์์ฑ
๐Exception Controller
@Slf4j
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorModule> methodValidException(MethodArgumentNotValidException e, HttpServletRequest request){
log.error("MethodArgumentNotValidException url:{}, trace:{}",request.getRequestURI(), e.getStackTrace());
return new ResponseEntity<>(errorResponse(e.getBindingResult()), HttpStatus.BAD_REQUEST);
}
private ErrorModule errorResponse(BindingResult bindingResult){
//bindingResult.getFieldError().getCode(); : model ์์ ์ค์ ํ annotation
//bindingResult.getFieldError().getField()); : model ์์ ๋ฐ์ํ Error ์์น
//bindingResult.getFieldError().getDefaultMessage(); : model ์์ ์ค์ ํ message
return new ErrorModule(
bindingResult.getFieldError().getField(),
bindingResult.getFieldError().getDefaultMessage());
}
}
3. ๊ณตํต ErrorModule ์์ฑ
๐ErrorModule
import lombok.Data;
@Data
public class ErrorModule {
String code; //enum ์ผ๋ก ์ ์ดํ ๋ชฉ์
String requestType; //ํ์ฅ์ฑ์ ๊ณ ๋ ค, ์์ฒญํ ๊ธฐ๋ฅ์ด ์ด๋ค ๊ฒ์ธ์ง ํ์ธํ๊ธฐ ์ํจ
String field; //๋ฌธ์ ๊ฐ ๋ฐ์ํ field ํ์ธ
String description; //์ค๋ช
String message; //์ถ๋ ฅํ๋ ๋ฉ์์ง
//TODO: ์ง๊ธ์ ์ด๊ฑธ๋ก..
public ErrorModule(String field, String message) {
this.field = field;
this.message = message;
}
public ErrorModule(String requestType, String description, String message) {
this.requestType = requestType;
this.description = description;
this.message = message;
}
public ErrorModule(String code, String requestType, String field, String description, String message) {
this.code = code;
this.requestType = requestType;
this.field = field;
this.description = description;
this.message = message;
}
4. @Valid ์ ์ธ
๐์ ์ด๋ฅผ ํ๊ณ ์ถ์ Parameter์ @Valid๋ฅผ ๋ถ์ฌ์ค๋ค.
Controller
๐จ๐ป๐ปโจ
@ApiOperation(value = "์ฟ ํฐ ๋ฐํ API", notes = "์ฟ ํฐ ๋ฐํ API ๊ธฐ๋ฅ (๊ฑด๋ณ)")
@ApiImplicitParams({
@ApiImplicitParam(name = "token", value = "API ํ ํฐ๊ฐ", dataType = "string", paramType = "header", required = true),
@ApiImplicitParam(name = "company", value = "API ์์๊ฐ", dataType = "string", paramType = "header", required = true),
@ApiImplicitParam(name = "group", value = "API ๊ทธ๋ฃน๊ฐ", dataType = "string", paramType = "header", required = true),
@ApiImplicitParam(name = "type", value = "GIFT: 1ํ๊ถ / RUSE: ๋คํ๊ถ", dataType = "string", paramType = "query", required = true),
@ApiImplicitParam(name = "amount", value = "type.equals('GIFT') ? ์ฟ ํฐ๊ธ์ก : 0", dataType = "long", paramType = "query", required = true),
@ApiImplicitParam(name = "phone", value = "์ฐ๋ฝ์ฒ (-) ํฌํจํ์ฌ ์ ์ก", dataType = "string", paramType = "query", required = true),
})
@PostMapping(value = "/issuance")
@Transactional(timeout = 5)
public ResponseEntity<JSONObject> couponIssuance(
@RequestHeader("token") String token,
@RequestHeader("company") String company,
@RequestHeader("group") String group,
//ํ๋ผ๋ฏธํฐ๋ฅผ Model ๋ก ๋ฐ๊ณ , Model ์์์ ์ ์ดํ๊ณ ์ ํ๋ค.
@RequestBody @Valid CouponModel couponModel
) {
try {
} catch (Exception e) {
e.printStackTrace();
}
if (true) {
return new ResponseEntity<>(new JSONObject(), HttpStatus.OK);
} else {
return new ResponseEntity<>(new JSONObject(), HttpStatus.NOT_FOUND);
}
}
5. ์คํ๊ฒฐ๊ณผ
๐์ผ๋ถ๋ฌ ํ๋ผ๋ฏธํฐ์ธ phone ์ ๋น์ฐ๊ณ ์์ฒญ์ ์ ์กํ ๊ฒฐ๊ณผ์ด๋ค.
์ํ๋ ์๋ต ๊ฐ์ผ๋ก ์ ๋ฟ๋ ค์ฃผ๊ณ ์๋ค.
๋ง์ง๋ง์ ํ์ฅ์ฑ์ ์๊ฐํ์ฌ enum ์ผ๋ก ๊ด๋ฆฌํ ์๊ฐ์ผ๋ก ๋ณ์๋ฅผ ์ถ๊ฐํ์๋ค.
'์ค๋ฅ ์ฝ๋'์ '์ฝ๋์ ๋ฐ๋ฅธ ๋ฌธ๊ตฌ' ์ถ๊ฐํ๋ค๋ฉด ๋์ฑ ๊น๋ํ ๊ตฌ์กฐ๋ฅผ ํ๋ฆฝํ ์ ์์ ๊ฒ ๊ฐ๊ธฐ์..