[request by Put] failed to parse the parameters and put it back to Request by HttpServletRequest

questions about parsing parameters in request and putting them back in request

get and post follow the method , parsing is normal, but an exception is thrown when a put request is encountered.

put requests request of spring , while tomcat is request of apache . Although they all inherit from HttpServletRequest , they throw a class conversion exception

when converting request .

has tried many methods, but all of them are ineffective in the end. the requirement to be realized is that the client passes the parameters, the interceptor parses the parameters and re-assigns them, and the control layer acquires the re-assigned parameters and does the corresponding logic.

this is the initialization assignment

private static Field requestField;  
  
private static Field parametersParsedField;  
  
private static Field coyoteRequestField;  
  
private static Field parametersField;  
  
private static Field hashTabArrField;  

.....

try {  
    Class clazz = Class.forName("org.apache.catalina.connector.RequestFacade");  
    requestField = clazz.getDeclaredField("request");  
    requestField.setAccessible(true);  
  
    parametersParsedField = requestField.getType().getDeclaredField("parametersParsed");  
    parametersParsedField.setAccessible(true);  
  
    coyoteRequestField = requestField.getType().getDeclaredField("coyoteRequest");  
    coyoteRequestField.setAccessible(true);  

    parametersField = coyoteRequestField.getType().getDeclaredField("parameters");  
    parametersField.setAccessible(true);  
  
    hashTabArrField = parametersField.getType().getDeclaredField("paramHashStringArray");  
    hashTabArrField.setAccessible(true);  
} catch (Exception e) {  
    e.printStackTrace();  
}  

resolve request parameters in this method

    Object innerRequest = requestField.get(request);//
    parametersParsedField.setBoolean(innerRequest, true);
    Object coyoteRequestObject = coyoteRequestField.get(innerRequest);
    Object parameterObject = parametersField.get(coyoteRequestObject);
Mar.01,2021

this method is not recommended, and it has been explained in the article corresponding to the connection that you can use the form of the wrapper class. It is safe and efficient to handle in the form of wrapper classes

Menu