Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to understand the helper class in restTemplateUtil

Shulou Source: shulou.com Published: 2022-06-02 02:39:43 10月03日 Update

In this issue, the editor will bring you about how to understand the help class in restTemplateUtil. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Import com.zzb.business.customerInfo.config.ReportConfig;import com.zzb.business.customerInfo.config.connection.PengYuanConfig;import org.springframework.core.io.FileSystemResource;import org.springframework.http.HttpEntity;import org.springframework.http.HttpMethod;import org.springframework.http.ResponseEntity;import org.springframework.http.client.ClientHttpResponse;import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;import org.springframework.web.client.DefaultResponseErrorHandler;import org.springframework.web.client.RestTemplate;import java.io.File;import java.io.IOException Import java.util.Map / * * @ author edison_kwok * / public class RestTemplateUtils {/ * can set the post method * * @ param url * @ param headers * @ param params * @ param restTemplate * @ return * / public static String postConnect (String url, Map headers, Map params, RestTemplate restTemplate) {/ / header information Includes http basic authentication information MultiValueMap headersMap = new LinkedMultiValueMap () For (String key: headers.keySet ()) {headersMap.add (key, headers.get (key));} / / body request body part MultiValueMap bodyMap = new LinkedMultiValueMap (); for (String key: params.keySet ()) {bodyMap.add (key, params.get (key)) } / / merge becomes a HttpEntity HttpEntity multiValueMapHttpEntity = new HttpEntity (bodyMap, headersMap) / / respond normally when the response value is 400 or 401. Do not throw restTemplate.setErrorHandler (new DefaultResponseErrorHandler () {@ Override public void handleError (ClientHttpResponse response) throws IOException {if (response.getRawStatusCode ()! = 400 | | response.getRawStatusCode ()! = 401) {super.handleError (response)) }); ResponseEntity responseEntity = restTemplate.exchange (url, HttpMethod.POST, multiValueMapHttpEntity, String.class); return responseEntity.getBody () } / * upload files to the file system * * @ param file * @ return * / public static String upload (File file, RestTemplate restTemplate, String uploadUrl) {if (! file.exists ()) {return null;} / / transfer files to the file management system FileSystemResource resource = new FileSystemResource (file) MultiValueMap param = new LinkedMultiValueMap (); param.add ("file", resource); ResponseEntity pathEntity = restTemplate.postForEntity (uploadUrl, param, String.class); / / Delete the local file file.delete (); / / return the file path for example: risk/file/customerReport/20190621/19062113582560509589.pdf try {return JsonUtils.parse (pathEntity.getBody (), new Object [0]) } catch (IOException e) {e.printStackTrace ();} return null;}} the above is shared by the editor on how to understand the help classes in restTemplateUtil. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Files help information content parameters systems analysis professional small and medium rich in content that is articles methods time more knowledge management systems articles industries perspectives Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL NVidia OPPO Reno Linux Shulou Technology