Get HttpServletRequest attribute value using Spring annotation [duplicate] - java

This question already has answers here:
Spring MVC: bind request attribute to controller method parameter
(7 answers)
Closed 7 years ago.
I have a HandlerInterceptorAdaptor.preHandle() method that simplified looks like this:
#Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
request.setAttribute("MyObject", myObject);
return true;
}
Next when my #RestController gets called, I would like it to look like this:
#RequestMapping(value="/", method=RequestMethod.PUT)
public ResponseEntity myMethod (MyObject myObject) {
}
I imagine there is some annotation I can put there where Spring will add the attribute I set earlier in the HandlerInterceptorAdaptor.
Could someone please tell me what that is?

Why not like this?
#RequestMapping(value="/", method=RequestMethod.PUT)
public ResponseEntity myMethod (HttpServletRequest request, HttpServletResponse response) {
MyClass obj = (MyClass) request.getAttribute("myObject");
}

Related

modify returned object of an endpoint method

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
}

modify Response Object returbed by an endpoint

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
}

HttpServletRequest.getParameter() of spring boot HandlerInterceptor returns null [duplicate]

This question already has answers here:
String string = request.getParameter("data") is null
(2 answers)
Closed 1 year ago.
I am overriding preHandle() method of Spring boot HandlerInterceptor.
When I am invoking getParameter() method on HttpServletRequest request it is returning null.
#Component
#Slf4J
public class CustomInterceptor implements HandlerInterceptor {
#Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
log.info("age::{}",request.getParameter("age"));
return HandlerInterceptor.super.preHandle(request, response, handler);
}
}
controller class...
#RestController
#Slf4J
public class Controller {
#PostMapping("/test")
public void test(#RequestBody Person person){
log.info("in test method");
}
}
dto...
#Data
public class Person {
private Integer age;
}
json body....
{"age":"34"}
output:
age:: null
Body is present in HttpServletRequest. i have verified it with bufferReader and getContentLength() but not able to access it via getParameter().
I have gone through below stack overflow links
Logging Payload of POSTs to Tomcat
HttpServletRequest.getParameter() returns null
http://natch3z.blogspot.com/2009/01/read-request-body-in-filter.html
These all helps to extract the data from request body but it requires a lot of custom code to be written. Is there any solution provided by spring using which directly I can access the param values.
Why getParameter() is returning null and how can we access the data directly?
I have solved this problem in other way.
I have used IOUtils of apache, org.apache.commons.io.IOUtils
String data = IOUtils.toString(request.getReader());
Person person = new ObjectMapper().readValue(data, Person.class);
this solves my problem but still I feel it to be workaround as getParameter() should return appropriate value for key I think so.

Use the annotations inside the method

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 ?

Spring MVC referencing params variable from RequestMapping

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.

Categories

Resources