原来的问题是:
How to resolve URI encoding problem in spring-boot?
. 根据其中一个建议,我正试图提出一个带有拦截器的解决方案,但仍然存在一些问题。
@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);
}
}
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
但是,“request.getParameterNames()”始终获取一个空的洗脱对象。为什么它没有得到参数?我希望首先对参数值进行编码:
"inputText":"5.00%"