Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA rest stops working when trying to upload blob

Tags:

java

jpa

My whole rest service stops working when I'm adding this code:

@PUT
@Path("upload/{id}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void addBlob(@PathParam("id") Integer id, @FormDataParam("file") InputStream uploadedInputStream) throws IOException {
    TheTempClient entityToMerge = find(id);
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        entityToMerge.setTestBlob(out.toByteArray());
        super.edit(entityToMerge);
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

It doesn't really say why either, all I'm getting is:

Severe:   WebModule[/MavenProjectTest]StandardWrapper.Throwable
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.

And a bunch of errors saying " caused by previous errors "

I must have done something really wrong here, are there any proffessional JPA enthusiasts that can help me out a little bit here?

Edit: I'm using annotations instead of web.xml, is it possible to do this without a web.xml?

like image 599
Dr Cox Avatar asked Oct 18 '22 17:10

Dr Cox


1 Answers

I had to add register(MultiPartFeature.class);

in the ApplicationConfig.java class, like this:

@javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends ResourceConfig {

public ApplicationConfig() {
    packages("com.test.thepackage.service");
    register(MultiPartFeature.class);
}

}

Now it works like a charm, without a web.xml file.

like image 150
Dr Cox Avatar answered Nov 15 '22 04:11

Dr Cox