Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HttpServletRequest attribute value using Spring annotation [duplicate]

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?

like image 374
DavidR Avatar asked Nov 30 '25 06:11

DavidR


1 Answers

Why not like this?

@RequestMapping(value="/", method=RequestMethod.PUT)
public ResponseEntity myMethod (HttpServletRequest request, HttpServletResponse response) {
     MyClass obj = (MyClass) request.getAttribute("myObject");
}
like image 143
m.aibin Avatar answered Dec 02 '25 18:12

m.aibin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!