使用Spring Boot进行泽西多部件配置

时间:2018-05-31 15:50:56

标签: java spring-boot jersey-2.0 multipart

我在尝试解析包含多部分文件的HttpServletRequest时遇到问题。我已经尝试了其他帖子的所有解决方案,关于我所面临的例外,但似乎没有任何效果。

这些是项目的相关依赖项:

<project>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.25.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.3</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

SpringBoot应用程序初始化程序:

@SpringBootApplication
public class ApplicationBoot extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(ApplicationBoot.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(ApplicationBoot.class, args);
    }
}

Jersey Initializer config和multipartResolver bean:

@Configuration
@ApplicationPath("/rest")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        register(AttachmentResource.class);
        register(ContentResource.class);
        register(MultiPartFeature.class);
    }

    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(100000);
        return multipartResolver;
    }

}

我面临的问题是关于多部分解析以及资源的问题:

@Component
@Path("/attachments")
public class AttachmentResource {

    @POST 
    @Path("/store")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public Response store(@Context HttpServletRequest httpRequest) {

        for (Part part : httpRequest.getParts()) {
        ...
        }
    }
}

当我调用getParts()时,我会收到 java.lang.IllegalStateException:由于未提供多部件配置,因此无法处理部件

我在application.properties中添加了这些属性:

spring.http.multipart.enabled=true
spring.http.multipart.max-file-size=100MB
spring.http.multipart.max-request-size=100MB
spring.http.multipart.file-size-threshold=0

在此处找到:https://docs.spring.io/spring-boot/docs/1.5.13.RELEASE/reference/html/common-application-properties.html

这也是SpringBoot在启动时启用调试输出时记录的有关Multipart配置的内容:

=========================
AUTO-CONFIGURATION REPORT
=========================

Positive matches:
-----------------
    MultipartAutoConfiguration matched:
        - @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.multipart.support.StandardServletMultipartResolver', 'javax.servlet.MultipartConfigElement'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
        - @ConditionalOnProperty (spring.http.multipart.enabled) matched (OnPropertyCondition)

   MultipartAutoConfiguration#multipartConfigElement matched:
      - @ConditionalOnMissingBean (types: javax.servlet.MultipartConfigElement; SearchStrategy: all) did not find any beans (OnBeanCondition)


Negative matches:
-----------------
    MultipartAutoConfiguration#multipartResolver:
        Did not match:
            - @ConditionalOnMissingBean (types: org.springframework.web.multipart.MultipartResolver; SearchStrategy: all) found bean 'multipartResolver' (OnBeanCondition)

稍后编辑:

如果我在独立的tomcat 8中部署应用程序并在con / context.xml中添加它,那么它可以工作:

<Context allowCasualMultipartParsing="true">

或者在项目中的src / main / webapp / META-INF下添加一个context.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="" allowCasualMultipartParsing="true"/>

但如果我使用嵌入式Tomcat启动项目,它就不起作用。

1 个答案:

答案 0 :(得分:1)

这不是如何在泽西岛使用MultiPart。如果要获取所有部分,可以使用FormDataMultiPart作为实体参数

@POST
public Response post(FormDataMultiPart multiPart) {
    Map<String, List<FormDataBodyPart>> parts = multiPart.getParts(); 
}

如果您希望按名称注入单个零件,可以使用@FormDataParam注释,将零件名称传递给注释。

@POST
public Response post(@FormDataParam("file") InputStream file,
                     @FormDataParam("file") FormDataContentDisposition fileDatail) {

}

另见