zoukankan      html  css  js  c++  java
  • feign调用远程服务 并传输媒体类型

    引包

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.2.0.RELEASE</version>
    </dependency>

    媒体文件feign支持config
    @Configuration
    @RequiredArgsConstructor(onConstructor = @__(@Autowired))
    public class FeignSupportConfig {

    private final ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    public Encoder feignFormEncoder() {
    return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }
    }



    写feignClient
    package com.huatai.txasr.interfance;

    import com.huatai.txasr.config.FeignSupportConfig;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;
    @FeignClient(name = "txasr" ,url = "https://******.com",configuration = FeignSupportConfig.class)
    public interface PublicClientIface {

    @RequestMapping(method = RequestMethod.POST, value = "/xxxx/media/voice/xxxxx",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String publicUpload(@RequestParam(value = "access_token")String access_token, @RequestParam(value = "format")String format, @RequestParam(value = "voice_id")String voice_id,@RequestPart(value = "file") MultipartFile file);

    }
    写调用端
    import com.alibaba.fastjson.JSON;
    import com.huatai.txasr.dto.PublicAsrOutDto;
    import com.huatai.txasr.dto.PublicTokenOutDto;
    import com.huatai.txasr.interfance.PublicAsrResult;
    import com.huatai.txasr.interfance.PublicClientIface;
    import com.huatai.txasr.interfance.TokenClient;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;

    /**
    * @author zhangshiyu
    * @version 1.0
    * @date 2020/10/26 11:01
    */
    @RestController
    @RequestMapping("/public")
    @CrossOrigin
    public class PublicAsrController {
    @Autowired
    PublicClientIface publicClientIface;

    @Autowired
    TokenClient tokenClient;

    @Autowired
    PublicAsrResult publicAsrResultl;



    @PostMapping("/asr")
    public String testPubliceAsr(@RequestPart(value = "file") MultipartFile file){
    PublicTokenOutDto publicTokenOutDto = tokenClient.publicToken("g****");
    String assessToken = publicTokenOutDto.getToken();
    String publicAsrOutDtoStr = publicClientIface.publicUpload(assessToken,"mp3","cs",file);
    PublicAsrOutDto publicAsrOutDto = JSON.parseObject(publicAsrOutDtoStr,PublicAsrOutDto.class);
    System.out.println(publicAsrOutDtoStr);
    if("0".equals(publicAsrOutDto.getErrcode())){
    String asrResult = publicAsrResultl.publicAsr(assessToken,"cs");
    System.out.println(asrResult);
    return asrResult;
    }else {
    return publicAsrOutDtoStr;
    }

    }


    }


    talk is cheap. show me the code.
  • 相关阅读:
    HDU-2602-Bone Collector
    HDU-1171-Big Event in HDU
    javascript概要
    核桃的数量
    P3372 【模板】线段树 1
    P3373 【模板】线段树 2
    拿糖果
    第二点五个不高兴的小明
    树的直径
    1240. 完全二叉树的权值
  • 原文地址:https://www.cnblogs.com/yushizhang/p/13891730.html
Copyright © 2011-2022 走看看