1

The original question is here: How to resolve URI encoding problem in spring-boot?. And following one of the suggestions, I am trying to come up with a solution with an Interceptor, but still has some issue.

I need to be able to handle some special characters in URL, for instance "%" and my spring controller is below:

@Controller
@EnableAutoConfiguration
public class QueryController {

private static final Logger LOGGER = LoggerFactory.getLogger(QueryController.class);

    @Autowired
    QueryService jnService;

@RequestMapping(value="/extract", method = RequestMethod.GET)
    @SuppressWarnings("unchecked")
    @ResponseBody
    public ExtractionResponse extract(@RequestParam(value = "extractionInput") String input) {

        // LOGGER.info("input: " + input);
        JSONObject inputObject = JSON.parseObject(input);


        InputInfo inputInfo = new InputInfo();

        JSONObject object = (JSONObject) inputObject.get(InputInfo.INPUT_INFO);

        String inputText = object.getString(InputInfo.INPUT_TEXT);
        inputInfo.setInputText(inputText);

        return jnService.getExtraction(inputInfo);
    }
}

Following suggestions I want to write an interceptor to encode the URL before the request is sent to the controller, and my interceptor looks like (not complete yet):

public class ParameterInterceptor extends HandlerInterceptorAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(ParameterInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse reponse,
                             Object handler) throws Exception {
        Enumeration<?> e = request.getParameterNames();
        LOGGER.info("Request URL::" + request.getRequestURL().toString());
        StringBuffer sb = new StringBuffer();
        if (e != null) {
            sb.append("?");
        }

        while (e.hasMoreElements()) {
            String curr = (String) e.nextElement();
            sb.append(curr + "=");
            sb.append(request.getParameter(curr));
        }

        LOGGER.info("Parameter: " + sb.toString());

        return true;
    }
}

I tested a URL in my browser:

http://localhost:8090/extract?extractionInput={"inputInfo":{"inputText":"5.00%"}} 

Due to the % sign, I received the error:

[log] - Character decoding failed. Parameter [extractionInput] with value [{"inputInfo":{"inputText":"5.0022:%225.00%%22}}] has been ignored. Note that the name and value quoted here may be corrupted due to the failed decoding.

When I test the interceptor, "request.getRequestURL()" gives the expected result:

http://localhost:8090/extract

However, "request.getParameterNames()" always get an empty Elumentation object. Why doesn't it get the parameters? What I hope is to first encode the parameter value:

"inputText":"5.00%"

'inputText' is a field of the object InputInfo in the json format. So how to get the request parameters to solve the problem?

user697911
  • 10,043
  • 25
  • 95
  • 169
  • which is the url for testing parameter actually? http://localhost:8090/extract? – Huy Nguyen Oct 11 '18 at 03:38
  • @huy. It is "http://localhost:8090/extract?extractionInput={"inputInfo":{"inputText":"5.00%"}}". – user697911 Oct 11 '18 at 17:02
  • I think you can decode and then encode to resolve % issue - e.g. return URLEncoder.encode(URLDecoder.decode(value, StandardCharsets.UTF_8.toString()), StandardCharsets.UTF_8.toString()); – Sanjay Amin Aug 02 '22 at 18:57

0 Answers0