简体   繁体   English

Spring Boot rest controller not working when package in a jar

[英]Spring Boot rest controller not working when package in a jar

I am developing a REST API using Spring Boot rest controller. I am developing a REST API using Spring Boot rest controller. Something strange is happening;发生了一些奇怪的事情; 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. 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. What confuse me is that there is no log.让我困惑的是没有日志。 And when I put a sysout at the very beginning of my controller I realized that the controller is not even executed !当我在 controller 的开头放置一个sysout时,我意识到 controller 甚至没有执行!

Here is the controller with the concerned endpoint, but i am not sure it will help:这是带有相关端点的 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;
    }
}

Question: Is there any way of making spring boot log everything that's happening between tomcat and my controller?问题:有什么方法可以让 spring 引导记录 tomcat 和我的 controller 之间发生的所有事情? King of --verbose option for spring boot? spring 引导的 --verbose 选项之王?

PS: Here is the DockerFile I am using to deploy the app 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"]

I finally found the problem thx to log.level.root=debug ;我终于找到了log.level.root=debug的问题; I am using the Spring resourceloader to load the template for my PDF service but it seems that it is not able to find the resources folder inside a jar file.我正在使用 Spring 资源加载器为我的 PDF 服务加载模板,但它似乎无法在 jar 文件中找到资源文件夹。

It says: cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar./BOOT-INF/classes./templates/......它说:无法解析为绝对文件路径,因为它不驻留在文件系统中:jar:file:/app.jar./BOOT-INF/classes./templates/......

I found a solution on internet and made it work by using inputStream:我在互联网上找到了一个解决方案,并通过使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM