Java 将资源更改为使用多部分表单数据后的Spring问题
我最近更改了以下jersey资源,添加了以下@Consumes注释以接受图像文件Java 将资源更改为使用多部分表单数据后的Spring问题,java,spring,spring-boot,jersey,jax-rs,Java,Spring,Spring Boot,Jersey,Jax Rs,我最近更改了以下jersey资源,添加了以下@Consumes注释以接受图像文件 @POST @PermitAll @Consumes(MediaType.MULTIPART_FORM_DATA) public Response addImage(@FormDataParam("file") InputStream uploadedInputStream) { imageService.addImage(uploadedInputStream);
@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");
问题实际上是我缺乏配置运动衫测试的经验。我必须使用
.register(MultiPartFeature.class)配置JerseyTest时在我的ResourceConfig上的代码>。啊 只是好奇,您使用自己的XxxRegistrationBean
而不只是使用Spring Boot提供的自动配置,这有什么具体原因吗?@peeskillet我也注册了一些其他东西,这就是我的教学方式。但这一切都可以在带有@组件
。您可以从Spring Boot中获得自动配置的好处。您能否显示完整的注册以及与此相关的任何其他配置Jersey@peeskillet我正在为DataSource和DataSourceTransactionManager使用Spring引导自动配置。顺便说一句,如果你看上面,我添加了另一个注册参数EAH,我不知道。当您没有配置MutliPartFeature
时,通常会看到该错误,但看起来您已经配置了。我只是想也许你正在使用的注册甚至不是真正正在使用的注册。这是我唯一能想到的。这就是我问的原因