无法将文件上传到嵌入式OpenEJB

时间:2013-01-30 12:22:08

标签: java jax-rs multipartform-data openejb

我正在使用嵌入式openejb运行一组jax-rs服务的集成测试。其中一个需要接收二进制文件。请参阅以下方法:

@POST
@Path("signOff")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void signOffDeliveries(
    @Encoded @FormParam("signature") File signature) {

}

现在,此方法可以在Websphere上正常运行,但是对嵌入式openejb运行测试失败。 signature获取包含整个图像二进制数据的文件名(许多加扰符号)。

现在,正如预期的那样,阅读此文件会得到FileNotFoundException。我的问题是,这是嵌入式openejb中的一个错误,还是我以错误的方式设置我的测试?下面是测试代码btw:

@RunWith(ApplicationComposer.class)
public class DeliveryServiceIT {

    private HttpClient httpClient;
    private DeliveryServiceClient client;

    @Configuration
    public Properties config() throws Exception {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
        properties.setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "true");
        return properties;
    }

    @MockInjector
    public Class<?> mockitoInjector() {
        return MockitoInjector.class;
    }

    @Module
    public EjbModule createModule() throws Exception {
        EjbJar ejbJar = new EjbJar();
        ejbJar.addEnterpriseBean(new StatelessBean(DeliveryService.class));
        EjbModule module = new EjbModule(ejbJar);
        return module;
    }

    @Before
    public void setup() {
        //this is where I create the http client that makes the actual http request
    }

    @Test
    public void signOffDeliveries_givenSignatureImageAndDeliveries_expectsValidRequest() throws FileNotFoundException, IOException {
        File f = new File("/signature.png");

        client.signOffDeliveries(file);
        //the client will take the file, get the bytes, create 
        //multipart/form-data request and send the request to the 
        //service method posted above
    }

由于这是在我的websphere上运行,我认为它的openejb版本存在问题,我在(4.5.0)中运行我的集成测试,或者测试设置方式存在问题。

1 个答案:

答案 0 :(得分:0)

您希望您的客户端通过HTTP发送文件完整服务器路径?这正是发生的事情。在HTTP POST主体中编码的表单参数signature用作构造类File的对象的单个参数。但是找不到文件。

改为使用String并在JAX-RS资源中构建特定于服务器的文件路径。