Specific methods used by spring’s resttemplata
Basic concepts
Spring resttemplate is a client provided by spring to access rest services. Resttemplate provides a variety of convenient methods to access remote HTTP services, which can greatly improve the writing efficiency of the client. Therefore, many clients, such as Android or third-party service providers, use resttemplate to request restful services.
Resttemplata of spring web encapsulates the underlying HTTP of Java. With resttemplata, users can no longer pay attention to the underlying connection establishment. Resttemplata not only supports the rest specification, but also defines the return value object type.
In use, you can directly create a resttemplate object. In the resttemplate object we created, there will be some message converters that return messages. You can find the corresponding converter and perform mediatype conversion according to the mediatype of the returned data. You can also create a message converter, create a class to inherit the abstractgenerichttpmessageconverter < T > class, or implement the httpmessageconverter < T > interface. It should be noted that the canread method and canwrite method should make their own judgment and write parameters to the stream in the writeinternal or write method, Get the returned result from the body of the stream in the readinternal or read method and type map it.
Resttemplate objects are created at the bottom by using Java Net package. You can specify different HTTP request methods by using clienthttprequestfactory.
The clienthttprequestfactory interface mainly provides two implementation methods:
Resttemplate uses simpleclienthttprequestfactory by default, and calls HTTPCONNECTION of JDK internally. The default timeout is - 1. We can define the timeout ourselves
Use get request:
Resttemplate source code:
It's best to use the get request to splice the parameters directly to the address. I don't know why. If the third parameter is used, even the multivaluemap type can't be used (someone on the Internet said that the multivaluemap type can be used, but I tried not)
Use post request:
Resttemplate source code:
Use put request:
Resttemplate source code:
This method has a small disadvantage that there is no return value of the request result. If the return value needs to be used, this method cannot be used.
If you want to use a delete type request, there are only the following types in the parameter column of the put method of resttemplate
These methods do not give us parameters to put the content of the request body. Therefore, if you want to directly use the delete method provided by resttemplate, the interface must use restful style, put the parameters in the address, and obtain the parameters through the @ pathvariable (value = "") annotation.
Important: in fact, we can directly use the exchange method of resttemplate, as follows
Only one method is listed here. Others can see the source code. This method can make all types of requests.
In this method, the method parameter can be obtained through the httpmethod enumeration. The requestentity parameter is its own encapsulated httpentity entity entity, including the request body and request header. The responsetype parameter is the mapping class that returns the result, The parameter urivariables gives me the impression that it is chicken ribs (personal opinion). The return interface of the request can be obtained through the getbody () method of the return value of the method.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.