2013-12-16 73 views
1

我試圖在GlassFish Server 4.0上運行的REST項目中進行文件上載工作。NetBeans Glassfish REST庫與Jersey庫衝突:ModelValidationException

GlassFish服務器(雖然我覺得很困惑)在javax.ws.rs庫裏面有它自己版本的澤西庫,到目前爲止工作得很好,但現在我需要在REST上使用MediaType.MULTIPART_FORM_DATA和FormDataContentDisposition服務器服務,並且無法在GlassFish中找到它們。

因此,我下載Jersey庫和添加

import com.sun.jersey.core.header.FormDataContentDisposition; 
import com.sun.jersey.multipart.FormDataParam; 

到庫,服務器端代碼

@ApplicationPath("webresources") 
@Path("/file") 
@Stateless 
public class FileResource 
{ 
    @POST 
    @Path("/upload") 
    @Consumes(MediaType.MULTIPART_FORM_DATA) 
    public Response uploadWeb(@FormDataParam("file") InputStream inputStream, 
      @FormDataParam("file") FormDataContentDisposition disposition) 
    { 
     int read = 0; 
     byte[] bytes = new byte[1024]; 
     try 
     { 
      while ((read = inputStream.read(bytes)) != -1) 
      { 
       System.out.write(bytes, 0, read); 
      } 
     } 
     catch (IOException ex) 
     { 
      Logger.getLogger(FileResource.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return Response.status(403).entity(inputStream).build(); 
    } 
} 

但是現在每當一個REST資源被調用(即使這在此前工作的人罰款)我得到的錯誤:

org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization. 

如何解決上述錯誤?如何將jersey.multipart支持添加到GlassFish服務器?

+0

這個答案可能會幫助:http://stackoverflow.com/questions/18252990/uploading-file-using -jersey環比REST類型的服務和最資源配置 – bruThaler

回答

1

確定找到了一個辦法解決通過使用僅使用GlassFish的可用庫下面的服務器端代碼:

@POST 
    @Path("/upload") 
    @Consumes(MediaType.MULTIPART_FORM_DATA) 
    public Response uploadWeb(InputStream inputStream) 
    { 
     DataInputStream dataInputStream = new DataInputStream(inputStream); 
     try 
     { 
      StringBuffer inputLine = new StringBuffer(); 
      String tmp; 
      while ((tmp = dataInputStream.readLine()) != null) 
      { 
       inputLine.append(tmp); 
       System.out.println(tmp); 
      } 
     } 
     catch (IOException ex) 
     { 
      Logger.getLogger(FileResource.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return Response.status(403).entity(dataInputStream).build(); 
    }