Search code examples
javaspring-bootintellij-ideaterminal

Spring Boot Response Body is different while running within Intellij vs Terminal


I have a simple spring boot application with a PostMapping. I respond with a text in the end that contains a table, basically this table is created by appending the lines after I run a command inside the spring boot app. Below is a ss with a bad formatting. My problem is that this table is not correctly formatted when running the app inside the terminal by using (java -jar app.jar)enter image description here

Here is the good table while running from Intellij:enter image description here

This is the code that it's used to return this text.

@PostMapping("/newman")
public ResponseEntity<String> triggerNewmanTests(@Valid @RequestBody NewmanRequesBody requestBody) throws NewmanException {
    String uuidRequest = UUID.randomUUID().toString();
    File directory = directoryService.createDirectory(requestBody.getLinkRepo(), uuidRequest);
    logger.info("Directory created under "+directory.getAbsolutePath());
    try {
        gitService.cloneRepo(requestBody.getLinkRepo(), directory);
        logger.info("Directory cloned under "+directory.getAbsolutePath());
        StringBuilder newmanStringBuilder = newmanService.triggerNewman(
                requestBody.getLinkRepo(),
                requestBody.getEnvironment(),
                uuidRequest
        );
        logger.info(newmanStringBuilder.toString());
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/plain; charset=UTF-8");
        return new ResponseEntity<>(newmanStringBuilder.toString(),responseHeaders, HttpStatus.OK);

I have to mention that the log (logger.info(newmanStringBuilder.toString());) prints the text very good also in terminal, so basically something happens with the request, something may be added in the transportation in Intellij.


Solution

  • Solved this issue! Bassicaly the command that I have run, newman run collection returned a response with colors that were understood by the terminalenter image description here

    Therefor Java added the color codes inside the response and it was actually printed out. Somehow Intellij removed these color codes from the response (I have no idea how, I can't find documentation on how Intellij actually runs the code)

    The fix looks like this:

    @PostMapping("/newman")
    public ResponseEntity<String> triggerNewmanTests(@Valid @RequestBody NewmanRequesBody requestBody) throws NewmanException {
        String uuidRequest = UUID.randomUUID().toString();
        File directory = directoryService.createDirectory(requestBody.getLinkRepo(), uuidRequest);
        logger.info("Directory created under "+directory.getAbsolutePath());
        try {
            gitService.cloneRepo(requestBody.getLinkRepo(), directory);
            logger.info("Directory cloned under "+directory.getAbsolutePath());
            StringBuilder newmanStringBuilder = newmanService.triggerNewman(
                    requestBody.getLinkRepo(),
                    requestBody.getEnvironment(),
                    uuidRequest
            );
            logger.info(newmanStringBuilder.toString());
            HttpHeaders responseHeaders = new HttpHeaders();
            responseHeaders.add("Content-Type", "text/plain; charset=UTF-8");
            // This is the fix !
            final String msgWithoutColorCodes =
                    newmanStringBuilder.toString().replaceAll("\u001B\\[[;\\d]*m", "");
            return new ResponseEntity<>(msgWithoutColorCodes,responseHeaders, HttpStatus.OK);
    

    After fixing this, the java -jar app.jar runs smooth, I get the good characters back in the response.

    But in my context I'm running this jar inside a docker container which, again, broke my response. I've got:enter image description here

    To fix this, you have to add locales inside your docker container, in my case adding this fixed the issue:

    RUN apt-get -y install locales
    
    # Set the locale
    RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && \
        locale-gen
    ENV LANG en_US.UTF-8
    ENV LANGUAGE en_US:en
    ENV LC_ALL en_US.UTF-8