在Springboot中上传MultipartFile失败了415

时间:2018-05-12 12:58:58

标签: spring-boot image-uploading multipartform-data multipart http-status-code-415

我有一个post子操作,它将multipartfile作为请求,我想稍后将它存储在FTP上。

但是现在我只有方法设置,当尝试从邮递员调用时,它给我一个错误响应HTTP 415。

这就是我所拥有的:

控制器:

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

你能帮帮我解决这个问题吗? 感谢

更新:

根据评论中的一些变化,这就是我所拥有的:

控制器:

 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 :(得分:1)

您有多个问题:

  1. 您正在将Jersey相关注释与Spring MVC混合使用。这些注释不适用于Jersey,因为它们是Spring MVC细节。
  2. 而不是使用这些导入:

    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. 为了使用第一个,您需要添加一个依赖项,以便您利用Jersey的多部分功能。 Spring Boot默认不提供这些。
    2. 因此,您需要转到Maven Central Repository,将org.glassfish.jersey.media:jersey-media-multipart带入您的类路径。

      您还需要在Jersey配置中启用多部分功能,如下所示:

      @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)