POST上传文件接口携带JSON参数,并且调用
项目开发中,与其他业务服务发送配置,需要传递JSON和图片,有所得,遂记此随笔!!!
文件上传接口
import cn.hutool.core.util.RandomUtil; import com.coalbot.isapidemo.entity.OCRParam; import com.coalbot.isapidemo.entity.OCRParamEx; import org.springframework.core.io.FileSystemResource; import org.springframework.http.*; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @ClassName:TestController * @Author: XinHai.Ma * @Date: 2025/3/12 15:15 * @Description: 接口调用测试 */ @RequestMapping("/channel") @RestController public class TestController { /** * 文件上传+简单JSON参数 * * @param param * @return */ @PostMapping("/upload") public Map<String, Object> upload(OCRParam param) { Map<String, Object> result = new LinkedHashMap<>(); result.put("getAlgorithm_key", param.getAlgorithm_key()); result.put("getAlgorithm_name", param.getAlgorithm_name()); result.put("images", param.getImages().stream().map(MultipartFile::getOriginalFilename).collect(Collectors.toList())); result.put("meter_type", param.getAlgorithm_key()); return result; } /** * 文件上传+复杂JSON参数 * * @param images * @param param * @return */ @PostMapping("/uploadEx") public Map<String, Object> uploadEx(@RequestPart("images") List<MultipartFile> images, @RequestPart("param") OCRParamEx param) { Map<String, Object> result = new LinkedHashMap<>(); result.put("getAlgorithm_key", param.getAlgorithm_key()); result.put("getAlgorithm_name", param.getAlgorithm_name()); result.put("images", images.stream().map(MultipartFile::getOriginalFilename).collect(Collectors.toList())); result.put("meter_type", param.getAlgorithm_key()); result.put("pointer", param.getPointer()); result.put("digit", param.getDigit()); return result; } }
OCRParam
@Data public class OCRParam { private String algorithm_key; private String algorithm_name; private List<MultipartFile> images; private String meter_type; }
OCRParamEx
@Data public class OCRParamEx { private String algorithm_key; private String algorithm_name; private String meter_type; private PointerParam pointer; private DigitParam digit; }
PointerParam
@Data public class PointerParam { private String name; private Integer type; private String min_value; private String max_value; private Integer value_num; }
DigitParam
@Data public class DigitParam { private Integer point_location; }
ApiPost调用
文件上传+简单JSON参数接口调用
文件上传+复杂JSON参数接口调用
ApiPost传递参数我会设置,用RestTemplate怎么调用呢?我还是第一次调用参数这么复杂的接口?
调用接口
/** * RestTemplate调用接口,文件上传+复杂JSON参数 * * @return */ @PostMapping("/callUploadEx") public String callUploadEx() { String jsonStr = "{ " + "\"algorithm_key\" : \"algorithm_key\", " + "\"algorithm_name\" : \"algorithm_name\", " + "\"meter_type\" : \"" + RandomUtil.randomInt(1, 101) + "\", " + "\"pointer\" : {" + " \"name\":\"仪表名称\", " + " \"type\":" + RandomUtil.randomInt(1, 101) + ", " + " \"min_value\":\"" + RandomUtil.randomInt(1, 101) + "\", " + " \"max_value\":\"" + RandomUtil.randomInt(1, 101) + "\"," + " \"value_num\":" + RandomUtil.randomInt(1, 101) + " }, " + "\"digit\" : {" + " \"point_location\":" + RandomUtil.randomInt(1, 101) + " } " + "}"; // 设置请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); // 设置参数 MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); FileSystemResource file = new FileSystemResource("C:\\Users\\maxin\\Pictures\\heiandijia.webp"); FileSystemResource file1 = new FileSystemResource("C:\\Users\\maxin\\Pictures\\1.webp"); body.add("images", file); body.add("images", file1); // 设置JSON请求头 HttpHeaders jsonHeaders = new HttpHeaders(); jsonHeaders.setContentType(MediaType.APPLICATION_JSON); // 设置JSON参数 body.add("param", new HttpEntity<>(jsonStr, jsonHeaders)); HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); ResponseEntity<String> response = new RestTemplate().exchange("http://localhost:8082/channel/uploadEx", HttpMethod.POST, requestEntity, String.class); return response.getBody(); }
需要嵌套请求头,并且对文件参数请求头和JSON参数请求头分别设置ContentType,学到了