exception : handle exception(api)
This commit is contained in:
@@ -6,7 +6,7 @@ import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
//@Component
|
||||
@Component
|
||||
public class WebServerCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
|
||||
@Override
|
||||
public void customize(ConfigurableWebServerFactory factory) {
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package hello.exception.api;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class ApiExceptionController {
|
||||
|
||||
@GetMapping("/api/members/{id}")
|
||||
public MemberDto getMember(@PathVariable("id") String id) {
|
||||
|
||||
if (id.equals("ex")) {
|
||||
throw new RuntimeException("잘못된 사용자");
|
||||
}
|
||||
|
||||
return new MemberDto(id, "hello " + id);
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
static class MemberDto {
|
||||
private String memberId;
|
||||
private String name;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,17 @@
|
||||
package hello.exception.servlet;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Controller
|
||||
@@ -33,6 +39,19 @@ public class ErrorPageController {
|
||||
return "error-page/500";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/error-page/500", produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public ResponseEntity<Map<String, Object>> errorPage500Api(HttpServletRequest request, HttpServletResponse response) {
|
||||
log.info("API errorPage 500");
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
Exception ex = (Exception) request.getAttribute(ERROR_EXCEPTION);
|
||||
result.put("status", request.getAttribute(ERROR_STATUS_CODE));
|
||||
result.put("message", ex.getMessage());
|
||||
Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
|
||||
|
||||
return new ResponseEntity<>(result, HttpStatus.valueOf(statusCode));
|
||||
}
|
||||
|
||||
private void printErrorInfo(HttpServletRequest request) {
|
||||
log.info("ERROR_EXCEPTION : {}", request.getAttribute(ERROR_EXCEPTION));
|
||||
log.info("ERROR_EXCEPTION_TYPE : {}", request.getAttribute(ERROR_EXCEPTION_TYPE));
|
||||
|
||||
Reference in New Issue
Block a user