Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

spring filter exception handling

public class ExceptionHandlerFilter extends OncePerRequestFilter {

    @Override
    public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            filterChain.doFilter(request, response);
        } catch (RuntimeException e) {

            // custom error response class used across my project
            ErrorResponse errorResponse = new ErrorResponse(e);

            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            response.getWriter().write(convertObjectToJson(errorResponse));
    }
}

    public String convertObjectToJson(Object object) throws JsonProcessingException {
        if (object == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(object);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: variables java 
Java :: is palindrome java 
Java :: java repository sql find not in list 
Java :: java hello word 
Java :: display in java 
Java :: upper en java 
Java :: java constructor overloading 
Java :: java sample code 
Java :: add two numbers bitwise 
Java :: java standard exception 
Java :: java append a string to file 
Java :: convert char array to set in java 
Java :: java throw an exception 
Java :: java instanceof keyword 
Java :: display two dimension array java 
Java :: array contain java 
Java :: greater than sign in java 
Java :: java remove map 
Java :: declaration of an array in java 
Java :: how to add the last area of an array in java 
Java :: how to turna date into a LocalDateTime java 
Java :: writing values in 2d array java 
Java :: bufferedinputstream 
Java :: stringbuilder insert beginning 
Java :: java vs python 
Java :: how to initialize a string in java 
Java :: thread java timer 
Java :: linkedlist java 
Java :: local date to date java 
Java :: program to check if the given date is in in the format dd/mm/yyyy java 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =