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
}
Related
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 have ServletRequest that is wrapped multiple times via javax.servlet.ServletRequestWrapper#ServletRequestWrapper
I have also some method where the incoming parameter is HttpServletRequest which is top level Wrapper of ServletRequest.
There is convenient method to check if this incoming ServletRequest is wrapped by some particular ServletRequestWrapper - javax.servlet.ServletRequestWrapper#isWrapperFor(java.lang.Class)
My question is if there exists some method(In some java lib) that allows getting this particular ServletRequestWrapper from incoming HttpServletRequest
The method that does similar stuff:
public <T extends HttpServletRequestWrapper> Optional<T> findWrapper(
HttpServletRequest request, Class<T> type) {
ServletRequest servletRequest = request;
HttpServletRequestWrapper wrapper;
while (servletRequest instanceof HttpServletRequestWrapper) {
wrapper = (HttpServletRequestWrapper) servletRequest;
if (type.isAssignableFrom(wrapper.getClass())) {
return Optional.of((T) wrapper);
} else {
servletRequest = wrapper.getRequest();
}
}
return Optional.empty();
}
I'm currently trying to modify, (via a Spring filter) some of the request variables being posted into a form.
Reason being, I would like to implement better phone number validation, and better control how telephone numbers are formatted. For that part of the puzzle, I intend to use Google's Lib phone number in my model so like so:
private PhoneNumber mobileNumber;
One getter, with no mention of the prefix at all, given that the filter will hopefully do the hard work for me.
I initially thought that perhaps I could use an attribute converter to do this i.e.
#Convert(converter = PhoneNumberConverter.class )
private PhoneNumber mobileNumber;
However, there is a problem with that, in that if the field is a composite type, the JPA doesn't support it: https://github.com/javaee/jpa-spec/issues/105 (compositie because PREFIX is needed as well as NUMBER) to build a lib phone object.
So. A filter (or Interceptor?) is what I'm left with. My question is, I'm new to the Spring framework and I'm not 100% sure whether just modifying the raw request will allow instantiation of the PhoneNumber object in the model - (I presume not), but any guidance on how Spring manages to do its magic tying up of request variables into an object (by mapping getters and setters) and how I would go about doing this manually in the filter would be helpful. Is there any way of access this Model object in the filter so I can set it directly?
public class PhonePrefixFilter extends OncePerRequestFilter
{
#Override
protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain )
throws ServletException, IOException
{
String prefix = request.getParameter( "phonePrefix" );
if( StringUtils.isNotEmpty( prefix ) )
request.setAttribute( "mobileNumber", prefix + request.getAttribute( "mobileNumber" ) );
filterChain.doFilter( request, response );
}
}
AFAIK you cannot modify your request parameter directly. You need a HttpServletRequestWrapper to provide custom getter to your parameter:
public static class PhoneRequestWrapper extends HttpServletRequestWrapper {
public PhoneRequestWrapper(HttpServletRequest request) {
super(request);
}
#Override
public String getParameter(String name) {
if (!("mobileNumber").equals(name)) {
return super.getParameter(name);
}
String prefix = getParameter("phonePrefix");
String mobileNumber = getRequest().getParameter("mobileNumber");
if (StringUtils.isNotEmpty(prefix) && StringUtils.isNotEmpty(mobileNumber)) {
return prefix + getRequest().getParameter("mobileNumber");
} else {
return mobileNumber;
}
}
}
Then create your filter:
public static class PhoneNumberFilter implements Filter {
#Override
public void init(FilterConfig filterConfig) throws ServletException {
}
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws
ServletException,
IOException {
filterChain.doFilter(new PhoneRequestWrapper((HttpServletRequest) request), response);
}
#Override
public void destroy() {
}
}
And register it in your #Configuration class:
#Bean
public Filter filterRegistrationBean() {
return new PhoneNumberFilter();
}
From now on, your request.getParamter("mobileNumber") will have the value appended with phonePrefix
Since your question is not very clear, if you want to override the behaviour of #RequestParam to get your phone number string, you can use custom HandlerMethodArgumentResolver to resolve your parameter
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");
}
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.