0

Tried to upload a file via Rest request using MultiPartFile. Getting the request in the controller in App A, which calls service, which then calls FeignClient to invoke different REST endpoint in App B.

Controller class

@RestController("strategy")
@Validated
public class StrategyController {
  private final StrategyService strategyService;

  public StrategyController(StrategyService strategyService) {
    this.strategyService = strategyService;
  }

  @RequestMapping(path = ASSORT_STRAT_UPLOAD_REQUEST, method = RequestMethod.POST)
  public ResponseEntity<String> upload(MultipartFile file) {
    return ResponseEntity.ok(strategyService.upload(file));
  }

Service class

@Service
public class StrategyServiceImpl implements StrategyService {
  private final strategyServiceRestClient strategyServiceRestClient;

  public StrategyServiceImpl(strategyServiceRestClient strategyServiceRestClient) {
    this.strategyServiceRestClient = strategyServiceRestClient;
  }

  @Override
  public String upload(MultipartFile file) {
    return strategyServiceRestClient.uploadRequest(file);
  }
}

Feign Client

@FeignClient(value = "strategy", url = "${strategy-service-url}", configuration = StrategyServiceFeignConfig.class)
public interface StrategyServiceRestClient {
  @RequestMapping(method = RequestMethod.POST, value = KimsBffConstants.ASSORT_STRAT_UPLOAD_REQUEST)
  @HandleFeignException(HandleFeignClientException.class)
  String uploadRequest(@RequestPart MultipartFile file);
}

Seeing the following exception when it calls strategyServiceRestClient.uploadRequest(file);

Caused by:

java.io.FileNotFoundException: MultipartFile resource [file] cannot be resolved to URL 
    at org.springframework.core.io.AbstractResource.getURL(AbstractResource.java:113)
    at org.springframework.core.io.AbstractResource.getURI(AbstractResource.java:123)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:569)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:689)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:774)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:774)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeOptionalFields(MapSerializer.java:869)
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeWithoutTypeInfo(MapSerializer.java:760)
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:720)
    at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:35)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:400)
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1514)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1007)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:456)
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:104)
    at org.springframework.cloud.openfeign.support.SpringEncoder.checkAndWrite(SpringEncoder.java:217)
    at org.springframework.cloud.openfeign.support.SpringEncoder.encodeWithMessageConverter(SpringEncoder.java:146)

This is the request/response from postman enter image description here

2
  • Thank yo. I've added the screenshot of my postman test to the actual question Commented Dec 31, 2024 at 14:22
  • please add the complete request headers (not as screenshot please) or the complete raw request. Response headers might also be useful, but you can try without for now. Commented Dec 31, 2024 at 14:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.