Оригинальный вопрос здесь: Как решить проблему кодировки URI в spring-boot? .И, следуя одному из предложений, я пытаюсь найти решение с помощью перехватчика, но у меня все еще есть проблема.
Мне нужно иметь возможность обрабатывать некоторые специальные символы в URL, например "%", и мой контроллер пружины находится ниже:
@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);
}
}
Следуя советам, я хочу написать перехватчик для кодированияURL-адрес перед отправкой запроса на контроллер, и мой перехватчик выглядит (еще не завершено):
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;
}
}
Я протестировал URL-адрес в своем браузере:
http://localhost:8090/extract?extractionInput={"inputInfo":{"inputText":"5.00%"}}
Из-зазнак%, я получил ошибку:
[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.
Когда я тестирую перехватчик, «request.getRequestURL ()» дает ожидаемый результат:
http://localhost:8090/extract
Однако, «запрос».getParameterNames () "всегда получает пустой объект Elumentation.Почему он не получает параметры?Я надеюсь сначала закодировать значение параметра:
"inputText":"5.00%"
'inputText' - это поле объекта InputInfo в формате json.Так как же получить параметры запроса для решения проблемы?