【发布时间】:2017-03-30 20:06:24
【问题描述】:
我最近更改了以下球衣资源,添加了以下@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");
【问题讨论】:
-
只是好奇,您使用自己的
XxxRegistrationBean而不是仅仅使用 Spring Boot 提供的自动配置是否有特定原因? -
@peeskillet 我也在注册一堆其他的东西,这正是我被教导的方式。
-
但这一切都可以在
ResourceConfig子类中使用@Component进行注释。您可以从 Spring Boot 中获得自动配置的好处。你能显示完整的注册,以及与泽西岛相关的任何其他配置 -
@peeskillet 我正在使用 Spring Boot 自动配置 DataSource 和 DataSourceTransactionManager 顺便说一句。如果你看上面我添加了其他注册参数
-
是的,我不知道。当您没有配置
MutliPartFeature时,您通常会看到该错误,但看起来您已经配置了。我只是想也许您正在使用的注册甚至不是真正使用的注册。这是我唯一能想到的。这就是我问的原因
标签: java spring spring-boot jersey jax-rs