泽西2多部分

时间:2016-11-11 06:13:27

标签: java rest jersey jetty jersey-2.0

我们正在使用RESTful Web服务,该服务具有接受多部分的资源。我们正在使用Spring4和Spring 4.我得到以下异常

<code>
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response xxx.AssetUploadResource.uploadAssetByStream(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition,java.lang.String) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class xxx.AssetUploadResource, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@1d63c523]}, definitionMethod=public javax.ws.rs.core.Response xxx.AssetUploadResource.uploadAssetByStream(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition,java.lang.String), parameters=[Parameter [type=class java.io.InputStream, source=Asset, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=asset, defaultValue=null], Parameter [type=class java.lang.String, source=Asset Metadata as a json string, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
</code>

泽西岛版本为2.24。此外,还添加了web.xml中的多部分依赖项和init-param

<code>
@Api
@Path("/asset")
@Component
public class AssetUploadResource {

    @POST
    @Consumes(MULTIPART_FORM_DATA)
    @Produces(APPLICATION_JSON)
    public Response uploadAssetByStream(
            @FormDataParam("asset") InputStream asset,
            @FormDataParam("asset") FormDataContentDisposition assetDetails,
            @FormDataParam("metadata") String metadata) {
</code>

的web.xml

<code>

 <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>xxx</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
</code>

你能告诉我我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

我正在使用带有Jersey的Spring,似乎spring-boot-starter-jersey依赖项不包含多部分表单数据媒体类型的必要依赖项,这是一个问题。

添加依赖项:

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.26</version>
    </dependency>

之后你还需要注册它。我是这样做的:

@Configuration
public class JerseyConfig extends ResourceConfig {


    JerseyConfig() {
        // where I keep all my things
        packages(
                "com.mycompany.api"                
        );

        // this is what you are looking for
        register(org.glassfish.jersey.media.multipart.MultiPartFeature.class);
    }
}