You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring项目部署Ubuntu服务器触发NoClassDefFoundError问题求助

Spring项目部署Ubuntu Tomcat触发NoClassDefFoundError问题

我用Spring构建的Web项目在本地Tomcat运行正常,但部署到Ubuntu服务器后,执行到以下两个控制器方法时触发java.lang.NoClassDefFoundError: Could not initialize class org.springframework.web.accept.PathExtensionContentNegotiationStrategy$ActivationMediaTypeFactory错误,服务器启动时无异常。

触发错误的控制器代码

@GetMapping(value = "/image.do", produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<byte[]> getImage(@RequestParam("imageId") String imageId, CommunityInfographicVO vo) throws Exception {
    logger.info("썹네일 이미지 controller 접근");
    int id = Integer.parseInt(imageId);
    vo.setInfoNo(id);
    List<CommunityInfographicVO> result = communityInfographicService.imageSelect(vo);
    logger.info("result from the DB got");

    if (result != null && !result.isEmpty()) {
        String path = result.get(0).getFilePath();
        String fileName = result.get(0).getAtchFileNm();
        String originalFileName = result.get(0).getOrignlFileNm();

        String filePath = path + File.separator + fileName + originalFileName;
        logger.info("filePathCreated");

        if (filePath != null && fileName != null) {
            try (InputStream inputStream = new FileInputStream(new File(filePath))) {
                byte[] fileBytes = IOUtils.toByteArray(inputStream);
                logger.info("fileBytes Created");
                // 返回图片字节流
                return ResponseEntity.ok().body(fileBytes);
            }
        }
    }
    return null;
}

@GetMapping(value = "/pdfImage.do", produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<byte[]> getPdfImag(@RequestParam("imageId") String imageId, @RequestParam("page") String page, CommunityInfographicVO vo) throws Exception {
    int infoNo = Integer.parseInt(imageId);
    int pageNo = Integer.parseInt(page);
    vo.setInfoNo(infoNo);
    List<CommunityInfographicVO> result = communityInfographicService.fileSelect(vo);
    if (result != null && !result.isEmpty()) {
        String path = result.get(0).getFilePath();
        String fileName = result.get(0).getAtchFileNm();
        String originalFileName = result.get(0).getOrignlFileNm();

        String filePath = path + File.separator + fileName + originalFileName;
        File file = new File(filePath);
        PDDocument document = PDDocument.load(file);
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        byte[] bytes = null;

        BufferedImage imageObj = pdfRenderer.renderImageWithDPI(pageNo, 100, ImageType.RGB);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(imageObj, "jpg", baos);
        bytes = baos.toByteArray();
        return ResponseEntity.ok().body(bytes);
    }
    return null;
}

解决方案

  • 检查Spring依赖完整性:解压部署的WAR包,确认WEB-INF/lib目录下存在对应版本的spring-web-*.jar,确保没有遗漏Spring Web核心依赖。
  • 补充JAF相关依赖:ActivationMediaTypeFactory初始化依赖Java Activation Framework,需在项目pom.xml(或Gradle配置)中添加对应依赖。Spring Boot项目可添加:
<dependency>
    <groupId>com.sun.activation</groupId>
    <artifactId>javax.activation</artifactId>
    <version>1.2.0</version>
</dependency>

非Spring Boot项目根据Spring版本匹配JAF依赖,避免因缺失javax.activation相关类导致初始化失败。

  • 验证JDK与Spring版本兼容性:确认Ubuntu服务器的JDK版本满足项目Spring版本要求,比如Spring 5.3+需要JDK 8及以上,Spring 6+需要JDK 17+,版本不兼容会引发类初始化异常。
  • 禁用路径扩展名内容协商:通过配置跳过PathExtensionContentNegotiationStrategy的触发,避免加载出错的类。在Spring配置类中添加:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }
}

或者在application.properties中配置:

spring.mvc.contentnegotiation.favor-path-extension=false
  • 检查服务器文件权限:确保Tomcat进程拥有读取控制器中指定文件路径的权限,避免文件读取异常间接触发类加载问题。

内容的提问来源于stack exchange,提问作者isuho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 02:00:59