I need to send a file and an entity to my server, my server is a spring boot app:
@PostMapping("/upload")
public void upload(@RequestParam("dto") MyDto dto,
@RequestParam("file") MultipartFile file) {
...
}
MyDto.java:
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyDto implements Serializable {
private String f1;
private String f2;
}
And my client:
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
new File("C:/dev/test.txt"),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
MyDto dto = new MyDto();
dto.setF1("f1");
dto.setF2("f2");
final FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart
.field("dto", dto, MediaType.APPLICATION_JSON_TYPE) // if I change to string type works fine;
.bodyPart(fileDataBodyPart);
Response response = ClientBuilder.newClient()
.target(String.format("%s%s", "http://localhost:8080", "/api/upload"))
.register(MultiPartFeature.class)
.request(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + token.getToken())
.post(Entity.entity(multipart, multipart.getMediaType()));
response -> InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://localhost:8080/api/upload, status=500, reason=Internal Server Error}}
So, someone have an ideia what's wrong?