簡體   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. 發生了一些奇怪的事情; 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM