Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the helper class in restTemplateUtil

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report