#ExceptionHandler with parameters not working - spring-mvc

I am trying to capture all exceptions of some class in my Controller class. It works fine when
I define it like this:
#ExceptionHandler(NoSearchResultException.class)
public String handleNoSearchResultException() {
return "someView";
}
But not if I add any parameters:
#ExceptionHandler(NoSearchResultException.class)
public String handleNoSearchResultException(Exception e) {
return "someView";
}
What could possibly be happening? Also, I've read #ExceptionHandler does not support Model arguments, so how would I pass a parameter (like the error message for instance) to the view in order to offer a dynamic error page?

To pass a parameter to the view I would create a custom Exception class in which you can store any required model parameters (such as error messages). Then in #ExceptionHandler method you can extract those model parameters and make them available in the view. For example:
class RequestException extends RuntimeException {
...
public void setErrorMessages(List<String> errorMsgs) {
this.errorMessages = errorMsgs
}
...
}
#ExceptionHandler(RequestException.class)
public ModelAndView handleNoSearchResultException(RequestException ex) {
ModelAndView mav = new ModelAndView("someView");
mav.addObject("errors", ex.getErrorMessages()); //fetch error messages
return mav;
}
As for parameters, try specifying NoSearchResultException as method parameter instead of it's Exception superclass.
EDIT:
Had a bug in 2nd example return value.

I Solved the problem by passing the custom arguments in request itself.
code is as below :
Controller
#RequestMapping(method = RequestMethod.GET, value = "/exception2")
public String getException1(ModelMap model, #CRequestParam("p") String p, HttpServletRequest request) {
System.out.println("Exception 2 " + p);
request.setAttribute("p", p);
throw new CustomGenericException("1", "2");
}
Exception Handler
#ExceptionHandler(CustomGenericException.class)
public ModelAndView handleCustomException(CustomGenericException ex, HttpServletRequest request) {
ModelAndView model2 = new ModelAndView("error/generic_error");
model2.addObject("exception", ex);
System.out.println(request.getAttribute("p"));
System.out.println("CustomGenericException ");
return model2;
}
here is Sackoverflow question and its answer and
Complete source code is available at git

Related

How to pass a generic collection Class object as an argument

I've RESTful service Spring MVC based.
The service has a RESTful resource method that returns the following response:
public class OperationalDataResponse<T> {
private String status;
private String statusMessage;
private T result;
//getters and setters
}
This response object encapsulates the result object of type T.
On the client side I use RestTemplate with GsonHttpMessageConverter added.
I get the response from service as a ResponseEntity
I handle the generic response with runtime Type as below:
public class OperationalDataRestClient<REQ,RESULT_TYPE> {
public OperationalDataResponse<RESULT_TYPE> getOperationalData(String resourcePath, Map<String, Object> urlVariables, Class<RESULT_TYPE> resultType) {
//code to invoke service and get data goes here
String responseString = responseEntity.getBody();
response = GsonHelper.getInstance().fromJson(responseString, getType(OperationalDataResponse.class, resultType));
}
Type getType(final Class<?> rawClass, final Class<?> parameter) {
return new ParameterizedType() {
#Override
public Type[] getActualTypeArguments() {
return new Type[] { parameter };
}
#Override
public Type getRawType() {
return rawClass;
}
#Override
public Type getOwnerType() {
return null;
}
};
}
}
This works like a charm as long as my resultType is a non-collection class.
So, this works great from caller code:
getOperationalData(someResourcePath, someUrlVariables, MyNonGenericClass.class)
However if my resultType is a collection (say, List<String> or List<MyNonGenericClass>)
then I don't know how to pass the resultType Class from the caller code.
For example, from caller code,
getOperationalData(someResourcePath, someUrlVariables, List.class)
or
getOperationalData(someResourcePath, someUrlVariables, List<MyNonGenericClass>.class)
throws compilation error.
I tried passing on ArrayList.class as well but that too doesn't work.
Any suggestion how can I pass a generic collection as a resultType from caller code (in other words, as an example, how can I pass the class object of a List<String> or List<MyNonGenericClass> from caller code ?)
If you know that ResultType is coming as a List, Then it will obvious fail like you said compilation issue.Why? because you are trying to send a List when you method only accepts a single value.In order to over come that issue you will have to change the method arguments to the following
public OperationalDataResponse<RESULT_TYPE> getOperationalData(String resourcePath, Map<String, Object> urlVariables, List<Class<RESULT_TYPE>> resultType){
....
}
and you will have to make some slight modification to getType() Method,loop it and then pass each class value to getType method like so
for(MyNonGenericClass myClass:mylist){
getType(OperationalDataResponse.class, myClass.getClass());
}

Spring Boot (MVC) keeping object information to pass it to further URLs

I'm trying to do my application that is dedicated for managing patients data base by doctors and have some problems with keeping information about object that is once send by post request method. I want them to be remembered in the URL. I tried to do something with #SessionAttributes but I don't think i do understand it well.
Here's my controller:
#Controller
#SessionAttributes("loggedInPersonelID")
#RequestMapping
public class PatientManagerController {
#Autowired
private PatientService patientService;
#Autowired
private PersonelService personelService;
private Personel getLoggedInPersonel(String personelID) {
return personelService.getPersonel(personelID);
}
#GetMapping
public ModelAndView getLoginView() {
ModelAndView mav = new ModelAndView("login-view");
mav.addObject("personel", new Personel());
return mav;
}
Method post passes logged in user to next URL /user={id} (used RedirectAttributes)
#PostMapping
public String loginUser(#ModelAttribute("personel") Personel personel,
RedirectAttributes redirectAttrs,
Model model) {
Personel loggedInPersonel = getLoggedInPersonel(personel.getPersonelID());
model.addAttribute("loggedInPersonelID", loggedInPersonel.getPersonelID());
if (loggedInPersonel != null) {
if (loggedInPersonel.getPassword().equals(personel.getPassword())) {
redirectAttrs.addAttribute("id", loggedInPersonel.getPersonelID());
return "redirect:/user={id}";
} else {
model.addAttribute("errorMessage", "Invalid credentials!");
return "login-view";
}
} else {
model.addAttribute("errorMessage", "User with given ID does not exist");
return "login-view";
}
}
Here's my get method that catches the view for logged in user. URL works here since the model was passed in previous post method. I've got something like /user=john-smith-123
#GetMapping("/user={id}")
public ModelAndView getUserMainView(#PathVariable("id") String personelID) {
ModelAndView mav = new ModelAndView("personel-main-view");
Personel loggedInPersonel = getLoggedInPersonel(personelID);
mav.addObject("personelOccupation", loggedInPersonel.getOccupation());
mav.addObject("personelName", loggedInPersonel.getName());
mav.addObject("personelSurname", loggedInPersonel.getSurname());
return mav;
}
However the next page doesn't remember the user's id anymore. I thought that passing it to the model's attribute with the same name as determined in #SessionAttributes("loggedInPersonelID") the information will be remembered.
#GetMapping("/user={id}/patients")
public ModelAndView getPatientsView(#PathVariable("id") String personelID) {
ModelAndView mav = new ModelAndView("patients-view");
Personel loggedInPersonel = getLoggedInPersonel(personelID);
mav.addObject("loggedInPersonelID", loggedInPersonel.getPersonelID());
mav.addObject("list", patientService.getPersonelsList(loggedInPersonel));
return mav;
}
The outcome in the URL: user=$%7BloggedInPersonelID%7D/patients and error There was an unexpected error (type=Internal Server Error, status=500).
No message available
Here's the link in a personel-main-view view that should move me to desired page
<a th:href="#{/user=${loggedInPersonelID}/patients}">My patients</a>
So how do I do that? Sorry for the messy code. Is this matter more complicated than it looks like? Is there something deeper I am missing?
PS. I am working with thymeleaf
try this:
<a th:href="#{/user=__${loggedInPersonelID}__/patients}">My patients</a>
this works as shown here

Spring Model - "Model object must not be null"

A method in one of my Spring controller class,
#RequestMapping(value = "/products/{productId}/specifications", method = RequestMethod.GET)
public String setup(#PathVariable("productId") Integer pid, Model m) {
//...
m.addAttribute(foo); <-- error
return "my-page";
}
After I got an error message "Model object must not be null", I change the method signature as shown in the following:
public ModelAndView setup(#PathVariable("productId") Integer pid) {
//...
ModelAndView mv = new ModelAndView("my-page");
mv.addObject(foo); <-- error
return mv;
}
I was able to run the modified code once. But I got the same error on ModelAndView. I have used Spring MVC for many years. That is my first time having this problem. What is the cause?
I use Spring 4.0.6.RELEASE.
Although you have not provided the code that shows what the foo reference points to, it is safe to assume it is a null reference.
I took a look at the Project code on Github, and it is clear what happening here.
The ModelAndView#addObject(Object) method delegates to the ModelMap#addAttribute(Object) method, which asserts that the provided Object is not null, using the exact message your question is asking about.
ModelAndView method:
public ModelAndView addObject(Object attributeValue) {
getModelMap().addAttribute(attributeValue);
return this;
}
ModelMap method:
public ModelMap addAttribute(Object attributeValue) {
Assert.notNull(attributeValue, "Model object must not be null");
if (attributeValue instanceof Collection && ((Collection<?>) attributeValue).isEmpty()) {
return this;
}
return addAttribute(Conventions.getVariableName(attributeValue), attributeValue);
}

Recommended way to display an error message without resorting to #ModelAttribute with Spring MVC

I have the following method skeleton in a Spring MVC application:
#RequestMapping(value = "/activateMember/{token}", method = RequestMethod.GET, produces = "text/html")
public String activateMember(#PathVariable("token") String token) {
...
}
I am trying to display an error message if the token is invalid for some reason. However I have no ModelAttribute in the method arguments and I don't really want one. But of course I can't use an Errors or BindingResults argument because of the absence of a ModelAttribute and its corresponding form.
So my question is:
what is the recommended way to display an error message given the above method signature and without introducing a ModelAttribute?
If the String you've returned from the method is a viewname (Spring default) then simply create a view for this case and do like:
#RequestMapping()
public String activateMember(#PathVariable("token") String token) {
if(checkToken(token)){
doProcess();
return "userprofile";
} else {
return "badtoken"
}
}
In more complicated case you may have a hierarchy of exceptions, related to bad tokens. (Token is expired, token is just incorrect and so on). You can register an #ExceptionHandler in the same controller:
#RequestMapping()
public String activateMember(#PathVariable("token") String token) {
return activate(token); // This method may throw TokenException and subclasses.
}
#ExceptionHandler(TokenException.class)
public ModelAndView tokenException(TokenException e){
// some code
return new ModelAndView("badtoken", "exception", e);
}

Spring MVC exception handling with HandlerExceptionResolver

I am currently trying to use HandlerExceptionResolver for exception handling in a Spring MVC project.
I want to handle normal exceptions via resolveException as well as 404's via
handleNoSuchRequestHandlingMethod.
Depending on the request type JSON or text/html the exception response should be returned appropriately.
resolveException works now.
But handleNoSuchRequestHandlingMethod is giving me a headache. It's never called!
According to the docu the method should be called on 404 errors
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html
What am I doing wrong...
This is what I have so far.
public class JsonExceptionResolver implements HandlerExceptionResolver {
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView resolveException(HttpServletRequest request,
if (exception instanceof NoSuchRequestHandlingMethodException) {
return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) exception, request, response, handler);
}
...
}
public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex,
HttpServletRequest request,
HttpServletResponse response,
Object handler){
logger.info("Handle my exception!!!");
ModelAndView mav = new ModelAndView();
boolean isJSON = request.getHeader("Accept").equals("application/json");
if(isJSON){
...
}else{
..
}
return mav;
}
}
EDIT with DefaultHandlerExceptionResolver:
public class MyExceptionResolver extends DefaultHandlerExceptionResolver {
protected final Log logger = LogFactory.getLog(getClass());
#Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {
logger.warn("An Exception has occured in the application", exception);
logger.info("exception thrown " + exception.getMessage() );
if (exception instanceof NoSuchRequestHandlingMethodException) {
return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) exception, request, response, handler);
}
...
return mav;
}
public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex,
HttpServletRequest request,
HttpServletResponse response,
Object handler){
logger.info("Handle my exception!!!");
ModelAndView mav = new ModelAndView();
boolean isJSON = request.getHeader("Accept").equals("application/json");
if(isJSON){
...
}else{
...
}
return mav;
}
}
The above code still has no effect.
Any other ideas?
According to Juergen Hoeller from Spring, it isn't possible with the HandlerExceptionResolver because it only works for sub-mapping e.g.
you have a controller mapped to /account/** and accesss a method from acount where no mapping exists like /acount/notExists than it should work.
I will open a JIRA improvement ticket for this functionality
EDIT:
JIRA ticket about this issue
https://jira.springsource.org/browse/SPR-8837?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=72648#comment-72648
handleNoSuchRequestHandlingMethod isn't part of the HandlerExceptionResolver interface, so just declaring a method of that name will do nothing. It's a protected method specific to DefaultHandlerExceptionResolver, and is called from its resolveException method (which is part of the interface):
if (ex instanceof NoSuchRequestHandlingMethodException) {
return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, request, response, handler);
}
To reproduce the same functionality, you can either subclass DefaultHandlerExceptionResolver and override the methods you need to, or you need to add a case in your resolveException method that handles NoSuchRequestHandlingMethodException.

Resources