IDEA运行正常,要部署时打成jar包却出现问题
原始代码为
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.thingsboard.common.util.JacksonUtil;
import jakarta.annotation.PostConstruct;
import java.io.IOException;
@Service
@Slf4j
public class DefaultTbMailConfigTemplateService implements TbMailConfigTemplateService {
private JsonNode mailConfigTemplates;
@PostConstruct
private void postConstruct() throws IOException {
mailConfigTemplates = JacksonUtil.toJsonNode(new ClassPathResource("/templates/mail_config_templates.json").getFile());
}
@Override
public JsonNode findAllMailConfigTemplates() {
return mailConfigTemplates;
}
}
改成流的形式读取
package org.thingsboard.server.service.mail;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.thingsboard.common.util.JacksonUtil;
import jakarta.annotation.PostConstruct;
import java.io.IOException;
import org.springframework.core.io.ClassPathResource;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
@Service
@Slf4j
public class DefaultTbMailConfigTemplateService implements TbMailConfigTemplateService {
private JsonNode mailConfigTemplates;
@PostConstruct
private void postConstruct() throws IOException {
// 获取 ClassPathResource 实例
ClassPathResource resource = new ClassPathResource("/templates/mail_config_templates.json");
// 使用 InputStream 读取资源
try (InputStream inputStream = resource.getInputStream()) {
// 使用 ObjectMapper 解析 JSON
ObjectMapper objectMapper = new ObjectMapper();
mailConfigTemplates = objectMapper.readTree(inputStream);
}
}
@Override
public JsonNode findAllMailConfigTemplates() {
return mailConfigTemplates;
}
}
即可运行成功