0

我正在使用 RESTEasy 和 WildFly 9.0.2 开发一组 REST 服务。

其中一个允许我上传文件。

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(MultipartFormDataInput input);

我成功地调用了 REST 服务并将文件上传到服务器,所以我开始实现集成测试。

为此,我使用了嵌入式 Grizzly HTTP 服务器:

@Before
public void setUp() throws Exception {
    // Weld and container initialization
    weld = new Weld();
    weld.initialize();
    ResourceConfig resourceConfig = new ResourceConfig().packages("...");
    server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), resourceConfig);
}

当我尝试测试新创建的服务时出现了问题:

@Test
public void hubbleMapServiceUploadMapTest() throws FileNotFoundException, IOException {
    Client client = null;
    WebTarget webTarget = null;
    Builder builder = null;
    Response response = null;

    try {
        client = ClientBuilder.newBuilder().build();

        webTarget = client.target(BASE_URI).path("/service/upload");
        builder = webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.WILDCARD);

        response = builder.post(Entity.entity(getEntityAsMultipart(), MediaType.MULTIPART_FORM_DATA));

    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (response != null) {
            response.close();
        }

        if (client != null) {
            client.close();
        }
    }

    assertNotNull(response);
    assertThat(response.getStatus(), is(Status.OK.getStatusCode()));
}

private GenericEntity<MultipartFormDataOutput> getEntityAsMultipart() {
    String resource = "my.file";
    MultipartFormDataOutput mfdo = new MultipartFormDataOutput();

    try {
        mfdo.addFormData("file",
                new FileInputStream(new File(getClass().getClassLoader().getResource(resource).getFile())),
                MediaType.APPLICATION_OCTET_STREAM_TYPE, resource);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(mfdo) {
    };

    return entity;
}

响应代码始终为415 Unsupported Media Type

所有剩余的集成测试都可以正常工作。

我究竟做错了什么?我是否需要以某种方式启用 Grizzly 的Multipart功能?

4

1 回答 1

1

在遵循@peeskillet的建议后,我切换到具有 CDI 意识的Sun JDK HTTP Server嵌入式服务器。

为此,需要执行以下操作:

  1. 添加 RESTEasy 和 WELD 必要的 Maven 依赖项:

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jdk-http</artifactId>
        <version>3.0.17.Final</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-cdi</artifactId>
        <version>3.0.17.Final</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.jboss.weld.se</groupId>
        <artifactId>weld-se</artifactId>
        <version>2.3.4.Final</version>
        <scope>test</scope>
    </dependency>
    
  2. 配置 WELD 和 RESTEasy CDI 扩展以用于 Sun JDK HTTP Server 的部署配置:

    private Weld weld;
    private SunHttpJaxrsServer server;
    
    @Before
    public void setUp() throws Exception {
        // Weld initialization
        weld = new Weld();
        weld.initialize();
    
        // RESTEasy CDI extension configuration
        ResteasyCdiExtension cdiExtension = CDI.current().select(ResteasyCdiExtension.class).get();
        ResteasyDeployment deployment = new ResteasyDeployment();
        deployment.setActualResourceClasses(cdiExtension.getResources());
        deployment.setInjectorFactoryClass(CdiInjectorFactory.class.getName());
        deployment.getActualProviderClasses().addAll(cdiExtension.getProviders());
    
        // Container initialization
        server = new SunHttpJaxrsServer();
        server.setDeployment(deployment);
        server.setPort(8787);
        server.setRootResourcePath("/some-context-root/rest");
        server.getDeployment().getActualResourceClasses().add(MyService.class);
        server.start();
    }
    

为了了解如何配置 RESTEasy 的 CDI 扩展,我依赖于@John AmentNettyTest示例。

于 2016-06-09T10:12:43.917 回答