引包
<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;
}
}
}