orange14
orange14

Reputation: 381

jersey, spring boot, upload image Unsupported Media Type

I am using jersey (JAX-RS) with spring-boot with jersey version 2.25.1 and I am trying to make a POST request in which I upload 1 file.

I have checked every link on stackoverflow and almost all of them tell me to register the MultipartFeature and to add init-params. I have made all the changes but I still get same errors:

@Component
@ApplicationPath("/secure")
    public class JerseyInitialization extends ResourceConfig {
          public JerseyInitialization() {
                this.register(new JacksonJsonProvider(ObjectMapperFactory.create()));
                this.register(MultiPartFeature.class);
                this.property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
                this.property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true);
                this.packages(true, "com.jersey.resources");
            }
    }

My Post request to upload images is:

@Path("/images")
@Consumes(MediaType.APPLICATION_JSON)
@Component
@Transactional
public class ImageResource {

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Image uploadFile(@Valid Image image,
                             @FormDataParam("file") InputStream uploadedInputStream,
                             @FormDataParam("file") FormDataContentDisposition fileDetail) throws IOException {

            image.setData(IOUtils.toByteArray(uploadedInputStream));
            image.setFilename(fileDetail.getFileName());

            System.out.println("File uploaded Successfully");

            return imageDao.save(image);
    }

}

My Application class is as follows:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class).run(args);

    }

    @Bean
    public ServletRegistrationBean jerseyServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/resources");
        registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyInitialization.class.getName());
        registration.addInitParameter("javax.ws.rs.Application", "com.verico.multipart.app.MultiPartApp");
        return registration;
    }
}

when I run curl with following command:

curl -X POST -H "Authorization: Bearer db4719e2-dd76-4977-9dab-e44670213c63" -F "[email protected]" -F "name=kreditech.jpg" localhost:8080/api/secure/images/upload

I get error as follows:

"status":415,"error":"Unsupported Media Type","message":"Unsupported Media Type"

Upvotes: 0

Views: 1012

Answers (2)

orange14
orange14

Reputation: 381

I was putting in wrong curl command

Correct Curl command:

curl -H "Authorization: Bearer db4719e2-dd76-4977-9dab-e44670213c63" -F "[email protected]" localhost:8080/api/secure/images/upload

Upvotes: 0

Anshul Sharma
Anshul Sharma

Reputation: 3522

remove @Consumes(MediaType.APPLICATION_JSON) before class and refer bellow code for upload image.

@Path("/upload")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@DefaultValue("") @FormDataParam("tags") String tags, 
            @FormDataParam("file") InputStream file,
            @FormDataParam("file") FormDataContentDisposition fileDisposition) {

    String fileName = fileDisposition.getFileName();

    saveFile(file, fileName);

    String fileDetails = "File saved at /Volumes/Drive2/temp/file/" + fileName + " with tags "+ tags;

    System.out.println(fileDetails);

    return Response.ok(fileDetails).build();
}

private void saveFile(InputStream file, String name) {
    try {
        /* Change directory path */
        java.nio.file.Path path = FileSystems.getDefault().getPath("/Volumes/Drive2/temp/file/" + name); 
        /* Save InputStream as file */
        Files.copy(file, path);
    } catch (IOException ie) {
        ie.printStackTrace();
    }
}

Upvotes: 1

Related Questions