Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Rest服务不接受给定的post,尽管它被注释为多部分_Java_Rest_Cxf_Apache Httpclient 4.x - Fatal编程技术网

Java Rest服务不接受给定的post,尽管它被注释为多部分

Java Rest服务不接受给定的post,尽管它被注释为多部分,java,rest,cxf,apache-httpclient-4.x,Java,Rest,Cxf,Apache Httpclient 4.x,我有一个apachecxfrest服务,其中on方法需要接收一个映像和一些其他字符串数据。为此,我创建了这样的服务定义 @Override @POST @Path("attach/{formId}/{instanceId}") @Consumes("multipart/mixed") @Produces("application/xml") public UploadResult attach(@FormParam("username") @WebParam(name = "username")

我有一个apachecxfrest服务,其中on方法需要接收一个映像和一些其他字符串数据。为此,我创建了这样的服务定义

@Override
@POST
@Path("attach/{formId}/{instanceId}")
@Consumes("multipart/mixed")
@Produces("application/xml")
public UploadResult attach(@FormParam("username") @WebParam(name = "username") String user,
                           @FormParam("password") @WebParam(name = "password") String password,
                           @PathParam("formId") @WebParam(name = "formId") String formId,
                           @PathParam("instanceId") @WebParam(name = "instanceId") String instanceId,
                           @FormParam("group") @WebParam(name = "group") String group,
                           @FormParam("data") @WebParam(name = "data") byte[] data,
                           @FormParam("metadata") @WebParam(name = "metadata") byte[] metadata)
    throws PDProcessingException,
    PDSecurityException
{
    return svc.attach( user, password, formId, instanceId, group, data, metadata );
}
现在我尝试在我的客户机上使用这个方法,但它不起作用,并且返回415个不受支持的mediatype。我用Wiztools.org RestClient和一个调用该方法的测试客户端代码进行了尝试

public static String attach(String username, String hashedpassword, String attachmentUri) {

    String retVal = "";
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://192.168.101.27/pdxapi/service/rest/ClientApp/attach/DE_OJ_PDIX_TEST/SRSQS.0");
    try {
        String hashedPw = DigestFactory.calculatePasswordHash(hashedpassword);

        File file = new File(attachmentUri);
        if (file == null || !file.exists()){
            throw new RuntimeException("there is no file " + attachmentUri);
        }
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("username", new StringBody(username));
        entity.addPart("password", new StringBody(hashedPw));
        FileBody fileBody = new FileBody(file,"image/jpeg");
        entity.addPart("data",fileBody);

        post.setEntity(entity);
        HttpResponse response = client.execute(post);
        if (response.getEntity() == null){

            retVal = "";                
        }
        else{
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }               
            retVal = sb.toString();             
        }

    } catch (IOException e) {
        System.out.print("exception " + e.getMessage());
    }
    return retVal;
}
我还尝试了注释@Consumes(“多部分/表单数据”),但结果相同。 有什么建议吗

谢谢


将所需的jar文件添加到您的项目中

我通过更改方法并使用此处描述的MultiPartBody解决了这个问题