REST API调用报inStream parameter is null错误排查求助
问题解决方案
核心错误定位
inStream parameter is null 错误直接来自 OwnerServiceImpl 的静态初始化块:加载 sql.properties 时,getClassLoader().getResourceAsStream("sql.properties") 返回 null,说明配置文件未在类路径中找到。
分步修复
1. 修正配置文件路径
确保 sql.properties 放在 Maven/Gradle 项目的 src/main/resources 根目录下,编译后该文件会被打包到类路径根目录,才能被类加载器正确读取。如果文件放在子目录(比如 resources/config),则要修改读取路径为 config/sql.properties。
2. 完善配置文件加载的异常处理
修改 OwnerServiceImpl 中的静态代码块,增加空值检查和日志输出,方便排查问题:
private final Properties sqlCommands = new Properties(); private static final Logger logger = LogManager.getLogger(OwnerServiceImpl.class); { final ClassLoader loader = getClass().getClassLoader(); try (InputStream config = loader.getResourceAsStream("sql.properties")) { if (config == null) { logger.error("未在类路径中找到 sql.properties 文件"); throw new RuntimeException("加载 sql.properties 失败:文件不存在"); } sqlCommands.load(config); } catch (IOException e) { logger.error("加载 sql.properties 出错", e); throw new RuntimeException("加载 sql.properties 失败", e); } }
3. 修复 DTO 注解冲突
PropertyOwnerDto 中 @Data 注解已经包含了 @Getter 和 @Setter 的功能,重复添加会导致注解冲突,删除多余的注解:
@Data public class PropertyOwnerDto { // 字段和方法保持不变 }
4. 确保 CDI 注入生效
给 OwnerServiceImpl 添加 CDI 作用域注解(比如 @ApplicationScoped),否则无法被注入到 OwnerResource 中:
@ApplicationScoped public class OwnerServiceImpl implements OwnerService { // 原有代码保持不变 }
5. 验证 JSON 序列化支持
确保项目依赖了 JSON 序列化库(WildFly 默认自带 Jackson),同时 RestApiResult 类需要具备正确的 getter/setter 方法(可以添加 @Data 注解),避免因无法序列化导致的隐性错误。
内容的提问来源于stack exchange,提问作者user10591439
相关产品推荐
相关产品推荐

