将资源更改为使用MULTIPART_FORM_DATA后出现Spring问题

时间:2017-03-30 20:06:25

标签: java spring spring-boot jersey jax-rs

我最近通过添加以下@Consumes注释来更改以下球衣资源以接受图像文件。

    @POST
    @PermitAll
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response addImage(@FormDataParam("file") InputStream uploadedInputStream) {
        imageService.addImage(uploadedInputStream);
        return Response.ok(200).build();
    }

我还在应用程序初始化

中添加了以下参数
registration.addInitParameter("jersey.config.server.provider.classnames",
                "org.glassfish.jersey.media.multipart.MultiPartFeature");

现在有几个随机单元测试因以下原因而失败:

    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 package.webservice.resource.ImageResource.addImage(java.io.InputStream) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class package.webservice.resource.ImageResource, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@5a515e5d]}, definitionMethod=public javax.ws.rs.core.Response package.webservice.resource.ImageResource.addImage(java.io.InputStream), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']

有什么我想念的吗?

编辑:添加完整注册:

registration.setFilter(filter);
    registration.setName("JerseyFilter");
    registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
    // Set the Jersey filter mapping and context path
    registration.addUrlPatterns("/*");
    registration.addInitParameter("jersey.config.servlet.filter.contextPath", "/");
    // Load the common package and application package
    registration.addInitParameter("jersey.config.server.provider.packages",
            "com.vanguard.jaxrs.feature;package.webservice.resource");
    // Enable javax security annotations on resources
    registration.addInitParameter("jersey.config.server.provider.classnames",
            "org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;org.glassfish.jersey.media.multipart.MultiPartFeature");
    // Enable media type mappings on the URI such as .xml and .json
    registration.addInitParameter("jersey.config.server.mediaTypeMappings",
            "xml:application/xml, json:application/json");
    // Enable Java bean validation integration
    registration.addInitParameter("jersey.config.beanValidation.enableOutputValidationErrorEntity.servers", "true");
    // Enable fall through to Spring MVC (allows forward to static content
    // if no jersey endpoint found)
    registration.addInitParameter("jersey.config.servlet.filter.forwardOn404", "true");

1 个答案:

答案 0 :(得分:0)

问题实际上是由于我缺乏配置JerseyTest的经验。配置JerseyTest时,我必须在ResourceConfig上使用.register(MultiPartFeature.class);。卫生署!