Here is my application.
@SpringBootApplication
@EnableFeignClients
public class PostLabApplication {
public static void main(String[] args) {
SpringApplication.run(PostLabApplication.class, args);
}
@Data
public static class Book {
private String author;
private String title;
}
@Data
private static class Comment {
private String id;
private String content;
private MultipartFile file;
}
@RestController
public class MyController {
@Autowired
MyFeignClient myFeignClient;
@PostMapping(value = "/books", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String addBook(@RequestPart("book") Book book, @RequestPart(name = "comments") List<Comment> comments) {
System.out.println(book);
System.out.println(comments);
System.out.println("addBook - Ok");
return "addBook - Ok";
}
@GetMapping("/books-client")
public String consumeAddBook() throws Exception {
Book book = new Book();
book.setAuthor("John");
book.setTitle("Java");
List<Comment> comments = new ArrayList<>();
Comment comment = new Comment();
comment.setId("001");
comment.setContent("hello");
ClassPathResource resource = new ClassPathResource("application.properties");
InputStream inputStream = resource.getInputStream();
// InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
// BufferedReader reader = new BufferedReader(streamReader);
// String content = "";
// for (String line; (line = reader.readLine()) != null;) {
// content += line;
// }
// System.out.println(content);
MultipartFile multipartFile = new MockMultipartFile("application.properties", "application.properties", "text/plain", IOUtils.toByteArray(inputStream));
comment.setFile(multipartFile);
comments.add(comment);
myFeignClient.addBook(book, comments);
return "CLIENT OK";
}
}
@FeignClient(name = "book", url = "http://127.0.0.1:8080")
public interface MyFeignClient {
@PostMapping(value = "/books", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String addBook(@RequestPart("book") Book book, @RequestPart(name = "comments") List<Comment> comments);
}
public class MyClientConfiguration {
@Bean
JsonFormWriter jsonFormWriter() {
return new JsonFormWriter();
}
}
@Configuration
public class SerializationConfiguration {
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
}
}
Calling "/book-client" is giving me that exception :
feign.codec.EncodeException: Unexpected IOException (of type java.io.FileNotFoundException): MultipartFile resource [application.properties] cannot be resolved to absolute file path
Yet if I uncomment the commented lines, the file content is displaying.
And if I comment the comment.setFile(multipartFile);
it is working but of course with a null file : [PostLabApplication.Comment(id=001, content=hello, file=null)]
I also tried with MultipartFile multipartFile = new MockMultipartFile("application.properties", "Hello".getBytes());
but same result.
What's the problem ? What's the solution for having it working ?