2013-08-15 17 views
33
@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=[[email protected]]}, 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) 

私はUploadFileServiceの下で、このようなテストサービスを実装:Jersey over RESTfullサービスを使用してファイルをアップロードしていて、リソース設定が変更できません。

    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) 
    

    環境:

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

    をし、私がログインするために、この例外が発生しましたアプリケーションでMultiPartFeatureを登録する必要があり、あなたのジャージーのアプリケーションでマルチパートを使用するためには

  • Netbeans7.3.1
  • Glassfishの4.0
+0

「ResourceConfig」ソリューションについては、http://stackoverflow.com/a/43926819/548473を参照してください。 – GKislin

答えて

45

とGlassfishの4.0

  • ジャージー2、すなわち:

    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; 
        } 
    } 
    

    詳細については、JerseyユーザーガイドのMultipartセクションを参照してください。

    GlassFishサーバーを再起動しようとすると、NetBeansが変更後にJerseyアプリケーションをどのように読み込んでいるのかわかりません(これが役に立たない場合はApplicationConfigを投稿してください)。

  • +0

    ありがとうございます!エラーメッセージが変更され、pom.xmlに1つのエラーが見つかりました。私はpom.xmlに古いmultipartバージョンを使用していました。 WELD-001408 [BackedAnnotatedConstructor]のインジェクションポイント[[BackedAnnotatedParameter]パラメーター1 @Inject public org.glassfish.jersey.media.multipart.internal.MultiPartReaderServerSide(@Context Providers、@Context Providers)で修飾子[@Default]を持つ[Providers]タイプの依存関係が満たされていません。プロバイダ)] – Sami

    +0

    これは、あなたの '.war'にあるいくつかのJersey 2.xビットを持つことと関連しているかもしれません(同時に彼らはGFクラスパス上にあります)。提供されたpom.xmlに 'jersey-media-multipart'アーティファクトをマークできますか?すでにGF 4.0の一部です。 –

    +0

    それはそれだった!今それは働いている! org.glassfish.jersey.media ジャージーメディア・マルチ 2.2 は、私はMavenを使っても同じ構成を有しているが、GF4ジャージクラスが見つからないわけではない Sami

    31

    私は同じ問題を抱えており、カスタムアプリケーションクラスの作成を避けたいと考えていました。それは十分に文書ではありませんが、マルチパートの機能を追加したい場合は、あなたがしなければならないすべてはあなたのweb.xmlジャージサーブレットの設定にこれを追加することです:

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

    私もloggingfilterを追加しました。

    +0

    あなたの解決策は最高です! –

    +1

    私はこれを動作させることはできません。それは特定の順序である必要がありますか?万が一ポンポンの要点はありますか? – streetlight

    +0

    これは最も簡単なアプローチであり、魅力のように機能します!強くお勧めします! – Ozil

    0

    だけマイナー

    com.sun.jersey.* 
    

    がApplicationConfigでorg.glassfish.jersey.media.multipart.*

    を私のための作業に使用しなかっただけで

    import org.glassfish.jersey.media.multipart.MultiPartFeature 
    import org.glassfish.jersey.media.multipart.FormDataContentDisposition; 
    import org.glassfish.jersey.media.multipart.FormDataParam; 
    

    明確化

    使用だけ

    として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; 
        } 
    } 
    
    +0

    Hlleoみんな、wildflyサーバーを使用しています。私のApplicationConfigでimport org.glassfish.jersey.media.multipart。*を使用することはできますか?はいの場合は、pom.xmlに追加する必要がある依存性を教えてください。私のプロジェクトでこのエラーが発生しました:javax.ws.rs.NotSupportedException:タイプのメッセージ本文リーダーを見つけることができませんでした:クラスcom.sun.jersey.core.header.FormDataContentDispositionコンテンツタイプ:multipart/form-data;進歩した – James

    2

    あなたは桟橋サーバとジャージのサーブレットを使用している場合、あなたは桟橋サーバーを開始しているあなたのメインクラスに次のコード、

    ServletHolder jerseyServlet =コンテキストを追加することで、この問題を解決することができます。 addServlet( org.glassfish.jersey.servlet.ServletContainer.class、 "/ *");ジャージサークル。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"); 
    
    -1

    あなたはそれが注釈を使用したい場合は、FormDataMultiPartの同等@FormDataParam( "ファイル")を使用することができます。サンプルコードエキス以下に示すように使用し

    public Response uploadFile(**@FormDataParam("file")** InputStream fileInputStream, 
          @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) { 
    
    0

    私はジャージー1.9.1を使用しています。 org.glassfish ...... Jersey 2でうまくいきます.Jersey 1では、com.sun ...クラスを使うのが良いでしょう。

    +1

    回答に2つの投稿を追加する代わりに、最初に質問に回答する投稿([この投稿](http://stackoverflow.com/a/42960464/3519000))を編集することをおすすめします。ありがとう。 – lrnzcig

    関連する問題