【问题标题】:Upload file to a graphql springboot microservice from another springboot microservice从另一个 spring boot 微服务将文件上传到 graphql spring boot 微服务
【发布时间】:2022-01-15 17:44:19
【问题描述】:

我有一个 springboot 微服务,它是使用 graphql 技术实现的,并公开了一个 mutation 作为端点来上传文件和其他文件相关信息。

mutation($file: Upload, $fileInfo: FileInfo) {
    uploadFile(file: $file, fileInfo: $fileInfo)
}

input FileInfo {
    name: String
    size: Int
}

Curl 命令调用这个,

curl -v -L http://localhost:8087/graphql \
-F operations='{ "query": "mutation($file: Upload, $fileInfo: FileInfo) { uploadFile(file: $file, fileInfo: $fileInfo)}", "variables": { "file": null, "fileInfo": {"name": "test.json", "size": 500} } }' \
-F map='{ "0": ["variables.file"]}' \
-F 0=test.json

同样通过邮递员实现,

我知道调用这个突变端点并从 UI 上传文件很容易,因为有许多好的客户端可用于 UI,但我必须从另一个 springboot 微服务调用这个突变,该微服务的文件为 InputStream 和FileInfo 存储在 HashMap 中。

如何通过 java 代码实现这一点?我们可以创建一个 restTemplate.exchangerestTemplate.postForEntity http 请求,以 MediaType.MULTIPART_FORM_DATA 作为内容类型来实现这一点吗?一个代码 sn-p 会非常有用。

【问题讨论】:

    标签: java spring-boot http graphql


    【解决方案1】:

    这也给我带来了困难,所以你去吧。 [Spring Boot 2.6.3 / DGS Netflix / Java 17]

    我在“uploadFile.graphql”文件中添加了查询

    1. 上传文件.graphql
    mutation($file: Upload!, $fileInfo: FileInfo!) {
        uploadFile(file: $file, fileInfo: $fileInfo)
    }
    
    1. 这是一个示例代码。 (没有运行,但应该可以)
    
    @Autowired
    private RestTemplate restTemplate;
    
    @Autowired
    private ObjectMapper objectMapper;
    
    private void uploadFile(){
    
      // Build the Query Variables Map
      var fileInfo = new FileInfo("test.json", 500);
      var fileInfoJson = objectMapper.writeValueAsString(fileInfo);
    
      var variables = new LinkedHashMap();
      variables.put("fileInfo", fileInfoJson);
      variables.put("file", null);
    
      // Build the GraphQL-Params
      var query = StreamUtils.copyToString( new ClassPathResource("uploadFile.graphql").getInputStream(), Charset.defaultCharset());
    
      var params = new LinkedHashMap();
      params.put("operationName", "FileUploadMutation");
      params.put("variables", variables);
      params.put("query", query)
    
      // Build Multi-Part Upload Request
      var file = Paths.get("src/test/resources/test.json");
      try{
        var contentBytes = Files.readAllBytes(file)
        var multiPartFile = new MultiPartByteArrayResource(file.getFileName(), contentBytes);
    
        var formData = new LinkedMultiValueMap<>();
        formData.set("operations", objectMapper.writeValueAsString(params));
        formData.set("map": "{\"file\": [\"variables.file\"]}");
        formData.set("file",multiPartFile);
    
        var headers = new HttpHeaders(); // If you need additional headers... here you go!
      
        var requestEntity = new HttpEntity<>(formData, headers);
        var response = restTemplate.exchange("/graphql", HttpMethod.POST, requestEntity, String.class);
    
        //Done!
      } catch(final IOException e){
        log.error(e.getMessage());
      }
    }
    

    我将它用于我的 GraphQl-Upload Mutations 的集成测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 2021-08-06
      • 2021-09-28
      • 2020-03-11
      • 2022-01-08
      • 2020-09-27
      • 2019-02-20
      相关资源
      最近更新 更多