gpt4 book ai didi

java - 使用 Spring Boot 进行 Jersey 多部分配置

转载 作者:行者123 更新时间:2023-11-30 02:09:03 27 4
gpt4 key购买 nike

我在尝试解析包含多部分文件的 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 初始化程序配置和 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;
}

}

以及我面临有关多部分解析和资源问题的类(class):

@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 在启动时启用有关多部分配置的调试输出时记录的内容:

=========================
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启动项目就不行了。

最佳答案

这不是如何将 MultiPart 与 Jersey 一起使用。如果你想获得所有零件,可以使用 FormDataMultiPart作为实体参数

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

如果您想按名称注入(inject)各个部件,可以使用@FormDataParam注释,将部件的名称传递给注释。

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

}
<小时/>

另请参阅

关于java - 使用 Spring Boot 进行 Jersey 多部分配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50628251/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com