0

I have a java bean which has both multipart and String data. I am trying to pass it in a rest client call which takes this java bean input and processes it.

Below are my model class, controller and rest client. On making a call from my rest client , I am getting this exception.

Exception in thread "main" org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.techidiocy.models.NHPdfMergeRequest] and content type [multipart/form-data]
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:810)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:594)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:384)

Model Class

import org.springframework.web.multipart.MultipartFile;
public class Candidate {

private String firstName;
private String lastName;
private MultipartFile resume;

//getters and setters
}

Controller Class

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class CandidateController {

    @Autowired
    private CandidateService candidateService;

    @RequestMapping(method=RequestMethod.POST, path="/add")
    public void add(@RequestBody Candidate request) {
        // do some processing
        String firstName = request.getFirstName();
        String lastName = request.getLastName();
        MultipartFile resume = request.getResume();

        candidateService.add(firstName, lastName, resume);
    }   
}

Rest Client

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.client.RestTemplate;

public class CandidateClient {

    public static void main(String[] args) throws IOException {
        String serverURL = "http://localhost:8080/add";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        Candidate candidate = new Candidate();
        candidate.setFirstName("John");
        candidate.setLastName("Doe");
        candidate.setResume(new MockMultipartFile("tmp.pdf", FileUtils.readFileToByteArray(new File("/home/john/resume/john.pdf"))));

        HttpEntity<Candidate> httpEntity = new HttpEntity<Candidate>(candidate, headers);

        RestTemplate client = new RestTemplate();
        client.postForEntity(serverURL, httpEntity, Resource.class);

    }

}

Note: I had also tried to set the header content type as json in rest client and then I am getting all the values as Null in the controller. headers.setContentType(MediaType.APPLICATION_JSON);

I had also searched over the internet for this kind of scenario but I am unable to find a solution for this.

I had also tried to pass all the parameters separately (not as part of java bean) then I am able to make it work.

saurav
  • 3,424
  • 1
  • 22
  • 33
  • possible duplicate https://stackoverflow.com/questions/42354139/spring-multipart-file-with-requestbody – Scary Wombat Mar 05 '19 at 02:43
  • In the above link it is taking 2 different params , 1 for request body and 2nd for multipart files. I am trying to achieve the same with 1 parameter only. i.e. `Candidate `bean – saurav Mar 05 '19 at 02:48
  • There is no converter for your `Candidate`class by default. So, you have either to create the converter or use params separately. – Ken Bekov Mar 05 '19 at 05:18
  • @KenBekov - yeah I had tried to create a converter but I got stuck at the point where how will i get the Candidate bean from the inputStream , which i get it from the httpentity object in read method. – saurav Mar 05 '19 at 05:37

0 Answers0