spring controller同時(shí)接口文件參數(shù)和json入?yún)⒛叶福⑦@些參數(shù)通過(guò)feign請(qǐng)求到其他微服務(wù)薪前。RPC調(diào)用通過(guò)springcloud實(shí)現(xiàn)。
示例:服務(wù)1:provider1关斜,服務(wù)2:provider2
provider1代碼示例:
controller接收參數(shù)示例:
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Slf4j
@RestController
@RequestMapping("/xxx")
public class MyController {
@RequestMapping("/reqFile")
public Object uploadFile(MultipartFile file, @ModelAttribute CommonReq req)
{
// xxx邏輯
// do feign
xxx.feign(file, req);
}
}
feign調(diào)用示例
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import feign.hystrix.FallbackFactory;
@FeignClient(value = "provider2", fallbackFactory = MyFeignFallbackFactory.class)
public interface MyFeign {
@PostMapping(value = "/xxx", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Resp feign(@RequestPart("file") MultipartFile file, @RequestPart("req")CommonReq req);
}
提示
我這里使用的springboot版本2.5.14示括,但上傳文件的時(shí)候之前uploadFile方法的file參數(shù)接收文件一直為null,然后再yml文件里面加上這行配置之后就可以接收到文件了痢畜,但是沒(méi)太搞懂為什么
spring:
mvc:
hiddenmethod:
filter:
enabled: true
provider2接收f(shuō)eign請(qǐng)求示例
controller接收示例:
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@RestController
@RequestMapping("/xxx")
public class Provider2Controller {
@PostMapping("/xxx")
public Object feignFile(@RequestPart("file") MultipartFile file, @RequestPart("req") String req){
// xxx邏輯
}
}