4

How can I modify JSON object sent in request body from UI side before reaching to spring controller?

I tried adding filter , but as soon as I read from request(servletrequest.getReader()) and then forward request to next filter in filter chain it fails(Request doesnt reach to controller).

BufferedReader reader = servletrequest.getReader();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
String data = buffer.toString();

Using this I got JSON in string but I am not able to figure out how to put it back in request.

user3640507
  • 65
  • 1
  • 7

1 Answers1

1

using HandlerInterceptor by Spring.You can also specifically choose to intercept specific requests. check this tutorial .This is all dependent on the version of Spring you are using.But this should give you a fair outlook into how to go about it.

package com.test.intercept;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;



public class HttpRequestInterceptor extends HandlerInterceptorAdapter {


@Autowired
private ServletContext context;

@Override
public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {
    HandlerMethod handlerMethod = (HandlerMethod) handler;
    String methodName = handlerMethod.getMethod().getName();
    Map<String, String> headerMap = HttpUtil.getAllHeaders(request);
    Boolean isAllowed=false;

    //doing what you want to your json
    //
    return isAllowed;
}

@Override
public void afterCompletion(HttpServletRequest arg0,
        HttpServletResponse arg1, Object arg2, Exception arg3)
        throws Exception {
    logger.info("in after completion");

}

@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
        Object arg2, ModelAndView arg3) throws Exception {
    logger.info("in posthandle");

}

and in your servletContext.xml add :

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/api/**" /> <!--to exclude a path -->
        <mvc:exclude-mapping path="/web/**" /> <!--to exclude a path -->
        <beans:bean class="com.test.interceptor.HttpRequestInterceptor"></beans:bean>
    </mvc:interceptor>
</mvc:interceptors>
Droidekas
  • 3,464
  • 2
  • 26
  • 40
  • I am able to get request parameter before reaching to controller in one of my filter(although this is better approach) but I want to know how can I get JSON from request and modify it. – user3640507 Jan 08 '15 at 08:29
  • see [this answer](http://stackoverflow.com/a/10458119/3396197) where it states that request body can be read only once.So I dont think modifying the JSON would be useful for you since you cannot access it in the next step. – Droidekas Jan 08 '15 at 09:00
  • If it absolutely necessary to pass data the way you want,you can modify headers ininterceptors.Maybe you could use that instead – Droidekas Jan 08 '15 at 09:02
  • what is HttpUtil.getAllHeaders(request);? – user666 Feb 22 '22 at 10:12