Tomcat + Spring MVC + HttpClient: how to transmit data using PUT and PATCH methods
In RESTful APIs, The PUT/PATCH method is generally used to update data. In the project code, HttpClient 4.5 is used, which is written as follows:
protected JSONObject doHttpUriRequest(HttpUriRequest httpUriRequest) { JSONObject result = null; HttpClient httpClient = HttpClients.createDefault(); try { HttpResponse httpResponse = httpClient.execute(httpUriRequest); StatusLine responseStatusLine = httpResponse.getStatusLine(); int statusCode = responseStatusLine.getStatusCode(); if (statusCode == 200) { HttpEntity responseEntity = httpResponse.getEntity(); String jsonString = EntityUtils.toString(responseEntity, CHARACTER_SET); result = new JSONObject(jsonString); EntityUtils.consume(responseEntity); } else { // error handling } } catch (IOException e) { e.printStackTrace(); return onLocalError(e); } return result;}protected JSONObject doHttpPatch(String uri, Map
params) { JSONObject result = null; HttpPatch httpPatch = new HttpPatch(uri); List
nvps = constructNvps(params); // constructing name-value pair try { httpPatch.setEntity(new UrlEncodedFormEntity(nvps, CHARACTER_SET)); result = doHttpUriRequest(httpPatch); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return onLocalError(e); } return result;}
DoHttpUriRequest () is a tool function used to process requests, and doHttpPatch () is a function used to process data.
It can be seen that the writing method is similar to a common POST request, but it only replaces HttpPost with HttpPatch.
However, on the server side, for example, there is a parameter named key in params whose value is value. In the Controller, This Is a patch method, but the key value is null. That is, Servlet cannot obtain parameters from form.
Google checked the cause, which generally means that the PATCH method is quite new, even though Tomcat 7.0.39 is not supported. How can this problem be solved? There are two methods:
1. Request with URI
Since form cannot be used to obtain parameters, write them at the URI's tail:
protected JSONObject doHttpPatchWithURI(String uri, Map
params) { JSONObject result = null; URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setPath(uri); uriBuilder.setParameters(constructNvps(params)); try { URI builtUri = uriBuilder.build(); HttpPatch httpPatch = new HttpPatch(builtUri); result = doHttpUriRequest(httpPatch); } catch (URISyntaxException e) { e.printStackTrace(); return onLocalError(e); } return result;}
Using this method, the servlet can obtain the parameter.
This method has a problem. That is, even if the key is null, it is included in the URI parameter. Received in the Servlet, the key value is changed to "" (Null String ). In this way, there will be ambiguity in the RESTful API: is it not updated, or is it new to a null string?
2. Add a filter to web. xml.
Another way is to keep the POST style method above, and add a filter in web. xml:
HttpPutFormContentFilter
org.springframework.web.filter.HttpPutFormContentFilter
HttpPutFormContentFilter
springWebMvcDispatcher
SpringWebMvcDispatcher is the servlet name.
Filter reads data from the form data of the request body and encapsulates the data into a ServletRequest so that methods such as ServletRequest. getParameter * () can read data.