Added example for spring boot exception handling
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.errorhandling.boot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication
|
||||
@PropertySource("errorhandling-application.properties")
|
||||
public class ErrorHandlingBootApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ErrorHandlingBootApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.errorhandling.boot.configurations;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
|
||||
@Component
|
||||
public class MyCustomErrorAttributes extends DefaultErrorAttributes {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
|
||||
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, includeStackTrace);
|
||||
errorAttributes.put("locale", webRequest.getLocale()
|
||||
.toString());
|
||||
errorAttributes.remove("error");
|
||||
errorAttributes.put("cause", errorAttributes.get("message"));
|
||||
errorAttributes.remove("message");
|
||||
errorAttributes.put("status", String.valueOf(errorAttributes.get("status")));
|
||||
return errorAttributes;
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.errorhandling.boot.configurations;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ErrorProperties;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
|
||||
import org.springframework.boot.web.servlet.error.ErrorAttributes;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Component
|
||||
public class MyErrorController extends BasicErrorController {
|
||||
|
||||
public MyErrorController(ErrorAttributes errorAttributes) {
|
||||
super(errorAttributes, new ErrorProperties());
|
||||
}
|
||||
|
||||
@RequestMapping(produces = MediaType.APPLICATION_XML_VALUE)
|
||||
public ResponseEntity<Map<String, Object>> xmlError(HttpServletRequest request) {
|
||||
Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.APPLICATION_XML));
|
||||
body.put("xmlkey", "the XML response is different!");
|
||||
HttpStatus status = getStatus(request);
|
||||
return new ResponseEntity<>(body, status);
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.errorhandling.boot.web;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class FaultyRestController {
|
||||
|
||||
@GetMapping("/exception")
|
||||
public ResponseEntity<Void> requestWithException() {
|
||||
throw new NullPointerException("Error in the faulty controller!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung;
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
#server.error.whitelabel.enabled=false
|
||||
#server.error.include-stacktrace=always
|
||||
Reference in New Issue
Block a user