Cannot implement simple file upload in Jersey - quot;annotated with POST of resource, class is not recognized as valid resource method. unavailablequot;(无法在泽西岛实现简单的文件上传-“用资源的 POST 注释,类不被识别为有效的资源方法.不可用) - IT屋-程序员软件开发技术分享社
问题描述
无法使用 Jersey 实现简单的文件上传.应用程序引导时引发的缺少依赖项错误:
Unable to implement simple file upload using Jersey. Missing dependency errors raised at application bootstrap:
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 服务似乎存在问题?我已阅读文档并遵循了几个示例,我并没有偏离这些示例.
It seems like there is an issue with mapping the input parameters to a REST service? I have read documentation and followed several examples, and I am not deviating from those examples.
代码如下:
@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'".
I pulled in "compile 'com.sun.jersey.contribs:jersey-multipart:1.17.1'" for the FormDataParams.
编辑:我能够让它在泽西岛工作,但只能以这种更原始的方式:
EDIT: I was able to get it working in Jersey but only in this more primitive fashion:
@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();
}
}
这可能是一个足够的解决方法,但仍想深入了解问题的根源.
This is probably a sufficient workaround, but would still like to get to the bottom of the issue.
推荐答案
我也遇到了同样的问题.
I had the same problem.
这是一个版本问题(我在 jersey.multipart 中使用 1.8,在其余球衣中使用 1.17.1).将它们全部设置为 1.17.1 workrd for mee.
It was a version problem (I'd 1.8 in jersey.multipart and 1.17.1 in the rest of jersey). Setting all of them to 1.17.1 workrd for mee.
从这里得到我的答案:
方法时缺少依赖项做一个文件上传休息网络服务
这篇关于无法在泽西岛实现简单的文件上传-“用资源的 POST 注释,类不被识别为有效的资源方法.不可用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法在泽西岛实现简单的文件上传-“用资源的 POST 注释,类不被识别为有效的资源方法.不可用"
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01