0

I have a post operation which takes multipartfile as request, i would like to store it on the FTP later.

But for now I have just the method setup and when trying to invoke from postman, it gives me an error response HTTP 415.

This is what I have:

CONTROLLER:

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.stereotype.Component;


@Path("/")
@Produces("application/json;charset=UTF-8")
@Component
public class GUIController {

    @Path("/upload")
    @POST
    @Consumes(MediaType.WILDCARD)
    public Response uploadPicture(@RequestParam("file") MultipartFile input) {
        return Response.ok().build();
    }

}

My postman request and response:

enter image description here

Can yo please help me fix this ? Thanks

UPDATE:

After a few changes based in the comments this is what i have:

Controller:

 import java.io.InputStream;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.springframework.stereotype.Component;

@Path("/media")
@Produces(MediaType.APPLICATION_JSON)
@Component
public class FileUploadController {

    @Path("/upload")
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadPicture(
            @FormDataParam("file") InputStream file,
            @FormDataParam("file") FormDataContentDisposition filFormDataContentDisposition) {
        return Response.ok().build();
    }
    }


public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
    register(MultiPartFeature.class);
    register(FileUploadController.class);
}

}

@Bean
public ServletRegistrationBean uploadServlet() {
    ServletContainer servletContainer = new ServletContainer(new JerseyConfig());
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servletContainer, "/media/*");
    servletRegistrationBean.setName("uploadServlet");
    return servletRegistrationBean;
}

I get the following error on invoking the upload method:

01:40:57.970 [http-nio-8030-exec-2] ERROR o.a.c.c.C.[.[.[/].[rest-api-v2] - Allocate exception for servlet rest-api-v2
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response com.example.tsi.channel.endpoint.v1.impl.FileUploadController.uploadPicture(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.example.tsi.channel.endpoint.v1.impl.FileUploadController, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@50286f19]}, definitionMethod=public javax.ws.rs.core.Response com.example.tsi.channel.endpoint.v1.impl.FileUploadController.uploadPicture(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:555) ~[jersey-server-2.23.2.jar:na]

Dependency tree:

|    |    |    \--- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.4.
5 -> 2.8.4
|    |    |         +--- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:2.8.4
|    |    |         |    +--- com.fasterxml.jackson.core:jackson-core:2.8.4
|    |    |         |    \--- com.fasterxml.jackson.core:jackson-databind:2.8.4
(*)
|    |    |         +--- com.fasterxml.jackson.core:jackson-core:2.8.4
|    |    |         +--- com.fasterxml.jackson.core:jackson-databind:2.8.4 (*)
|    |    |         \--- com.fasterxml.jackson.module:jackson-module-jaxb-annota
tions:2.8.4 (*)
|    |    +--- org.glassfish.jersey.containers:jersey-container-servlet-core:2.1
 -> 2.23.2
|    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2
|    |    |    |    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    |    |    +--- javax.annotation:javax.annotation-api:1.2
|    |    |    |    +--- org.glassfish.jersey.bundles.repackaged:jersey-guava:2.
23.2
|    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2:hk2-utils:2.5.0-b05
|    |    |    |    |    \--- org.glassfish.hk2.external:aopalliance-repackaged:
2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2:hk2-locator:2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2.external:aopalliance-repackaged:
2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    |    +--- org.glassfish.hk2:hk2-utils:2.5.0-b05
|    |    |    |    |    \--- org.javassist:javassist:3.20.0-GA
|    |    |    |    \--- org.glassfish.hk2:osgi-resource-locator:1.0.1
|    |    |    +--- org.glassfish.jersey.core:jersey-server:2.23.2
|    |    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)
|    |    |    |    +--- org.glassfish.jersey.core:jersey-client:2.23.2
|    |    |    |    |    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    |    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)

|    |    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    |    |    \--- org.glassfish.hk2:hk2-locator:2.5.0-b05 (*)
|    |    |    |    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    |    |    +--- org.glassfish.jersey.media:jersey-media-jaxb:2.23.2
|    |    |    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)

|    |    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2:hk2-locator:2.5.0-b05 (*)
|    |    |    |    |    \--- org.glassfish.hk2:osgi-resource-locator:1.0.1
|    |    |    |    +--- javax.annotation:javax.annotation-api:1.2
|    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2:hk2-locator:2.5.0-b05 (*)
|    |    |    |    \--- javax.validation:validation-api:1.1.0.Final
|    |    |    \--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    \--- org.glassfish.jersey.media:jersey-media-multipart:2.1 -> 2.22.2
|    |         +--- org.glassfish.jersey.core:jersey-common:2.22.2 -> 2.23.2 (*)

|    |         \--- org.jvnet.mimepull:mimepull:1.9.6
|    +--- org.springframework.boot:spring-boot-starter-actuator:1.4.2.RELEASE
|    |    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE
|    |    |    +--- org.springframework.boot:spring-boot:1.4.2.RELEASE
|    |    |    |    +--- org.springframework:spring-core:4.3.4.RELEASE
|    |    |    |    \--- org.springframework:spring-context:4.3.4.RELEASE
|    |    |    |         +--- org.springframework:spring-aop:4.3.4.RELEASE
|    |    |    |         |    +--- org.springframework:spring-beans:4.3.4.RELEAS
E
|    |    |    |         |    |    \--- org.springframework:spring-core:4.3.4.RE
LEASE
|    |    |    |         |    \--- org.springframework:spring-core:4.3.4.RELEASE

|    |    |    |         +--- org.springframework:spring-beans:4.3.4.RELEASE (*)

|    |    |    |         +--- org.springframework:spring-core:4.3.4.RELEASE
|    |    |    |         \--- org.springframework:spring-expression:4.3.4.RELEAS
E
|    |    |    |              \--- org.springframework:spring-core:4.3.4.RELEASE

|    |    |    +--- org.springframework.boot:spring-boot-autoconfigure:1.4.2.REL
EASE
|    |    |    |    \--- org.springframework.boot:spring-boot:1.4.2.RELEASE (*)
|    |    |    +--- org.springframework.boot:spring-boot-starter-logging:1.4.2.R
ELEASE
|    |    |    |    +--- ch.qos.logback:logback-classic:1.1.7
|    |    |    |    |    +--- ch.qos.logback:logback-core:1.1.7
|    |    |    |    |    \--- org.slf4j:slf4j-api:1.7.20 -> 1.7.21
|    |    |    |    +--- org.slf4j:jcl-over-slf4j:1.7.21
|    |    |    |    |    \--- org.slf4j:slf4j-api:1.7.21
|    |    |    |    +--- org.slf4j:jul-to-slf4j:1.7.21
|    |    |    |    |    \--- org.slf4j:slf4j-api:1.7.21
|    |    |    |    \--- org.slf4j:log4j-over-slf4j:1.7.21
|    |    |    |         \--- org.slf4j:slf4j-api:1.7.21
|    |    |    +--- org.springframework:spring-core:4.3.4.RELEASE
|    |    |    \--- org.yaml:snakeyaml:1.17
|    |    \--- org.springframework.boot:spring-boot-actuator:1.4.2.RELEASE
|    |         +--- org.springframework.boot:spring-boot:1.4.2.RELEASE (*)
|    |         +--- org.springframework.boot:spring-boot-autoconfigure:1.4.2.REL
EASE (*)
|    |         +--- com.fasterxml.jackson.core:jackson-databind:2.8.4 (*)
|    |         +--- org.springframework:spring-core:4.3.4.RELEASE
|    |         \--- org.springframework:spring-context:4.3.4.RELEASE (*)
|    +--- org.springframework.boot:spring-boot-starter-jersey:1.4.2.RELEASE
|    |    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-tomcat:1.4.2.RELEASE

|    |    |    +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.6
|    |    |    +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.6
|    |    |    \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.6
|    |    |         \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.6
|    |    +--- org.springframework.boot:spring-boot-starter-validation:1.4.2.REL
EASE
|    |    |    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (
*)
|    |    |    +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.6
|    |    |    \--- org.hibernate:hibernate-validator:5.2.4.Final
|    |    |         +--- javax.validation:validation-api:1.1.0.Final
|    |    |         +--- org.jboss.logging:jboss-logging:3.2.1.Final -> 3.3.0.Fi
nal
|    |    |         \--- com.fasterxml:classmate:1.1.0 -> 1.3.3
|    |    +--- com.fasterxml.jackson.core:jackson-databind:2.8.4 (*)
|    |    +--- org.springframework:spring-web:4.3.4.RELEASE
|    |    |    +--- org.springframework:spring-aop:4.3.4.RELEASE (*)
|    |    |    +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|    |    |    +--- org.springframework:spring-context:4.3.4.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:4.3.4.RELEASE
|    |    +--- org.glassfish.jersey.core:jersey-server:2.23.2 (*)
|    |    +--- org.glassfish.jersey.containers:jersey-container-servlet-core:2.2
3.2 (*)
|    |    +--- org.glassfish.jersey.containers:jersey-container-servlet:2.23.2
|    |    |    +--- org.glassfish.jersey.containers:jersey-container-servlet-cor
e:2.23.2 (*)
|    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)
|    |    |    +--- org.glassfish.jersey.core:jersey-server:2.23.2 (*)
|    |    |    \--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    +--- org.glassfish.jersey.ext:jersey-bean-validation:2.23.2
|    |    |    +--- org.glassfish.hk2.external:javax.inject:2.5.0-b05
|    |    |    +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)
|    |    |    +--- org.glassfish.jersey.core:jersey-server:2.23.2 (*)
|    |    |    +--- javax.validation:validation-api:1.1.0.Final
|    |    |    +--- org.hibernate:hibernate-validator:5.1.3.Final -> 5.2.4.Final
 (*)
|    |    |    \--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    +--- org.glassfish.jersey.ext:jersey-spring3:2.23.2
|    |    |    +--- org.glassfish.jersey.core:jersey-server:2.23.2 (*)
|    |    |    +--- org.glassfish.jersey.containers:jersey-container-servlet-cor
e:2.23.2 (*)
|    |    |    +--- org.glassfish.hk2:hk2:2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2:hk2-utils:2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    +--- org.glassfish.hk2:config-types:2.5.0-b05
|    |    |    |    |    \--- org.glassfish.hk2:hk2-config:2.5.0-b05
|    |    |    |    |         \--- org.glassfish.hk2:hk2-core:2.5.0-b05
|    |    |    |    |              +--- org.glassfish.hk2:hk2-locator:2.5.0-b05
(*)
|    |    |    |    |              \--- org.glassfish.hk2:hk2-utils:2.5.0-b05
|    |    |    |    +--- org.glassfish.hk2:hk2-core:2.5.0-b05 (*)
|    |    |    |    +--- org.glassfish.hk2:hk2-config:2.5.0-b05 (*)
|    |    |    |    +--- org.glassfish.hk2:hk2-locator:2.5.0-b05 (*)
|    |    |    |    +--- org.glassfish.hk2:hk2-runlevel:2.5.0-b05
|    |    |    |    |    +--- org.glassfish.hk2:hk2-api:2.5.0-b05 (*)
|    |    |    |    |    \--- org.glassfish.hk2:hk2-locator:2.5.0-b05 (*)
|    |    |    |    \--- org.glassfish.hk2:class-model:2.5.0-b05
|    |    |    |         \--- org.glassfish.hk2.external:asm-all-repackaged:2.5.
0-b05
|    |    |    +--- org.glassfish.hk2:spring-bridge:2.5.0-b05
|    |    |    |    \--- org.springframework:spring-context:3.2.3.RELEASE -> 4.3
.4.RELEASE (*)
|    |    |    +--- org.springframework:spring-beans:3.2.3.RELEASE -> 4.3.4.RELE
ASE (*)
|    |    |    +--- org.springframework:spring-core:3.2.3.RELEASE -> 4.3.4.RELEA
SE
|    |    |    +--- org.springframework:spring-web:3.2.3.RELEASE -> 4.3.4.RELEAS
E (*)
|    |    |    \--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |    \--- org.glassfish.jersey.media:jersey-media-json-jackson:2.23.2
|    |         +--- org.glassfish.jersey.core:jersey-common:2.23.2 (*)
|    |         +--- org.glassfish.jersey.ext:jersey-entity-filtering:2.23.2
|    |         |    \--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    |         +--- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:2.5.4 -> 2.8.
4 (*)
|    |         +--- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.5.
4 -> 2.8.4 (*)
|    |         \--- com.fasterxml.jackson.core:jackson-annotations:2.5.4 -> 2.8.
4
|    +--- org.springframework.boot:spring-boot-starter-web-services:1.4.2.RELEAS
E
|    |    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-web:1.4.2.RELEASE
|    |    |    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (
*)
|    |    |    +--- org.springframework.boot:spring-boot-starter-tomcat:1.4.2.RE
LEASE (*)
|    |    |    +--- org.hibernate:hibernate-validator:5.2.4.Final (*)
|    |    |    +--- com.fasterxml.jackson.core:jackson-databind:2.8.4 (*)
|    |    |    +--- org.springframework:spring-web:4.3.4.RELEASE (*)
|    |    |    \--- org.springframework:spring-webmvc:4.3.4.RELEASE
|    |    |         +--- org.springframework:spring-aop:4.3.4.RELEASE (*)
|    |    |         +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|    |    |         +--- org.springframework:spring-context:4.3.4.RELEASE (*)
|    |    |         +--- org.springframework:spring-core:4.3.4.RELEASE
|    |    |         +--- org.springframework:spring-expression:4.3.4.RELEASE (*)

|    |    |         \--- org.springframework:spring-web:4.3.4.RELEASE (*)
|    |    +--- org.springframework:spring-oxm:4.3.4.RELEASE
|    |    |    +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:4.3.4.RELEASE
|    |    \--- org.springframework.ws:spring-ws-core:2.3.1.RELEASE
|    |         +--- org.springframework.ws:spring-xml:2.3.1.RELEASE
|    |         |    +--- org.springframework:spring-beans:4.0.9.RELEASE -> 4.3.4
.RELEASE (*)
|    |         |    +--- org.springframework:spring-context:4.0.9.RELEASE -> 4.3
.4.RELEASE (*)
|    |         |    \--- org.springframework:spring-core:4.0.9.RELEASE -> 4.3.4.
RELEASE
|    |         +--- org.springframework:spring-aop:4.0.9.RELEASE -> 4.3.4.RELEAS
E (*)
|    |         +--- org.springframework:spring-beans:4.0.9.RELEASE -> 4.3.4.RELE
ASE (*)
|    |         +--- org.springframework:spring-core:4.0.9.RELEASE -> 4.3.4.RELEA
SE
|    |         +--- org.springframework:spring-oxm:4.0.9.RELEASE -> 4.3.4.RELEAS
E (*)
|    |         +--- org.springframework:spring-web:4.0.9.RELEASE -> 4.3.4.RELEAS
E (*)
|    |         \--- org.springframework:spring-webmvc:4.0.9.RELEASE -> 4.3.4.REL
EASE (*)
|    +--- io.dropwizard.metrics:metrics-jersey2:3.1.2
|    |    +--- io.dropwizard.metrics:metrics-core:3.1.2 (*)
|    |    +--- io.dropwizard.metrics:metrics-annotation:3.1.2
|    |    |    \--- org.slf4j:slf4j-api:1.7.7 -> 1.7.21
|    |    +--- org.glassfish.jersey.core:jersey-server:2.11 -> 2.23.2 (*)
|    |    \--- org.slf4j:slf4j-api:1.7.7 -> 1.7.21
|    +--- org.springframework.boot:spring-boot-starter-web:1.4.2.RELEASE (*)
|    \--- net.logstash.logback:logstash-logback-encoder:4.7
|         +--- ch.qos.logback:logback-core:1.1.6 -> 1.1.7
|         \--- com.fasterxml.jackson.core:jackson-databind:2.6.5 -> 2.8.4 (*)
+--- org.springframework.boot:spring-boot-starter-jdbc: -> 1.4.2.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (*)
|    +--- org.apache.tomcat:tomcat-jdbc:8.5.6
|    |    \--- org.apache.tomcat:tomcat-juli:8.5.6
|    \--- org.springframework:spring-jdbc:4.3.4.RELEASE
|         +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|         +--- org.springframework:spring-core:4.3.4.RELEASE
|         \--- org.springframework:spring-tx:4.3.4.RELEASE
|              +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|              \--- org.springframework:spring-core:4.3.4.RELEASE
+--- org.springframework:spring-jdbc: -> 4.3.4.RELEASE (*)
+--- org.springframework.boot:spring-boot-starter-redis: -> 1.4.2.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE (*)
|    +--- org.springframework.data:spring-data-redis:1.7.5.RELEASE
|    |    +--- org.springframework.data:spring-data-keyvalue:1.1.5.RELEASE
|    |    |    +--- org.springframework.data:spring-data-commons:1.12.5.RELEASE
|    |    |    |    +--- org.springframework:spring-core:4.2.8.RELEASE -> 4.3.4.
RELEASE
|    |    |    |    +--- org.springframework:spring-beans:4.2.8.RELEASE -> 4.3.4
.RELEASE (*)
|    |    |    |    +--- org.slf4j:slf4j-api:1.7.21
|    |    |    |    \--- org.slf4j:jcl-over-slf4j:1.7.21 (*)
|    |    |    +--- org.springframework:spring-context:4.2.8.RELEASE -> 4.3.4.RE
LEASE (*)
|    |    |    +--- org.springframework:spring-tx:4.2.8.RELEASE -> 4.3.4.RELEASE
 (*)
|    |    |    +--- org.slf4j:slf4j-api:1.7.21
|    |    |    \--- org.slf4j:jcl-over-slf4j:1.7.21 (*)
|    |    +--- org.springframework:spring-tx:4.2.8.RELEASE -> 4.3.4.RELEASE (*)
|    |    +--- org.springframework:spring-oxm:4.2.8.RELEASE -> 4.3.4.RELEASE (*)

|    |    +--- org.springframework:spring-aop:4.2.8.RELEASE -> 4.3.4.RELEASE (*)

|    |    +--- org.springframework:spring-context-support:4.2.8.RELEASE -> 4.3.4
.RELEASE
|    |    |    +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|    |    |    +--- org.springframework:spring-context:4.3.4.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:4.3.4.RELEASE
|    |    +--- org.slf4j:slf4j-api:1.7.21
|    |    \--- org.slf4j:jcl-over-slf4j:1.7.21 (*)
|    \--- redis.clients:jedis:2.8.2
|         \--- org.apache.commons:commons-pool2:2.4.2
+--- org.springframework:spring-context-support: -> 4.3.4.RELEASE (*)
+--- io.projectreactor:reactor-bus: -> 2.0.8.RELEASE
|    +--- com.goldmansachs:gs-collections:5.1.0
|    |    \--- com.goldmansachs:gs-collections-api:5.1.0
|    \--- io.projectreactor:reactor-core:2.0.8.RELEASE
|         +--- org.reactivestreams:reactive-streams:1.0.0
|         \--- org.slf4j:slf4j-api:1.7.12 -> 1.7.21
+--- com.canaldigital.tsi:common-config-service:1.1.16 (*)
+--- com.thoughtworks.xstream:xstream:1.3
|    \--- xpp3:xpp3_min:1.1.4c
+--- com.oracle:ojdbc6:11.2.0.2.0
+--- io.swagger:swagger-jersey2-jaxrs:1.5.9 (*)
+--- org.glassfish.jersey.media:jersey-media-multipart:2.22.2 (*)
\--- junit:junit:4.11
     \--- org.hamcrest:hamcrest-core:1.3
Sohaib Jamal
  • 35
  • 1
  • 8

1 Answers1

3

You have multiple issues:

  1. You are mixing Jersey related annotations with Spring MVC ones.These are not working with Jersey, since they are Spring MVC specifics.

Instead of using these imports:

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

You need to replace them with:

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import java.io.InputStream;

Your controller would look similar to this:

import java.io.InputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.springframework.stereotype.Component;

@Path("/media")
@Produces(MediaType.APPLICATION_JSON)
@Component
public class GUIController {

    @Path("/upload")
    @POST
    @Consumes(MediaType.WILDCARD)
    public Response uploadPicture(
            @FormDataParam("file") InputStream file,
            @FormDataParam("file") FormDataContentDisposition filFormDataContentDisposition) {
        return Response.ok().build();
    }
}

This is the equivalent for jersey upload for multi part form data.

  1. In order to use the first, you will need to add a dependency in order for you to take leverage of Jersey's multipart functionality. Spring Boot does not offer these by default.

So you need to head over to Maven Central Repository to take the org.glassfish.jersey.media:jersey-media-multipart into your classpath.

You will need to also enable in your Jersey configuration the multipart feature like so:

@Component
@ApplicationPath("media")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        register(MultiPartFeature.class);
        register(GUIController.class);
    }
}
  1. This is not an issue per-se, but rather an advice to limit your consumption of the content types. Instead of doing @Consumes(MediaType.WILDCARD), a better way would be to be more specific on what you are trying to accomplish. In your case: @Consumes(MediaType.MULTIPART_FORM_DATA)
Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
  • Thank you for the detailed answer. helped me clarify certain things :) However Im still missing something. I changed as per point 1 also did point 3. Point 2: I created a JerseyConfig class as you said Every call works fine except the flle upload method Error: org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization What am I missing ? Do i need to do something in config class or in servlet registration ? – Sohaib Jamal May 12 '18 at 18:15
  • @SohaibJamal, can you try not creating that JerseyConfig? I had an example of my own where I register my jersey resource and I also added the `MultiPartFeature`. I think that might not be necessary. How does your resource get picked up by spring boot? – Andrei Sfat May 12 '18 at 18:21
  • @SohaibJamal I see an example where they don't do the `JerseyConfig` thing and it seems to be picked up nicely. Maybe take a peek at this tutorial https://www.geekmj.org/jersey/jax-rs-single-file-upload-example-406/ – Andrei Sfat May 12 '18 at 18:23
  • This tutorial also has a JerseyConfig in its project up for download But I tried by removing the JErseyConfig and i still got the same error. Could it be that I am doing something wrong registering these configs ? – Sohaib Jamal May 12 '18 at 19:29
  • @SohaibJamal can you edit your answer with an update, with what problems you have? Maybe show the stacktrace of the exceptions ? Maybe some versions of the spring boot and stuff like that, so I know what the problem might be – Andrei Sfat May 12 '18 at 20:22
  • @SohaibJamal, might be something similar with respect with that error https://stackoverflow.com/questions/39405067/org-glassfish-jersey-server-model-modelvalidationexception-validation-of-the-ap, I think that stacktrace of the exception will narrow down the issue. Maybe you have .the same `@Path` defined somewhere else. I added the `@Path("/test")` in your GUIController just as an example. – Andrei Sfat May 12 '18 at 20:25
  • I have updated my answer. Yes, i read this post too but I had changed the @Path to my own name whcih isnt defined anywhere else. – Sohaib Jamal May 12 '18 at 20:57
  • @SohaibJamal seems to be related to this https://stackoverflow.com/questions/14288856/jersey-2-injection-source-for-multipart-formdata – Andrei Sfat May 12 '18 at 21:03
  • 1
    @SohaibJamal what spring-boot, jersey, jersey-multipart versions are you using? – Andrei Sfat May 12 '18 at 21:07
  • I have updated the question with my servlet registration. springboot: 1.4.2.RELEASE jersey : 1.3.3.RELEASE jersey mulitpart: 2.27' – Sohaib Jamal May 12 '18 at 21:19
  • I am not able to run with jersey multipart 2.27 for some reason, I keep getting some error with `Caused by: java.lang.NoSuchFieldError: INCLUDE_ALL`. – Andrei Sfat May 12 '18 at 21:31
  • which version do you suggest ? – Sohaib Jamal May 12 '18 at 21:33
  • I got mine working with `jersey-media-multipart` 2.22.1. – Andrei Sfat May 12 '18 at 21:34
  • But I think that `ServletRegistrationBean` seems a bit weird. Can you try making the `JerseyConfig` annotated with `@Component` instead? There is a JerseyAutoConfiguration from spring boot that looks for `jerseyConfig` as bean, so it should be picked up. Or just make a normal bean. Also double checking, you're using `spring-boot-starter-jersey`, right? – Andrei Sfat May 12 '18 at 21:35
  • @SohaibJamal, I have tried the `ServletRegistrationBean`, it's definitely not picked up by spring boot. I will edit my answer with the updated version for your `JerseyConfig` to include a default path making use of `@ApplicationPath` – Andrei Sfat May 12 '18 at 21:57
  • @SohaibJamal, also, keep in mind, 1.3.3.RELEASE of spring-boot-starter-jersey comes with jersey 2.22.2. You will probably want to use that for `jersey-media-multipart` – Andrei Sfat May 12 '18 at 22:01
  • Yes it is spring-boot-starter-jersey Unfortunately I am still getting No injection source found for a parameter of type public javax.ws.rs.core.Response com.canaldigital.tsi.channel.endpoint.v1.impl.GUIController.uploadPicture(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0 – Sohaib Jamal May 12 '18 at 22:10
  • @SohaibJamal can you enable debug for your spring boot app? You can do that by adding in your application.properties/yml the following property: debug = true / debug: true, depending on your application file. And search for `JerseyAutoConfiguration` and let me know the output. Might be that jersey is not being auto configured by spring boot. Do you happen to have other controllers defined with jersey in your app or this is the single one? – Andrei Sfat May 13 '18 at 07:02
  • @SohaibJamal Also, have you configured the `JerseyConfig` like I suggested (removing `ServletRegistrationBean` and defining it as: `@Component @ApplicationPath("test") public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(MultiPartFeature.class); register(GUIController.class); } }`) – Andrei Sfat May 13 '18 at 07:02
  • @SohaibJamal also, take a look at this answer https://stackoverflow.com/a/30656345/471481 . Might be something related. – Andrei Sfat May 13 '18 at 07:17
  • No, the other controllers are using Jax-rs primarily. Yes, the jersey config is as you said – Sohaib Jamal May 13 '18 at 08:01
  • not sure what you are using. maven or gradle? can you try doing a dependency tree (`mvn dependency:tree` / `gradle dependencies`) to see what jersey related dependencies you have. – Andrei Sfat May 13 '18 at 08:22
  • its gradle, the tree is quite long, Ill update in my answer – Sohaib Jamal May 13 '18 at 08:27
  • Hey man, I tried sending the request through the web client and not postman. It WORKED.. THANKS ALOT man :) Youre the real champion – Sohaib Jamal May 13 '18 at 09:44
  • Glad I could be of help! Cheers – Andrei Sfat May 13 '18 at 10:15
  • 1
    Thanks so much ! This saved my day ! – Digao Jul 06 '20 at 23:46