move code to boot data module
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.jsonexception;
|
||||
|
||||
public class CustomException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CustomException() {
|
||||
super("Custom exception message.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.jsonexception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ControllerAdvice
|
||||
@ResponseBody
|
||||
public class ErrorHandler {
|
||||
|
||||
@ExceptionHandler(CustomException.class)
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public CustomException handleCustomException(CustomException ce) {
|
||||
return ce;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.jsonexception;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class MainController {
|
||||
|
||||
@GetMapping("/")
|
||||
public void index() throws CustomException {
|
||||
throw new CustomException();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.jsonexception;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jsonexception.CustomException;
|
||||
import com.baeldung.jsonexception.MainController;
|
||||
|
||||
public class MainControllerIntegrationTest {
|
||||
|
||||
@Test(expected = CustomException.class)
|
||||
public void givenIndex_thenCustomException() throws CustomException {
|
||||
|
||||
MainController mainController = new MainController();
|
||||
|
||||
mainController.index();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user