Skip to content

Commit

Permalink
Merge pull request eugenp#8696 from amit2103/BAEL-21705
Browse files Browse the repository at this point in the history
added missing code which are mentioned in article
  • Loading branch information
lor6 authored Mar 1, 2020
2 parents 52ac10a + 6e634d1 commit a2f82f1
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -25,6 +28,8 @@

import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.service.IFooService;
import com.baeldung.web.exception.CustomException1;
import com.baeldung.web.exception.CustomException2;
import com.baeldung.web.exception.MyResourceNotFoundException;
import com.baeldung.web.hateoas.event.PaginatedResultsRetrievedEvent;
import com.baeldung.web.hateoas.event.ResourceCreatedEvent;
Expand All @@ -36,6 +41,8 @@
@RequestMapping(value = "/foos")
public class FooController {

private static final Logger logger = LoggerFactory.getLogger(FooController.class);

@Autowired
private ApplicationEventPublisher eventPublisher;

Expand Down Expand Up @@ -137,4 +144,10 @@ public void update(@PathVariable("id") final Long id, @RequestBody final Foo res
public void delete(@PathVariable("id") final Long id) {
service.deleteById(id);
}

@ExceptionHandler({ CustomException1.class, CustomException2.class })
public void handleException(final Exception ex) {
final String error = "Application specific error handling";
logger.error(error, ex);
}
}
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;
}



}
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;

}
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;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.baeldung.web.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public final class MyResourceNotFoundException extends RuntimeException {

public MyResourceNotFoundException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import java.util.Collections;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
Expand All @@ -24,6 +26,7 @@
import com.baeldung.persistence.model.Foo;
import com.baeldung.persistence.service.IFooService;
import com.baeldung.web.controller.FooController;
import com.baeldung.web.exception.CustomException1;
import com.baeldung.web.hateoas.event.PaginatedResultsRetrievedEvent;

/**
Expand Down Expand Up @@ -56,5 +59,15 @@ public void givenPresentFoo_whenFindPaginatedRequest_thenPageWithFooRetrieved()
.andExpect(status().isOk())
.andExpect(jsonPath("$",Matchers.hasSize(1)));
}


@Test
public void delete_forException_fromService() throws Exception {
Mockito.when(service.findAll()).thenThrow(new CustomException1());
this.mockMvc.perform(get("/foos")).andDo(h -> {
final Exception expectedException = h.getResolvedException();
Assert.assertTrue(expectedException instanceof CustomException1);

});
}

}

0 comments on commit a2f82f1

Please sign in to comment.