Spring WebFlux集成JodConverter转PDF遇415媒体类型不支持错误求助
解决Spring WebFlux + JodConverter文档转PDF时的415 Unsupported Media Type错误
可能的原因及解决方案
1. 修正Controller的请求参数注解配置
确保接收文件的参数使用@RequestPart而非@RequestParam,同时明确指定接口支持的媒体类型,覆盖application/msword(.doc)、application/vnd.openxmlformats-officedocument.wordprocessingml.document(.docx)等常见文档类型。
示例修正后的Controller代码:
import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.codec.multipart.FilePart; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; import org.springframework.core.io.Resource; @RestController public class ConverterController { private final ConverterService converterService; public ConverterController(ConverterService converterService) { this.converterService = converterService; } @PostMapping( value = "/converter/v1/pdf", consumes = MediaType.MULTIPART_FORM_DATA_VALUE ) public Mono<ResponseEntity<Resource>> convertToPdf( @RequestPart(value = "file", required = true) FilePart filePart ) { return converterService.convertToPdf(filePart) .map(resource -> ResponseEntity.ok() .header("Content-Disposition", "attachment; filename=\"converted.pdf\"") .contentType(MediaType.APPLICATION_PDF) .body(resource)); } }
2. 确认WebFlux的Multipart解析配置
Spring WebFlux默认支持multipart请求,但如果存在自定义配置,需确保Multipart解析器正确初始化。可以在配置类中显式声明:
import org.springframework.context.annotation.Configuration; import org.springframework.http.codec.multipart.DefaultPartHttpMessageReader; import org.springframework.web.reactive.config.WebFluxConfigurer; import org.springframework.http.codec.ServerCodecConfigurer; @Configuration public class WebFluxConfig implements WebFluxConfigurer { @Override public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) { configurer.defaultCodecs().multipartResolver(new DefaultPartHttpMessageReader()); } }
也可在application.yml中调整multipart的大小限制(按需配置):
spring: webflux: multipart: max-file-size: 10MB max-request-size: 10MB
3. 验证Postman请求配置
- 确保请求方式为POST,地址准确:
http://localhost:8083/converter/v1/pdf - 选择
form-data格式,Key设置为file(需与Controller中@RequestPart的value完全匹配),Value选择文件上传,不要手动修改文件的Content-Type,让Postman自动识别 - 若自动识别异常,可手动指定对应类型:.doc文件设为
application/msword,.docx文件设为application/vnd.openxmlformats-officedocument.wordprocessingml.document
4. 检查JodConverter依赖与配置
确保项目引入正确的JodConverter依赖(以Maven为例):
<dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.4.6</version> </dependency> <dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-local</artifactId> <version>4.4.6</version> </dependency>
同时在application.yml中配置LibreOffice/OpenOffice的路径与端口:
jodconverter: local: enabled: true office-home: /usr/lib/libreoffice # Linux路径,Windows示例:C:/Program Files/LibreOffice port-numbers: 8100
注意:需确保LibreOffice/OpenOffice服务已正常启动。
5. 调试请求详情
在Controller中添加日志,打印文件的Content-Type与基本信息,确认请求是否被正确解析:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger log = LoggerFactory.getLogger(ConverterController.class); @PostMapping(...) public Mono<ResponseEntity<Resource>> convertToPdf(@RequestPart("file") FilePart filePart) { log.info("接收文件:{},Content-Type:{}", filePart.filename(), filePart.headers().getContentType()); // 后续转换逻辑 }
内容的提问来源于stack exchange,提问作者Akhilesh Pandey
相关产品推荐
相关产品推荐

