diff --git a/springboot-exception-handler/pom.xml b/springboot-exception-handler/pom.xml index 8674076b..ec898a8e 100644 --- a/springboot-exception-handler/pom.xml +++ b/springboot-exception-handler/pom.xml @@ -27,6 +27,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-mustache + org.springframework.boot spring-boot-starter-validation diff --git a/springboot-exception-handler/src/main/java/kr/pe/advenoh/controller/ExceptionTestController.java b/springboot-exception-handler/src/main/java/kr/pe/advenoh/controller/ExceptionTestController.java new file mode 100644 index 00000000..10b6c9dd --- /dev/null +++ b/springboot-exception-handler/src/main/java/kr/pe/advenoh/controller/ExceptionTestController.java @@ -0,0 +1,24 @@ +package kr.pe.advenoh.controller; + +import kr.pe.advenoh.exception.SupportInfoException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Slf4j +@RequiredArgsConstructor +@Controller +@RequestMapping("/api/exception2") +public class ExceptionTestController { + @GetMapping("/supportInfoException") + public String throwCustomException(Model model) { + log.debug("Throw SupportInfoException"); + if (true) { + throw new SupportInfoException("Exception occurred"); + } + return "index"; + } +} diff --git a/springboot-exception-handler/src/main/java/kr/pe/advenoh/controller/ExceptionTestRestController.java b/springboot-exception-handler/src/main/java/kr/pe/advenoh/controller/ExceptionTestRestController.java new file mode 100644 index 00000000..4a047845 --- /dev/null +++ b/springboot-exception-handler/src/main/java/kr/pe/advenoh/controller/ExceptionTestRestController.java @@ -0,0 +1,20 @@ +package kr.pe.advenoh.controller; + +import kr.pe.advenoh.exception.SupportInfoException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Slf4j +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/exception") +public class ExceptionTestRestController { + @GetMapping("/supportInfoException") + public Object throwCustomException() { + log.debug("Throw SupportInfoException"); + throw new SupportInfoException("Exception occurred"); + } +} diff --git a/springboot-exception-handler/src/main/java/kr/pe/advenoh/controller/ViewMappingController.java b/springboot-exception-handler/src/main/java/kr/pe/advenoh/controller/ViewMappingController.java new file mode 100644 index 00000000..171673d4 --- /dev/null +++ b/springboot-exception-handler/src/main/java/kr/pe/advenoh/controller/ViewMappingController.java @@ -0,0 +1,14 @@ +package kr.pe.advenoh.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class ViewMappingController { + + @GetMapping("/") + public String index() { + return "index"; + } +} diff --git a/springboot-exception-handler/src/main/java/kr/pe/advenoh/exception/SupportInfoException.java b/springboot-exception-handler/src/main/java/kr/pe/advenoh/exception/SupportInfoException.java new file mode 100644 index 00000000..46b8643a --- /dev/null +++ b/springboot-exception-handler/src/main/java/kr/pe/advenoh/exception/SupportInfoException.java @@ -0,0 +1,13 @@ +package kr.pe.advenoh.exception; + +public class SupportInfoException extends RuntimeException { + private static final long serialVersionUID = -6234353119945998503L; + + public SupportInfoException(String message) { + super(message); + } + + public SupportInfoException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/springboot-exception-handler/src/main/java/kr/pe/advenoh/spring/config/WebConfig.java b/springboot-exception-handler/src/main/java/kr/pe/advenoh/spring/config/WebConfig.java new file mode 100644 index 00000000..85f5f537 --- /dev/null +++ b/springboot-exception-handler/src/main/java/kr/pe/advenoh/spring/config/WebConfig.java @@ -0,0 +1,8 @@ +package kr.pe.advenoh.spring.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig implements WebMvcConfigurer { +} diff --git a/springboot-exception-handler/src/main/resources/application.properties b/springboot-exception-handler/src/main/resources/application.properties index 9f31294c..18b8caba 100644 --- a/springboot-exception-handler/src/main/resources/application.properties +++ b/springboot-exception-handler/src/main/resources/application.properties @@ -20,4 +20,7 @@ spring.h2.console.path=/h2-console spring.output.ansi.enabled=always logging.level.root=DEBUG logging.level.org.springframework.web=DEBUG -logging.level.org.hibernate=DEBUG \ No newline at end of file +logging.level.org.hibernate=DEBUG + +spring.mustache.prefix=classpath:/templates/ +spring.mustache.suffix=.html \ No newline at end of file diff --git a/springboot-exception-handler/src/main/resources/templates/index.html b/springboot-exception-handler/src/main/resources/templates/index.html new file mode 100644 index 00000000..6f4c5d3a --- /dev/null +++ b/springboot-exception-handler/src/main/resources/templates/index.html @@ -0,0 +1,10 @@ + + + + + Title + + +this is a test + + \ No newline at end of file diff --git a/springboot-exception-handler/src/test/java/kr/pe/advenoh/controller/ExceptionTestControllerTest.java b/springboot-exception-handler/src/test/java/kr/pe/advenoh/controller/ExceptionTestControllerTest.java new file mode 100644 index 00000000..402dab67 --- /dev/null +++ b/springboot-exception-handler/src/test/java/kr/pe/advenoh/controller/ExceptionTestControllerTest.java @@ -0,0 +1,24 @@ +package kr.pe.advenoh.controller; + +import kr.pe.advenoh.utils.SpringMockMvcTestSupport; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +class ExceptionTestControllerTest extends SpringMockMvcTestSupport { + private final String PATH = "/api/exception2"; + + @Test + void throwCustomException() throws Exception { + this.mockMvc.perform(get(PATH + "/supportInfoException2")) + .andDo(print()) + .andExpect(status().isInternalServerError()) + .andExpect(jsonPath("$.message", is("Exception occurred"))); + + } +} \ No newline at end of file diff --git a/springboot-exception-handler/src/test/java/kr/pe/advenoh/controller/ExceptionTestControllerTestRest.java b/springboot-exception-handler/src/test/java/kr/pe/advenoh/controller/ExceptionTestControllerTestRest.java new file mode 100644 index 00000000..64ad1de7 --- /dev/null +++ b/springboot-exception-handler/src/test/java/kr/pe/advenoh/controller/ExceptionTestControllerTestRest.java @@ -0,0 +1,23 @@ +package kr.pe.advenoh.controller; + +import kr.pe.advenoh.utils.SpringMockMvcTestSupport; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +class ExceptionTestControllerTestRest extends SpringMockMvcTestSupport { + private final String PATH = "/api/exception"; + + @Test + void throwCustomException() throws Exception { + this.mockMvc.perform(get(PATH + "/supportInfoException")) + .andDo(print()) + .andExpect(status().isInternalServerError()) + .andExpect(jsonPath("$.message", is("Exception occurred"))); + + } +} \ No newline at end of file diff --git a/springboot-exception-handler/src/test/java/kr/pe/advenoh/utils/SpringBootTestSupport.java b/springboot-exception-handler/src/test/java/kr/pe/advenoh/utils/SpringBootTestSupport.java new file mode 100644 index 00000000..aefc73e4 --- /dev/null +++ b/springboot-exception-handler/src/test/java/kr/pe/advenoh/utils/SpringBootTestSupport.java @@ -0,0 +1,7 @@ +package kr.pe.advenoh.utils; + +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public abstract class SpringBootTestSupport implements TestConfig { +} diff --git a/springboot-exception-handler/src/test/java/kr/pe/advenoh/utils/SpringMockMvcTestSupport.java b/springboot-exception-handler/src/test/java/kr/pe/advenoh/utils/SpringMockMvcTestSupport.java new file mode 100644 index 00000000..0f7c30a0 --- /dev/null +++ b/springboot-exception-handler/src/test/java/kr/pe/advenoh/utils/SpringMockMvcTestSupport.java @@ -0,0 +1,11 @@ +package kr.pe.advenoh.utils; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.test.web.servlet.MockMvc; + +@AutoConfigureMockMvc +public abstract class SpringMockMvcTestSupport extends SpringBootTestSupport { + @Autowired + protected MockMvc mockMvc; +}