本文介绍了使用Jersey在REST满服务上传文件和资源配置是不可修改的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


@POST $ b $ @Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam(file)InputStream uploadedInputStream,
@FormDataParam(file)FormDataContentDisposition fileDetail){

System.out.println( - --------------- UploadFileService ------------------ 1)。
//我们应该使用磁盘还是数据库?决定使用DISK
//路径应该从属性文件读取
String uploadedFileLocation =// uploaded /+ fileDetail.getFileName();

//保存
writeToFile(uploadedInputStream,uploadedFileLocation);

字符串输出=文件上传到:+ uploadedFileLocation;
//一切就OK
返回Response.status(200).entity(输出).build();

$ b



 警告:找不到类型为public javax.ws.rs.core.Response的参数的注入源com.insame.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header .FormDataContentDisposition)在索引0. 

SEVERE:WebModule [/ insame] StandardWrapper.Throwable
org.glassfish.jersey.server.model.ModelValidationException:应用程序资源模型的验证失败应用初始化。
[[FATAL]没有找到注入源的参数类型为public javax.ws.rs.core.Response com.insame.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core .header.FormDataContentDisposition)在索引0。 source ='ResourceMethod {httpMethod = POST,consumeTypes = [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),参数= [参数[type = class java.io.InputStream,source = file,defaultValue = null],参数[type = class com.sun.jersey.core .header.FormDataContentDisposition,源=文件,默认值=空],的responseType =类javax.ws.rs.core.Response},nameBindings = []}'] $ b $在org.glassfish.jersey.server.ApplicationHandler b .initialize(ApplicationHandler.java:410)
在org.glassfish.jersey.server.ApplicationHandler.access $ 500(ApplicationHandler.java:157)
在o rg.glassfish.jersey.server.ApplicationHandler $ 3.run(ApplicationHandler.java:280)
在org.glassfish.jersey.internal.Errors $ 2.call(Errors.java:289)$ B $在组织湾glassfish.jersey.internal.Errors $ 2.call(Errors.java:286)
在org.glassfish.jersey.internal.Errors.process(Errors.java:315)
在org.glassfish.jersey .internal.Errors.process(Errors.java:297)
在org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
在org.glassfish.jersey.server.ApplicationHandler < INIT>(ApplicationHandler.java:277)
。在org.glassfish.jersey.servlet.WebComponent<初始化>(WebComponent.java:262)
。在org.glassfish.jersey.servlet .ServletContainer.init(ServletContainer.java:167)

我在 UploadFileService

  @GET 
@Path(count)
@Produces(text / plain)
public String countREST(){
return1 one 1;
}

我得到这个例外log:

  FINE:[Web-Security] hasResource perm:(javax.security.jacc.WebResourcePermission

/ webresources / file。上传/计数GET)
SEVERE:WebModule [/ insame] StandardWrapper.Throwable
java.lang.IllegalStateException:资源配置在此上下文中不可修改。
在org.glassfish.jersey.server.ResourceConfig $ ImmutableState.register(ResourceConfig.java:257)
警告:StandardWrapperValve [com.insame.service.ApplicationConfig]:为servlet com.insame分配异常。 service.ApplicationConfig
java.lang.IllegalStateException:资源配置在此上下文中不可修改。
在org.glassfish.jersey.server.ResourceConfig $ ImmutableState.register(ResourceConfig.java:257)
在org.glassfish.jersey.server.ResourceConfig $ ImmutableState.register(ResourceConfig.java:205)在org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:435)

。在org.glassfish.jersey.servlet.WebComponent<初始化>(WebComponent.java:261)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:167)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:349)

环境


  • Netbeans7.3.1

  • Glassfish 4.0

  • 使用Glassfish 4.0的Jersey 2

解决方案

为了在Jersey应用程序中使用multipart,您需要注册在您的应用程序中,即:

  public class ApplicationConfig extends Application {

public Set< Class<?>> getClasses(){
final Set< Class<>> resources = new HashSet< Class<>>();

//添加您的资源。
resources.add(UploadFileService.class);

//添加其他功能,如对Multipart的支持。
resources.add(MultiPartFeature.class);

返回资源;




$ b $ p
$ b

欲了解更多信息,请参阅 Jersey用户指南中的Multipart部分。



ApplicationConfig
)。


@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.access$500(ApplicationHandler.java:157)
    at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:280)
    at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
    at org.glassfish.jersey.internal.Errors$2.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:

@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

解决方案

In order to use multipart in your Jersey application you need to register MultiPartFeature in your application, i.e.:

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 Multipart section in the Jersey Users Guide.

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).

这篇关于使用Jersey在REST满服务上传文件和资源配置是不可修改的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 08:20