2013-12-16 68 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(); 
    }