Tomcat + Spring MVC + HttpClient: How to pass data using put and patch methods

Source: Internet
Author: User

In the restful style of the API. The Put/patch method is typically used to update data. In the code of the project, the HttpClient 4.5 is used, which is written in this way:

protectedJsonobjectdohttpurirequest(Httpurirequest httpurirequest) {Jsonobject result =NULL; HttpClient HttpClient = Httpclients.createdefault ();Try{HttpResponse HttpResponse = Httpclient.execute (httpurirequest); Statusline responsestatusline = Httpresponse.getstatusline ();intStatusCode = Responsestatusline.getstatuscode ();if(StatusCode = = $) {httpentity responseentity = httpresponse.getentity ();            String jsonstring = entityutils.tostring (responseentity, Character_set); result =NewJsonobject (jsonstring);        Entityutils.consume (responseentity); }Else{//Error handling}    }Catch(IOException e) {E.printstacktrace ();returnOnlocalerror (e); }returnResult;}protectedJsonobjectDohttppatch(String Uri, map<string, string>params) {Jsonobject result =NULL; Httppatch Httppatch =NewHttppatch (URI); list<namevaluepair> Nvps = Constructnvps (params);//Constructing Name-value pair    Try{httppatch.setentity (NewUrlencodedformentity (Nvps, Character_set));    result = Dohttpurirequest (Httppatch); }Catch(Unsupportedencodingexception e) {E.printstacktrace ();returnOnlocalerror (e); }returnResult;}

Dohttpurirequest () is a tool function that handles sending requests. Dohttppatch () is a function that handles data in detail.
The visual notation is almost identical to a normal post request, simply replacing the httppost with a httppatch.
But on the server side, for example, there is a parameter called key in the params. Values are value, in the controller, it is recognized that this is a patch method, but the value of key is null. Is that the servlet cannot get the number of references from the form.


Google looked at the reason, roughly speaking, Patch is a very new method, even if the Tomcat 7.0.39 is not supported.

How is that broken? There are two ways:

1. Use a URI to request

Since you can't use a form to get a reference, write it at the tail of the URI:

protecteddoHttpPatchWithURIparams) {    null;    new URIBuilder();    uriBuilder.setPath(uri);    uriBuilder.setParameters(constructNvps(params));    try {        URI builtUri = uriBuilder.build();        new HttpPatch(builtUri);        result = doHttpUriRequest(httpPatch);    catch (URISyntaxException e) {        e.printStackTrace();        return onLocalError(e);    }    return result;}

Using this approach, the servlet is able to get the number of references.


There is a problem with this approach. Even if key is a null value, the reference to the URI will also be taken. Received in the servlet, the value of key becomes "" (empty string). In the RESTful style API there will be ambiguity: in the end is not updated, or updated into an empty string it?

2. Add a filter in Web. XML

Another option is to keep the above post style method, adding a filter to Web. xml:

<filter  > << Span class= "hljs-built_in" >filter -name>httpputformcontentfilter</-name> <filter -class  >org.springframework.web. filter . Httpputformcontentfilter</filter -class  ></filter  ><filter -mapping > <filter -name>httpputformcontentfilter</ filter -name> <servlet-name>springwebmvcdispatcher</servlet-name ></filter -mapping>  

Among them Springwebmvcdispatcher is the name of the servlet.
Filter works by reading the data from the form data of the request body and wrapping it into a servletrequest, so that methods such as servletrequest.getparameter* () can read the data.

Documentation:
Http://stackoverflow.com/questions/20370927/patch-method-in-tomcat-with-spring-mvc

Tomcat + Spring MVC + HttpClient: How to pass data using put and patch methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.