forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#8696 from amit2103/BAEL-21705
added missing code which are mentioned in article
- Loading branch information
Showing
6 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...g-boot-rest/src/main/java/com/baeldung/web/error/RestResponseStatusExceptionResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.baeldung.web.error; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.ModelAndView; | ||
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.xml.XmlMapper; | ||
|
||
@Component | ||
public class RestResponseStatusExceptionResolver extends AbstractHandlerExceptionResolver { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(RestResponseStatusExceptionResolver.class); | ||
|
||
@Override | ||
protected ModelAndView doResolveException(HttpServletRequest request, | ||
HttpServletResponse response, Object handler, Exception ex) { | ||
try { | ||
if (ex instanceof IllegalArgumentException) { | ||
return handleIllegalArgument( | ||
(IllegalArgumentException) ex, request, response, handler); | ||
} | ||
} catch (Exception handlerException) { | ||
logger.warn("Handling of [{}] resulted in Exception", ex.getClass().getName(), handlerException); | ||
} | ||
return null; | ||
} | ||
|
||
private ModelAndView handleIllegalArgument(IllegalArgumentException ex, | ||
final HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||
final String accept = request.getHeader(HttpHeaders.ACCEPT); | ||
|
||
response.sendError(HttpServletResponse.SC_CONFLICT); | ||
response.setHeader("ContentType", accept); | ||
|
||
final ModelAndView modelAndView = new ModelAndView("error"); | ||
modelAndView.addObject("error", prepareErrorResponse(accept)); | ||
return modelAndView; | ||
} | ||
|
||
/** Prepares error object based on the provided accept type. | ||
* @param accept The Accept header present in the request. | ||
* @return The response to return | ||
* @throws JsonProcessingException | ||
*/ | ||
private String prepareErrorResponse(String accept) throws JsonProcessingException { | ||
final Map<String, String> error = new HashMap<>(); | ||
error.put("Error", "Application specific error message"); | ||
|
||
final String response; | ||
if(MediaType.APPLICATION_JSON_VALUE.equals(accept)) { | ||
response = new ObjectMapper().writeValueAsString(error); | ||
} else { | ||
response = new XmlMapper().writeValueAsString(error); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
|
||
|
||
} |
7 changes: 7 additions & 0 deletions
7
spring-boot-rest/src/main/java/com/baeldung/web/exception/CustomException1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.baeldung.web.exception; | ||
|
||
public class CustomException1 extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
spring-boot-rest/src/main/java/com/baeldung/web/exception/CustomException2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.baeldung.web.exception; | ||
|
||
public class CustomException2 extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
spring-boot-rest/src/main/java/com/baeldung/web/exception/MyResourceNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters