Java 使用 Jersey 通过 RESTfull 服务上传文件并且资源配置不可修改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18252990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uploading file using Jersey over RESTfull service and The resource configuration is not modifiable?
提问by Sami
@Path("file.upload")
public class UploadFileService {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
System.out.println("-----------------UploadFileService------------------1.");
// Should we use a disk or DB? Decided to use DISK
// Path should be read from properties-files
String uploadedFileLocation = "//uploaded/" + fileDetail.getFileName();
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
// All went OK
return Response.status(200).entity(output).build();
}
WARNING: No injection source found for a parameter of type public javax.ws.rs.core.Response com.insame.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.
SEVERE: WebModule[/insame]StandardWrapper.Throwable
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 com.insame.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=MethodHandler{handlerClass=class com.insame.service.UploadFileService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@47bee27a]}, handlingMethod=public javax.ws.rs.core.Response com.insame.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class com.sun.jersey.core.header.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:410)
at org.glassfish.jersey.server.ApplicationHandler.access0(ApplicationHandler.java:157)
at org.glassfish.jersey.server.ApplicationHandler.run(ApplicationHandler.java:280)
at org.glassfish.jersey.internal.Errors.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors.call(Errors.java:286)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:277)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:262)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:167)
I implemented test service like this under the UploadFileService
:
我在下面实现了这样的测试服务UploadFileService
:
@GET
@Path("count")
@Produces("text/plain")
public String countREST() {
return "1 one 1";
}
and I got this exception to log:
我得到了这个异常来记录:
FINE: [Web-Security] hasResource perm: ("javax.security.jacc.WebResourcePermission"
"/webresources/file.upload/count" "GET")
SEVERE: WebModule[/insame]StandardWrapper.Throwable
java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:257)
WARNING: StandardWrapperValve[com.insame.service.ApplicationConfig]: Allocate exception for servlet com.insame.service.ApplicationConfig
java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:257)
at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:205)
at org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:435)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:261)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:167)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:349)
Environment
环境
- Netbeans7.3.1
- Glassfish 4.0
- Jersey 2 with Glassfish 4.0
- Netbeans7.3.1
- 玻璃鱼 4.0
- 泽西岛 2 与 Glassfish 4.0
采纳答案by Michal Gajdos
In order to use multipart in your Jersey application you need to register MultiPartFeaturein your application, i.e.:
为了在您的 Jersey 应用程序中使用 multipart,您需要在您的应用程序中注册MultiPartFeature,即:
public class ApplicationConfig extends Application {
public Set<Class<?>> getClasses() {
final Set<Class<?>> resources = new HashSet<Class<?>>();
// Add your resources.
resources.add(UploadFileService.class);
// Add additional features such as support for Multipart.
resources.add(MultiPartFeature.class);
return resources;
}
}
For more information see Multipartsection in the Jersey Users Guide.
有关更多信息,请参阅Jersey 用户指南中的Multipart部分。
For the second issue you're facing try to restart the GlassFish server, I am not sure how NetBeans are reloading the Jersey app after a change (if this doesn't help, please post your ApplicationConfig
).
对于您面临的第二个问题,尝试重新启动 GlassFish 服务器,我不确定 NetBeans 在更改后如何重新加载 Jersey 应用程序(如果这没有帮助,请发布您的ApplicationConfig
)。
回答by Ed Pike
I had the same problem and wanted to avoid creating a custom application class. It is not well documented, but if you want to add Multipart functionality, all you have to do is add this to your web.xml jersey servlet config:
我遇到了同样的问题,想避免创建自定义应用程序类。它没有很好的文档记录,但是如果您想添加 Multipart 功能,您所要做的就是将其添加到您的 web.xml jersey servlet 配置中:
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
I also added a loggingfilter.
我还添加了一个日志过滤器。
回答by Rauan Argyn
Just minor clarification
只是轻微的澄清
Use
用
import org.glassfish.jersey.media.multipart.MultiPartFeature
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
Not
不是
com.sun.jersey.*
Did work for me only when used org.glassfish.jersey.media.multipart.*
只有在使用时才对我有用 org.glassfish.jersey.media.multipart.*
In ApplicationConfig just register MultiPartFeature as
在 ApplicationConfig 中,只需将 MultiPartFeature 注册为
import org.glassfish.jersey.media.multipart.MultiPartFeature;
@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
resources.add(UploadFileService.class);
resources.add(MultiPartFeature.class);
return resources;
}
}
回答by Loyola A
If you are using jetty server and jersey servlet, then you can solve this problem by adding the following code in your main class where you have started the jetty server,
如果您使用的是 jetty 服务器和 jersey servlet,那么您可以通过在启动 jetty 服务器的主类中添加以下代码来解决此问题,
ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/*"); jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/classes to load. jerseyServlet .setInitParameter( "jersey.config.server.provider.classnames", <Your entry point class's canonical name> + ";org.glassfish.jersey.media.multipart.MultiPartFeature");
ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/*"); jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/classes to load. jerseyServlet .setInitParameter( "jersey.config.server.provider.classnames", <Your entry point class's canonical name> + ";org.glassfish.jersey.media.multipart.MultiPartFeature");
回答by Yergalem
You can use @FormDataParam("file") equivalent of FormDataMultiPart if you want it using annotation.
如果您希望使用注释,您可以使用等效于 FormDataMultiPart 的 @FormDataParam("file") 。
Used as given below sample code extract:
使用如下示例代码摘录:
public Response uploadFile( **@FormDataParam("file")** InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
回答by Yergalem
I'm using Jersey 1.9.1. org.glassfish...... works well with Jersey 2. For Jersey 1 you better use com.sun... classes.
我正在使用泽西岛 1.9.1。org.glassfish……与 Jersey 2 配合良好。对于 Jersey 1,您最好使用 com.sun... 类。