dendini
dendini

Reputation: 3952

NetBeans Glassfish REST library conflicting with Jersey library: ModelValidationException

I'm trying to make a file upload work inside a REST project running on GlassFish Server 4.0.

The GlassFish server (although I find it confusing) has its own version of the Jersey library inside javax.ws.rs library which so far has worked fine, now however I need to use MediaType.MULTIPART_FORM_DATA and FormDataContentDisposition on the REST server service and can't find them inside GlassFish.

I therefore downloaded Jersey libraries and added

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

to the libraries, the server side code is

@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();
    }
}

Now however whenever a REST resource is called (even the ones that previously were working fine) I get the error:

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

How do I fix the above error? How do I add jersey.multipart support to the GlassFish server?

Upvotes: 1

Views: 1636

Answers (1)

dendini
dendini

Reputation: 3952

Ok found a way around by using following server side code which uses only GlassFish available libraries:

@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();
    }

Upvotes: 1

Related Questions