Jersey Multipart 415不支持的MediaType

时间:2016-02-23 22:45:10

标签: java rest mime-types multipartform-data jersey-2.0

我必须从Jersey客户端发送多部分消息。这些是主要方法

public String save(Asset asset) throws IllegalAccessException {
        UriBuilder uriBuilder = UriBuilder
                 .fromUri("https://localhost/my/path")
                 .path("/save");
    return postEntity(uriBuilder.build(), asset);
}

protected String postEntity(URI uri, Asset asset) throws IllegalAccessException {

    FormDataMultiPart formDataMultiPart = createFormData(asset); 
    formDataMultiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);

    ClientConfig cc = new DefaultClientConfig();
    cc.getClasses().add(MultiPartWriter.class);
    WebResource webResource = Client.create(cc).resource(uri);

    ClientResponse clientResponse = webResource
            .type(MediaType.MULTIPART_FORM_DATA_TYPE)
            .header("Accept", MediaType.APPLICATION_JSON)
            .header("Content-Type", MediaType.MULTIPART_FORM_DATA_TYPE)
            .header("Authorization", "Bearer ....")             
            .post(ClientResponse.class, formDataMultiPart);

    return clientResponse.getEntity(String.class);
}   

public FormDataMultiPart createFormData(Asset asset) throws IllegalAccessException {
    FormDataMultiPart formData = new FormDataMultiPart();
    for (Field field : asset.getClass().getDeclaredFields()) {
        field.setAccessible(true);
        Object object = field.get(asset);

        if (object instanceof InputStream) {
            FormDataContentDisposition formDataContentDisposition = FormDataContentDisposition.name("file").fileName(asset.getFileName()).build();
            formData.bodyPart(new FormDataBodyPart(formDataContentDisposition, asset.getFile(), MediaType.APPLICATION_OCTET_STREAM_TYPE));
        } else {
            formData.field(field.getName(), object.toString());
        }
    }
    return formData;
}

在服务器端,REST服务是:

public interface IDamRestService
{
    @POST
    @Path("/save")
    @Consumes({MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Asset saveAsset(FormDataMultiPart multipart);
}

这是我的ResourceConfig扩展类:

public class CoreServiceResourceConfig extends ResourceConfig {
        public CoreServiceResourceConfig() {       
            super( MultiPartFeature.class);
        }
}

此代码段来自我的web.xml:

<servlet>
    <servlet-name>Secured REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>         
        <param-value>my.services.package</param-value>
    </init-param>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>            
        <param-value>CoreServiceResourceConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Secured REST Service</servlet-name>
    <url-pattern>/secured/*</url-pattern>
</servlet-mapping>

当我在其余客户端吃午餐时,我从服务器收到错误,状态代码为:415不支持媒体类型。

我缺少什么?

0 个答案:

没有答案