[英]Spring Boot rest controller not working when package in a jar
I am developing a REST API using Spring Boot rest controller. 发生了一些奇怪的事情; When I test my controller with Eclipse it is working just fine BUT when i deploy the app, packaged in a jar and started with the "java" command line in a docker container then, it doesn't work. 让我困惑的是没有日志。 当我在 controller 的开头放置一个sysout时,我意识到 controller 甚至没有执行!
这是带有相关端点的 controller,但我不确定它是否会有所帮助:
@RestController
@RequestMapping("/pdf")
@EnableSwagger2
public class PDFGeneratorResources {
@Autowired
PDFGenerator pdfService;
@Autowired
ResourceLoader resourceLoader;
@PostMapping("/generate-recipies-shoppinglist")
public ResponseEntity<String> generateRecipiesAndShoppingListPDF(@RequestBody List<Day> daysList) {
System.out.println("TRACE");
ResponseEntity<String> responseEntity = null;
String generatedPDFFileURL = "";
try {
generatedPDFFileURL = pdfService.generatePDFFromHTML(PDFTemplates.RecipiesAndShoppingList,
new RecipiesShoppinglistContextBuilder(new ArrayList<Day>(daysList)));
responseEntity = new ResponseEntity<String>(generatedPDFFileURL, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
responseEntity = new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return responseEntity;
}
}
问题:有什么方法可以让 spring 引导记录 tomcat 和我的 controller 之间发生的所有事情? spring 引导的 --verbose 选项之王?
PS:这是我用来部署应用程序的 DockerFile
FROM registry.gitlab.com/softreaver/meals-ready-backend/runners:centos7jdk11
MAINTAINER MILAZZO_christopher
COPY ./target/*.jar /app.jar
RUN echo -e "/usr/bin/java -Xms128m -Xmx128m -jar /app.jar\n" > /start-app.sh
RUN chmod u+x /start-app.sh
EXPOSE 8080
ENTRYPOINT ["/bin/bash", "/start-app.sh"]
我终于找到了log.level.root=debug的问题; 我正在使用 Spring 资源加载器为我的 PDF 服务加载模板,但它似乎无法在 jar 文件中找到资源文件夹。
它说:无法解析为绝对文件路径,因为它不驻留在文件系统中:jar:file:/app.jar./BOOT-INF/classes./templates/......
我在互联网上找到了一个解决方案,并通过使用 inputStream 使其工作:
@Service
public class ResourceLoaderService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
ResourceLoader resourceLoader;
public String getResourceAbsolutePathString(String location) throws Exception {
Resource resource = resourceLoader.getResource(location);
String absolutePathString = "/";
try {
if (resource.getURL().getProtocol().equals("jar")) {
logger.debug("Jar file system activated");
File tempFile = Files.createTempFile("Mealsready_backend_", null).toFile();
resource.getInputStream().transferTo(new FileOutputStream(tempFile));
absolutePathString = tempFile.getAbsolutePath();
} else {
absolutePathString = resource.getFile().getAbsolutePath();
}
} catch (IOException e) {
logger.error("Error while trying to retrieve a resource : " + e.getMessage());
// TO DELETE Remplacer par un ServiceException
throw new Exception();
}
return absolutePathString;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.