I am using SpringMockMvc and in my controller I have #ExceptionHandler. When calling post request, I am getting the below error.
Failed to invoke #ExceptionHandler method: public org.springframework.http.ResponseEntity<com.xx.xxx> com.xx.xx.handleException(java.lang.Throwable,org.eclipse.jetty.server.Request)
java.lang.IllegalStateException: Current request is not of type [org.eclipse.jetty.server.Request]: org.springframework.mock.web.MockHttpServletRequest#47fac1bd
at org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver.resolveArgument(ServletRequestMethodArgumentResolver.java:97)
I am not sure why #ExceptionHandler cannot handle if the request is of type org.springframework.mock.web.MockHttpServletRequest
That could never work, simply because org.springframework.mock.web.MockHttpServletRequest is not a sub-type of org.eclipse.jetty.server.Request. Thus, it is impossible for Spring to provide an instance of MockHttpServletRequest for a parameter of type org.eclipse.jetty.server.Request.
You'll need to change the type of the second parameter in your handleException() method to javax.servlet.http.HttpServletRequest.
Related
I send following http request:
http://localhost:8081/member/createCompany/getSmallThumbnail/
On server side I hit into controller method:
#RequestMapping("/error")
public String error(Model model, HttpServletRequest request){
if(request.getRequestURI().contains("thumbnail")){
System.out.println("thumbnail accepted");
}
request.toString();
model.addAttribute("message", "page not found");
return "errorPage";
}
At this method I want to know url with which the request arrived.
If in debug I stop inside this method I see information needed for me:
But I cannot find method in request which will return this.
Please help to return url which I want.
P.S.
Actually I have not mapped controller in my spring mvc application(url is broken) for http://localhost:8081/member/createCompany/getSmallThumbnail/. This url("/error") configured in web.xml as error page.
Your request got redispatched to /error (presumably for error processing).
If this framework follows the normal Servlet error dispatching behavior, then your original request can be found in the HttpServletRequest.getAttributes() under the various javax.servlet.RequestDispatcher.ERROR_* keys.
ERROR_EXCEPTION - The exception object
ERROR_EXCEPTION_TYPE - The type of exception object
ERROR_MESSAGE - the exception message
ERROR_REQUEST_URI - the original request uri that caused the error dispatch
ERROR_SERVLET_NAME - the name of the servlet that caused the error
ERROR_STATUS_CODE - the response status code determined for this error dispatch
What you want is
String originalUri = (String) request.getAttribute(
RequestDispatcher.ERROR_REQUEST_URI)
I have been able to get websockets working with my application using sock.js stompjs and spring 4. However I'm having jackson mapping issues when I try to send a json object and use #validated. The jackson error is:
Could not read JSON: can not deserialize instance of com.... out of START_ARRAY token
Here is the server side code:
#MessageMapping("/notify/{id}")
#SendTo("/subscription/{id}")
#ResponseBody
public SimpleDemoObject add(#Payload #Validated ReqObject req, #DestinationVariable("id") Long id, Errors errors)
And the client side:
socket.stomp.send( _contextPath + "/notify/" + id, {"content-type": "application/json"}, data);
I was previously trying to use #RequestBody but I believe #Payload is the correct way here?
I can get it to work if I remove #Payload and #Validated but I would like to use spring validation on my request object. Any tips on what I'm doing wrong?
I wrote a spring-mvc controller method to get an array of values in the request parameter.The method looks like below
/**
Trying to get the value for request param foo which passes multiple values
**/
#RequestMapping(method=RequestMethod.GET)
public void performActionXX(HttpServletRequest request,
HttpServletResponse response,
#RequestParam("foo") String[] foo) {
......
......
}
The above method works fine when the request url is in below format
...?foo=1234&foo=0987&foo=5674.
However when the request url is in below format the server returns 400 error
...?foo[0]=1234&foo[1]=0987&foo[2]=5674
Any idea how to fix the method to cater to the second format request url?
This is not possible with #RequestParam. What you can do is implement and register your own HandlerMethodArgumentResolver to perform to resolve request parameters like
...?foo[0]=1234&foo[1]=0987&foo[2]=5674
into an array. You can always checkout the code of RequestParamMethodArgumentResolver to see how Spring does it.
Note that I recommend you change how the client creates the URL.
The server is supposed to define an API and the client is meant to follow it, that's why we have the 400 Bad Request status code.
I resolved this issue using the request.getParameterMap().Below is code.
Map<String,String> parameterMap= request.getParameterMap();
for(String key :parameterMap.keySet()){
if(key.startsWith("nameEntry")){
nameEntryLst.add(request.getParameter(key));
}
}
I am trying to send a post request with cxf implementation. But I am getting InjectionUtils - Parameter Class SizeQuantityBean has no constructor with single String parameter, static valueOf(String) or fromString(String) methods error while binding a class.
My code to send request:
sellerService.register(sellerBean);
Implementation:
#Path(BASE_PATH + "/register")
#POST
#Produces({MediaType.APPLICATION_JSON})
BaseBean register(#QueryParam("") SellerBean sellerBean){
...
...
}
SellerBean class has List of SizeQuantityBean. How to reslove this conflict?
Have you tried removing #QueryParam annotation because your request is POST, you should use no tag body.
I am working on a spring project which has several classes. Now the code in that project is not up to the standard because of which there are no try catch blocks defined in any of the classes.
I have the following mapping in my web.xml
<error-page>
<exception-code>404</exception-type>
<location>/error.do</location>
</error-page>
the /error.do is mapped to a controller from where its forward to a generic error view . Now ,will it be possible to get the stacktrace of my exception. My only requirement is to get the exception details in that error handler like -- where exception originated like class name, method name and exception message which i felt will be available in stack trace.
Any idea how to proceed with this problem statement?
Yes you can do it Spring provides #ExceptionHandler annotation.So in your controller write one method as :
#ExceptionHandler(YourException.class)
public ModelAndView handleYourException(YourException ex, HttpServletRequest request) {
mav = new ModelAndView();
mav.addObject("exception", exception);
mav.setViewName("YourException Page");
return mav;
}
Hope you got my point.