I have the next method:
#RequestMapping(value="/busqueda/basica", method = {RequestMethod.POST,RequestMethod.GET})
public String busquedaBasica(HttpServletRequest request,
HttpServletResponse response,
ModelMap modelMap,
#RequestParam("nombreBasica") String nombre){
...
}
Is there any way to get the method's request, POST or GET?
Yes, the HttpServletRequest has a getMethod() that returns a String value representing the HTTP method.
Related
I have an endpoint method that returns a List of Items, I wonder how can I intercept this list to apply some modifications using HandlerInterceptor, is it possible to get the returned list from the HttpServletResponse here?
for example:
#PostMapping(value="/someUrl")
public List<Items> find(#RequestBody Command cmd){
//do something
return arrayListOfItems;
}
In MyHandlerInterceptor class:
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws IOException {
//how can I get the returned List<Items> and modify it here, before returning it to the client
}
I have an endpoint method that returns a List of Items, I wonder how can I intercept this list to apply some modifications for example:
#PostMapping(value="/someUrl")
public List<Items> find(#RequestBody Command cmd){
//do something
return arrayListOfItems;
}
I tried to do using HandlerInterceptor, is it possible to get the returned list from the HttpServletResponse here?, if not what is the best solution ?
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws IOException {
//how can I get the returned List<Items> and modify it here, before returning it to the client
}
I implement a controller as the following:
#RequestMapping(value = "/export", method = RequestMethod.POST)
public #ResponseBody ResponseEntity<Object> Export(HttpServletRequest req, HttpServletResponse response, String type,String text) {
........
}
When posting the text(form param) which the length is small(about 20k) from client, the controller works ok and can get form params(data and type).
But 'type' and 'text' are null in service side, when text(form param) is very long(more than 200k) from client.
Who know how to handle it.
Form params can be read from request as req.getParameter("type").
change your method as below since you are already using req and resp in the method signature
public #ResponseBody ResponseEntity<Object> Export(HttpServletRequest req, HttpServletResponse response){
String type = req.getParameter("type");
String text = req.getParameter("text");
}
You could use the Spring MVC annotation as follows.
public #ResponseBody void export(#PathVariable final String whatEver,
#RequestParam("type") final String type, #RequestParam("text") final String text,
final HttpServletRequest request)
Is there a way to use the value of the annotation inside the same method that it has been declared ?
#GET
#Produces({MediaType.APPLICATION_XML})
#Path(CONSTANTS.PATH1)
public MyModel getInfo(
#PathParam(CONSTANTS.ID) String id,
#Context HttpServletRequest request,
#Context HttpServletResponse response) {
...
}
In the above example, is it possible to use the value of #Path(CONSTANTS.PATH1) inside the method? I can directly use the value of CONSTANTS.PATH1, but if it possible to get it from annotations itself ?
I have the method below:
#RequestMapping(value = "/path/to/{iconId}", params="size={iconSize}", method = RequestMethod.GET)
public void webletIconData(#PathVariable String iconId, #PathVariable String iconSize, HttpServletResponse response) throws IOException {
// Implementation here
}
I know how to pass the variable "webletId" from the RequestMapping using the #PathVariable, but how do I reference the variable "iconSize" from params?
Thanks a lot.
Use #RequestParam:
#RequestMapping(value = "/path/to/{iconId}", method = RequestMethod.GET)
public void webletIconData(#PathVariable String iconId,
#RequestParam("size") String iconSize,
HttpServletResponse response) throws IOException { ... }
See also:
15.3.2.3 Supported handler method arguments and return types
axtavt is right
I only want to explain what your mistake is:
The #RequestMapping params parameter is a filter to make sure that the annotated handler method is only invoked if there is a parameter with the requested value.
So a handler method annotated with #RequestMapping(params="action=doSomething") will be only invoked if there is an request parameter actionwith the content doSomething.