通过Jersey REST Client使用multipart文件POST formdata

时间:2018-06-05 08:09:42

标签: java rest spring-mvc jersey jersey-client

我正试图通过Jersey REST Client发布一些formdata和多部分文件。我为jersey客户端添加了以下依赖项:

<dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.9</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.9</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.9</version>
    </dependency>

我的POST控制器方法如下:

@RequestMapping(value = "/formdata/submit", method = RequestMethod.POST)
public String postFormData(@ModelAttribute String formData, 
@RequestParam("file) MultipartFile file, Model model, HttpServletRequest 
request, HttpServletResponse response)
{
try
{
    ObjectMapper mapper = new ObjectMapper();
    String jsonData = mapper.writeValueAsString(formData);
    String postUrl = getPostUrl();

    ClientConfig config = new DefaultClientConfig();
    com.sun.jersey.api.client.Client client = 
    com.sun.jersey.api.client.Client.create(config);
    WebResource webResource = client.resource(postUrl);

    ClientResponse responseMsg = webResource
        .queryParam("jsonData", jsonData)
        .queryParam("file", file.toString())
        .post(ClientResponse.class);
}
catch (Exception e)
{
    return "error";
}
return "success";
}

My Spring MVC REST控制器接受多部分文件,如下所示:

@Consumes(MediaType.MULTIPART_FORM_DATA)
@RequestMapping(value = "/save-comment", method = RequestMethod.POST)
public String addComment(@FormDataParam("jsonData") String jsonData, 
@FormDataParam("file") MultipartFile file, ModelMap model)
{
    //My Logic to save file and data
}

当我尝试将数据和文件一起发布到我的Spring MVC REST Controller方法时,我的泽西客户端帖子给了我一个错误。

我收到错误:

<!DOCTYPE html><html><head><title>Apache Tomcat/8.0.32 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [[Lorg.springframework.web.multipart.MultipartFile;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [Lorg.springframework.web.multipart.MultipartFile;.&lt;init&gt;()</h1><div class="line"></div><p><b>type</b> Exception report</p><p><b>message</b> <u>Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [[Lorg.springframework.web.multipart.MultipartFile;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [Lorg.springframework.web.multipart.MultipartFile;.&lt;init&gt;()</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b></p><pre>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [[Lorg.springframework.web.multipart.MultipartFile;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [Lorg.springframework.web.multipart.MultipartFile;.&lt;init&gt;()
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)

我需要使用com.sun.jersey在multipart文件中发布formdata 我能够使用org.glassfish.jersey.media找到一些建议,但没有找到我正在寻找的任何建议。

0 个答案:

没有答案