We use DeferredResult in Controller methods. How is it possible to perform a FORWARD to another controller method?
I am aware of how to perform a REDIRECT, but I do not want the round trip to the browser and back.
I am also aware of how to perform a FORWARD by returning a ModelAndView or by returning a String from the controller method.
But I am restricted to use DeferredResult.
How is it possible to perform a FORWARD with DeferredResult similar to returning a string like ยด "forward:/forwardUrl"?
Related
I am collecting custom metrics for my controller endpoints via HandlerInterceptor. In postHandle I have everything I need and I would like to save the metrics for my endpoints along with the original route defined in the controller, so not the actual route filled with the data.
E.g. I have #GetMapping("/cats/{name}") and make GET request on /cats/tom I still want the defined route "cats/{name}"
I found that I can do this with the Object handler which is the parameter of postHandle -> I can cast it to HandlerMethod get the annotations of the method and find it from memberValues. This seems a bit overkill I also have to filter for Get-, Post-, Put-, Delete-, Patch-, RequestMapping annotations. Is there any easier or more straightforward way to do this? If so how? If not what else do I have to take into consideration with this solution?
It turns out Spring adds this data to the HttpServletRequest attributes. So it can be retrieved using:
String routePattern = (String) request.getAttribute("org.springframework.web.servlet.HandlerMapping.bestMatchingPattern");
All, I want to call a method or class before every request hits to my spring controller, i can not use #postconstruct since i need HttpServletRequest Object. i can not use Interceptors too, since if my variable is null or false i will redirect to home page which will call again interceptor and redirects again, this will be in a infinite loop. can anybody help me on this.
Thanks,
Generally, when user's form is submitted, request is passed to spring controller.
and Controllers are shaped like this
TestController(HttpServletRequest request, HttpServletResponse response)
I want to pass "MyHttpSevletRequest, MyHttpServletResponse" not "HttpSevletRequest, HttpServletResponse".
Is it possible?
I want to know is it possible, and how? in technique.
Don't say that "No need to to that, alternative way is here"
Any answer will be appreiciated. Thank you.
I dont know how to do it directly but I know a workaround to get what you intend to do done.
You can use spring aop methodbeforeadvice and afterreturningadvice to get hold of the request and response objects before and after they enter/leave the action method. Basically kind of a request response interceptor you would be doing. In that you can write a transformer method that would take the standard request and response object as input and output you with your custom request and response object(your custom class should implement the HttpServletRequest interface) and then override the request and reponse objects with your custom objects.
I have a simple Spring program, the backend is Spring MVC with Restful web service, the front end is pure HTML + ajax.
My problem is, when I try to use the following to map a HTTP request params to a pojo, it always fails:
#RequestMapping(value = "/books", method = RequestMethod.PUT)
public #ResponseBody
String updateBook(BookInfo book)
Here I use PUT method, because it's a modification operation. There's no exception, but I get nothing injected into book object.
With same HTTP request parameters, if I change the method to POST, and client send it via a POST, it would be success:
#RequestMapping(value = "/books", method = RequestMethod.POST)
public ResponseEntity<String> addBook(BookInfo book)
This time book will always get the filled.
Why there's difference between PUT and POST? or it's the issue of return type? (one is ResponseBody, the other is ResponseEntity)? Or, if you use PUT, then the pojo must be in persistent context?
How should I investigate the issue?
I think its not the problem with your configuration or code.
In Spring Framework there is a filter provided named HiddenHttpMethodFilter that serves all the method but initially it will do the POST request but with a hidden _method form field. And this filter reads this hidden field value and then changes the method value accordingly. Please refere this link to know more about this. I think configuring with this filter will resolve your problem.
Hope this helps you. Cheers.
I am using Urinfo for accessing query parameters in my restful web service as follows
#GET
#Produces("text/plain")
#Path("/association")
public Response Association(
#Context UriInfo uriInfo){
String clinicianId = uriInfo.getQueryParameters().getFirst("clinicianId");
List<String> providerList = uriInfo.getQueryParameters().get("clinicialProviderId");
How to access Parameters for PUT metod using uriinfo.
The same way.
Perhaps I don't understand the question. Query parameters are accessed the same way regardless of the HTTP method. Are you talking about different parameters for PUT? Do you mean Form fields? If you are sending form data in your PUT request and you want to access that, you can use #FormParam to inject them into your method parameters or fields. Btw, there is also #QueryParam you can use to inject query parameters instead of using UriInfo.