JAva RestTemplateを使用してリモートインタフェースを呼び出す

1803 ワード

1.環境:springBoot+mybatisPlus+oracle
/**
     *       
     * @param serverUrl:      
     * @param json:map       
     * @param filePath:      ,      ,      , null
     * @return
     */
    public static ResponseEntity  rstTemplate (String serverUrl,String json,String filePath){
        RestTemplate restTemplate = new RestTemplate();
        //     
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        //     ,   LinkedMultiValueMap
        MultiValueMap form = new LinkedMultiValueMap<>();
        //        ,      ,   null
        if(StringUtils.hasLength(filePath)) {
            FileSystemResource fileSystemResource = new FileSystemResource(filePath);
            form.add("file", fileSystemResource);
        }
        form.add("parameterJson",json);

        // HttpEntity        
        HttpEntity> httpEntity = new HttpEntity(form, headers);

        ResponseEntity s = restTemplate.postForEntity(serverUrl,httpEntity, String.class);
        return s;
    }

2呼び出し方法
public static void main(String[] args){
Map mapZip=new HashMap<>();
  mapZip.put("fileName",“    .txt”);
            String jsonMap=JSON.toJSONString(mapZip);
            //   
            String fileZipUrl="http://localhost:8080/uploadProject/fileService/departMentZip";
            String iniPath="d:/test/    .txt"
            //   json        ,    json       zip  
            ResponseEntity responseEntity=FileUpLoadUtil.test(fileZipUrl,jsonMap,iniPath);
   }

詳細https://mp.csdn.net/mdeditor/99713503#