Загрузка MultipartFile в Springboot завершается неудачно с 415 - PullRequest
0 голосов
/ 12 мая 2018

У меня есть операция post, которая принимает multipartfile в качестве запроса, я хотел бы сохранить ее на FTP позже.

Но на данный момент у меня есть только настройка метода, и при попытке вызвать его из почтальона он выдает ошибку HTTP 415.

Вот что у меня есть:

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

}

Мой запрос и ответ почтальона:

enter image description here

Можете ли вы помочь мне исправить это? Спасибо

UPDATE:

После нескольких изменений, основанных на комментариях, вот что у меня есть:

Контроллер:

 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;
}

При вызове метода загрузки появляется следующая ошибка:

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]

Дерево зависимостей:

|    |    |    \--- 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

1 Ответ

0 голосов
/ 12 мая 2018

У вас есть несколько проблем:

  1. Вы смешиваете аннотации, относящиеся к Джерси, с Spring MVC. Они не работают с Джерси, поскольку они относятся к спецификациям Spring MVC.

Вместо использования этих импортов:

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

Вам необходимо заменить их на:

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

Ваш контроллер будет выглядеть примерно так:

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

Это эквивалент загрузки джерси для данных формы из нескольких деталей.

  1. Для того, чтобы использовать первое, вам нужно будет добавить зависимость, чтобы вы могли использовать универсальные функциональные возможности Джерси. Spring Boot не предлагает их по умолчанию.

Итак, вам нужно перейти в Центральный репозиторий Maven , чтобы перенести org.glassfish.jersey.media:jersey-media-multipart в ваш путь к классам.

Вам также необходимо включить в конфигурации на Джерси функцию, состоящую из нескольких частей, например:

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

    public JerseyConfig() {
        register(MultiPartFeature.class);
        register(GUIController.class);
    }
}
  1. Это не проблема как таковая, а скорее совет по ограничению потребления типов контента. Вместо того, чтобы делать @Consumes(MediaType.WILDCARD), лучшим способом было бы быть более точным в том, что вы пытаетесь достичь. В вашем случае: @Consumes(MediaType.MULTIPART_FORM_DATA)
...