springboot restcontroller requestparam 설정
@GetMapping(value = "/test")
public Map<String, Object> accounts(
@RequestParam(value = "code") String code,
@RequestParam(value = "page" , required = false, defaultValue = "0") int page,
@RequestParam Map<String, Object> req) throws Exception {
@RequestParam 필수값 exception handler
잘못된 예
@ControllerAdvice
@RestController
public class ResponseExceptionHandler extends ResponseEntityExceptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public Object handleValidationExceptions(
Exception ex) {
return ex.getMessage()
}
@ExceptionHandler(MissingServletRequestParameterException.class)
public Object handleMissingServletRequestParameterException(
MissingServletRequestParameterException ex) {
return "parameter is null : " + ex.getParameterName()
}
}
기동에러
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'handlerExceptionResolver' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation...5.3.25.jar:5.3.25]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerExceptionResolver]: Factory method 'handlerExceptionResolver' threw exception;
Caused by: java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.bind.MissingServletRequestParameterException]:
에러 발생 사유
ResponseEntityExceptionHandler 에
@ExceptionHandler(MissingServletRequestParameterException.class)를 처리 하고 있어서
에러 수정
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
@Slf4j
@ControllerAdvice
@RestController
public class ResponeExceptionHandler {
@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseBody
public String handleMissingServletRequestParameterException(
MissingServletRequestParameterException ex) {
return "paramer is null : " + ex.getParameterName();
}
@ExceptionHandler(Exception.class)
@ResponseBody
public String handleAllExceptions(Exception ex) {
return ex.getMessage();
}
}
extends ResponseEntityExceptionHandler 클래스와 함께 사용하면 안됨.
'skill > Java.Kotlin' 카테고리의 다른 글
[Error] ClassNotFoundException: org.apache.http.impl.client.HttpClients (0) | 2023.02.22 |
---|---|
Springboot request param VO class로 받을 시, 필수 값 null일 경우 exception handler 처리 (0) | 2023.02.01 |
[Kotlin] 배열 만들기 List<Int>, List<String> (0) | 2022.04.27 |
Kotlin Scheduler (batch) (0) | 2022.02.07 |
[SpringBoot] Completed 405 METHOD_NOT_ALLOWED Error 해결 (0) | 2021.06.12 |