我使用Jersey 2.1成功创建了一个小型服务器,包括jersey-media-multipart,可以上传文件。
现在我想使用Apache CXF开发一个简单的客户端,使用与我的服务器相同的接口。
简化,我的代码如下所示:
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
/**
* @author outofrange
*/
public class JaxRSClient {
public static void main(String[] args) throws IOException {
JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
bean.setServiceClass(MultipartRestService.class);
bean.setAddress("http://www.google.at");
bean.getOutInterceptors().add(new LoggingOutInterceptor());
bean.create(MultipartRestService.class).multipathRequest(Files.newInputStream(new File("pom.xml").toPath(), StandardOpenOption.READ));
}
}
@Path("/multipart")
interface MultipartRestService {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
Response multipathRequest(@FormDataParam("inputStream") InputStream inputStream);
}
我可以看到CXF使用multipart / formdata作为标题中的Content-Type,但是有效负载格式不正确:没有边界,没有内容处置:
Jul 13, 2014 3:57:43 PM org.apache.cxf.interceptor.LoggingOutInterceptor
INFORMATION: Outbound Message
---------------------------
ID: 1
Address: http://www.google.at/multipart
Http-Method: POST
Content-Type: multipart/form-data
Headers: {Content-Type=[multipart/form-data], Accept=[application/xml]}
Payload: <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.outofrange.sscce</groupId>
<artifactId>jaxrsclient</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</project>
--------------------------------------
如果我针对我的真实服务器(不仅是Google作为虚拟地址)发出请求,我会收到400 - 错误请求回复。
Apache CXF能否自动检测jersey-media-multipart并创建代理?