无法使用Jersey实现简单的文件上传.缺少应用程序引导时引发的依赖性错误:
The following errors and warnings have been detected with resource and/or provider classes: SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.foo.MyResource.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at parameter at index 0 SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.foo.MyResource.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at parameter at index 1 SEVERE: Method,public javax.ws.rs.core.Response com.foo.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition),annotated with POST of resource,class com.foo.FS2Resource,is not recognized as valid resource method. unavailable com.sun.jersey.spi.inject.Errors$ErrorMessagesException at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
将输入参数映射到REST服务似乎存在问题?我已阅读文档并遵循了几个示例,我并没有偏离这些示例.
这是代码:
@Path("v1/") public class FileUploadResource { @POST @Path("upload") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces({MediaType.APPLICATION_JSON}) public Response uploadFile( @FormDataParam("file") InputStream is,@FormDataParam("file") FormDataContentDisposition detail) { String name = detail.getFileName(); // do upload stuff String output = .... return Response.status(200).entity(output).build(); } }
我为FormDataParams引入了“compile’com.sun.jersey.contribs:jersey-multipart:1.17.1’”.
编辑:我能够让它在泽西岛工作,但只有这种更原始的方式:
@POST @Path("upload") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.TEXT_PLAIN) public Response uploadFile(final MimeMultipart file) { if (file == null) { return Response.status(Response.Status.BAD_REQUEST).entity("Must supply a valid file").build(); try { for (int i = 0; i < file.getCount(); i++) { // ... do something with file.getBodyPart(i)); } return Response.ok("done").build(); } catch (final Exception e) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e).build(); } }
这可能是一个充分的解决方法,但仍然希望找到问题的根源.
解决方法
@H_403_21@ 我有同样的问题.这是一个版本问题(在jersey.multipart中我是1.8,在球衣的其余部分是1.17.1).将所有这些设置为1.17.1 workrd for mee.
从这里得到我的答案:
Missing dependency for method when doing a file upload rest web service