在Java对象中发送Multipart和JSON数据-Spring Boot Rest

时间:2019-03-05 02:08:36

标签: java spring spring-restcontroller

我有一个同时具有multipart和String数据的java bean。我试图在一个休息客户端调用中传递它,该调用接受此java bean输入并对其进行处理。

下面是我的模型类,控制器和rest客户。 从我的其他客户拨打电话时,我遇到了这个异常。

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)

模型类

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

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

//getters and setters
}

控制器类

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);
    }   
}

其他客户

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);

    }

}

注意:我还尝试在其他客户端中将标头内容类型设置为json,然后在控制器中将所有值都设置为Null。 headers.setContentType(MediaType.APPLICATION_JSON);

我也已经在互联网上搜索了这种情况,但是我无法找到解决方案。

我还尝试过分别传递所有参数(而不是作为Java bean的一部分),然后能够使其工作。

0 个答案:

没有答案