如何采用Spring Boot的Jersey多部分配置?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (142)

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

<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初始化器配置和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()) {
        ...
        }
    }
}
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
=========================
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)
<Context allowCasualMultipartParsing="true">
<?xml version="1.0" encoding="UTF-8"?>
<Context path="" allowCasualMultipartParsing="true"/>
提问于
用户回答回答于

这不是如何在Jersey中使用MultiPart。

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

}

扫码关注云+社区

领取腾讯云代金券