栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

RestTemplate exchange

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

RestTemplate exchange

@GetMapping("/putByText")
@ApiOperation(value = "远程更新资源", position = 1, httpMethod = "GET", response = Result.class)
public Result putByText(@ApiParam(value = "text参数", name = "text") @RequestParam(name = "text") String text){
    MultiValueMap map = new linkedMultiValueMap<>();
    map.add("text", text);
    JSonObject jsonObject = new RestTemplateUtil().putSend(restTemplate,"url", map);
    return Result.build(jsonObject);
}


@PostMapping("/postUploadFile")
@ApiOperation(value = "远程上传文件", position = 1, httpMethod = "POST", response = Result.class)
public String postUploadFile(MultipartFile file) throws IOException {
    MultiValueMap bodyParams = new linkedMultiValueMap<>();
    org.springframework.core.io.Resource resource = new ByteArrayResource(file.getBytes()){
        @Override
        public String getFilename() {
            return file.getOriginalFilename();
        }
    };
    bodyParams.add("file", resource);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity> requestEntity = new HttpEntity<>(bodyParams, headers);
    String result= restTemplate.postForObject("url", requestEntity, String.class);
    return result;
}

工具类

package cn.piesat.ias.util;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

public class RestTemplateUtil {

    private static final Logger log = LoggerFactory.getLogger(RestTemplateUtil.class);

    
    public JSonObject putSend(RestTemplate restTemplate ,String apiUrl, MultiValueMap map) {
        JSonObject jsonObject = new JSonObject();
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//            List acceptableMediaTypes = new ArrayList<>();
//            acceptableMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
//            acceptableMediaTypes.add(MediaType.APPLICATION_PROBLEM_JSON_UTF8);
            HttpEntity> httpEntity = new HttpEntity<>(map, headers);
            ResponseEntity response = restTemplate.exchange(new URI(apiUrl), HttpMethod.PUT, httpEntity,String.class);
            String result = response.getBody();
            jsonObject = JSONObject.parseObject(result);
            jsonObject.put("responseCode","200");
        }catch (Exception e){
            jsonObject.put("responseCode","500");
            log.error("调用restTemplate.exchange()"+e);
        }
        return jsonObject;
    }

    
    public String exchange(RestTemplate restTemplate ,String apiUrl,HttpMethod method ,MultiValueMap map) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            HttpEntity> httpEntity = new HttpEntity<>(map, headers);
            ResponseEntity response = restTemplate.exchange(new URI(apiUrl), method, httpEntity,String.class);
            String result = response.getBody();
            return result;
        }catch (Exception e){
            log.error("调用restTemplate.exchange()"+e);
        }
        return null;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591567.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号