简体   繁体   English

如何使用RestEasy框架发送Multipart响应

[英]How to send Multipart response using RestEasy framework

I am trying to implement a webservice that accepts a json string and based on the key it fetches a .zip file. 我正在尝试实现一个接受json字符串的web服务,并根据它获取.zip文件的密钥。

Then I need to send the .zip file and a json string bundled in a multipart data. 然后我需要发送.zip文件和捆绑在多部分数据中的json字符串。

So basically my response should a multipart object containing two parts 所以基本上我的响应应该是一个包含两个部分的multipart对象

1) .zip file
2) json string

Here is my current code 这是我目前的代码

public class ContentRepo {



@POST
@Path("/fetchModel")
@Consumes("application/json")
@Produces("multipart/mixed")
public Response getContent(String strJson)
{
    Response response = null;
    try{
        JSONObject objJson = new JSONObject(strJson);
        String strAssetName = objJson.getString("assetName");
        if(null != strAssetName){
            Client client = ClientBuilder.newClient();
            ResteasyClient restEasyClient = (ResteasyClient) client;
            ResteasyWebTarget target = restEasyClient.target("http://localhost:8091/ContentServer/").path("fetchModel");
            response = target.request()
                    .post(Entity.entity(getMultiPartData("Car"), MediaType.MULTIPART_FORM_DATA));
        }
    }catch(Exception ex){
        ex.printStackTrace();
    }

    return response;
}

public MultipartFormDataOutput getMultiPartData(String strAssetName){

    MultipartFormDataOutput objMultiPartData = new MultipartFormDataOutput();
    JSONObject objJson = new JSONObject();

    try{
         if(strAssetName.equalsIgnoreCase("Car")){
            //car assets
            try {
                objMultiPartData.addFormData("file", new FileBody(new File("D:/programs/content_server/Car/Car.zip")), MediaType.APPLICATION_OCTET_STREAM_TYPE);
                objJson.put("png", "car");  
                objMultiPartData.addFormData("mapping", new StringBody(objJson.toString()), MediaType.APPLICATION_JSON_TYPE);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }catch(Exception ex){
        ex.printStackTrace();
    }

    return objMultiPartData;
}

} }

However, when the run the above I am not able to fetch the multipart response. 但是,当运行上面的时候我无法获取多部分响应。 Instead a get the below exception 相反,获得以下异常

Caused by: java.lang.NoSuchMethodError: org.jboss.resteasy.spi.ResteasyProviderFactory.<init>(Lorg/jboss/resteasy/spi/ResteasyProviderFactory;)V
at org.jboss.resteasy.client.jaxrs.internal.ClientConfiguration.<init>(ClientConfiguration.java:44)
at org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder.build(ResteasyClientBuilder.java:347)
at org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder.build(ResteasyClientBuilder.java:52)
at javax.ws.rs.client.ClientBuilder.newClient(ClientBuilder.java:114)
at com.app.wikicontent.WikitudeContentRepo.getARModel(WikitudeContentRepo.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:155)
at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257)
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222)
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211)
at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525)
... 25 more

Your stack trace is complaining that it can't find a ResteasyProvider constructor that was not introduced until org.jboss.resteasy:resteasy-jaxrs version 3.0 in order to support org.jboss.resteasy:resteasy-client . 你的堆栈跟踪抱怨它找不到一个ResteasyProvider 构造函数 ,直到org.jboss.resteasy:resteasy-jaxrs 3.0版才引入 ,以支持org.jboss.resteasy:resteasy-client So you probably have an older version of resteasy-jaxrs somewhere on your runtime classpath. 所以你可能在运行时类路径的某个地方有一个旧版本的resteasy-jaxrs Remove it and make sure that your deployed application has its versions of resteasy-jaxrs and resteasy-client in sync. 删除它并确保您部署的应用程序的resteasy-jaxrsresteasy-client版本同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM