Illegal character causes spring boot microservice build to fail during creation of docker image

322 views Asked by At

I am establishing inter communication with Micro services in a Spring boot application. The response is generated in JSON format and logged in log file using logback.xml. This is further leveraged on ELK side with docker-maven plugins. While performing mvn clean install, it throws exception during creation of docker image through pom.xml

Error stack :

Caused by: com.spotify.docker.client.exceptions.DockerException: com.spotify.docker.client.shaded.com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 0)): only regular white space (\r, \n, \t) is allowed between tokens at [Source: (File); line: 1, column: 2] at com.spotify.docker.client.auth.ConfigFileRegistryAuthSupplier.authForBuild (ConfigFileRegistryAuthSupplier.java:108) at com.spotify.docker.client.auth.MultiRegistryAuthSupplier.authForBuild (MultiRegistryAuthSupplier.java:77) at com.spotify.docker.client.DefaultDockerClient.build (DefaultDockerClient.java:1483) at com.spotify.docker.client.DefaultDockerClient.build (DefaultDockerClient.java:1460) at com.spotify.plugin.dockerfile.BuildMojo.buildImage (BuildMojo.java:240) at com.spotify.plugin.dockerfile.BuildMojo.execute (BuildMojo.java:135) at com.spotify.plugin.dockerfile.AbstractDockerMojo.tryExecute (AbstractDockerMojo.java:265) at com.spotify.plugin.dockerfile.AbstractDockerMojo.execute (AbstractDockerMojo.java:254) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)

pom.xml:

<!-- Dockerfile from Spotify -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.11</version>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId}</repository>
                </configuration>
                <executions>
                    <execution>
                        <id>default</id>
                        <phase>install</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

I debugged code and found the cause of error. This is caused in below method at line highlighted:

private List<Comment> findCommentsForFeed(Feeds feed) {
        log.info("Finding comments of feed with id {}", feed.getId());
        
        String url = UriComponentsBuilder.fromHttpUrl(commentServiceBaseUrl).path("comments")
                .queryParam("feedId", feed.getId()).toUriString();

        // ** THIS LINE CAUSES ERROR... **
        ResponseEntity<List<Comment>> response = restTemplate.exchange(url, HttpMethod.GET, null,
                new ParameterizedTypeReference<List<Comment>>() {
                });

        List<Comment> comments = Objects.isNull(response.getBody()) ? new ArrayList<>() : response.getBody();
        log.info("Found {} comment(s) of feed with id {}", comments.size(), feed.getId());
        return comments;
    }

My controller looks like :

@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/feeds", produces = MediaType.APPLICATION_JSON_VALUE)
public class FeedController {

    @Autowired
    private final FeedService service;

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<Feeds>> getFeeds() {
        List<Feeds> feeds = service.getFeeds();
        return ResponseEntity.ok(feeds);
    }

    @GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<FeedWithComment> getFeed(@PathVariable Long id) {
        FeedWithComment feedWithComments = service.getFeed(id).orElseThrow(ResourceNotFoundException::new);
        return ResponseEntity.ok(feedWithComments);
    }

My logback.xml file for json output appears like:

<springProfile name="docker">

    <appender name="jsonConsoleAppender"
        class="ch.qos.logback.core.ConsoleAppender">
        <encoder
            class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp>
                    <timeZone>UTC</timeZone>
                </timestamp>
                <version />
                <logLevel />
                <message />
                <loggerName />
                <threadName />
                <context />
                <pattern>
                    <omitEmptyFields>true</omitEmptyFields>
                    <pattern>
                        {
                        "trace": {
                        "trace_id": "%mdc{X-B3-TraceId}",
                        "span_id":
                        "%mdc{X-B3-SpanId}",
                        "parent_span_id": "%mdc{X-B3-ParentSpanId}",
                        "exportable": "%mdc{X-Span-Export}"
                        }
                        }
                    </pattern>
                </pattern>
                <mdc>
                    <excludeMdcKeyName>traceId</excludeMdcKeyName>
                    <excludeMdcKeyName>spanId</excludeMdcKeyName>
                    <excludeMdcKeyName>parentId</excludeMdcKeyName>
                    <excludeMdcKeyName>spanExportable</excludeMdcKeyName>
                    <excludeMdcKeyName>X-B3-TraceId</excludeMdcKeyName>
                    <excludeMdcKeyName>X-B3-SpanId</excludeMdcKeyName>
                    <excludeMdcKeyName>X-B3-ParentSpanId</excludeMdcKeyName>
                    <excludeMdcKeyName>X-Span-Export</excludeMdcKeyName>
                </mdc>
                <stackTrace />
            </providers>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="jsonConsoleAppender" />
    </root>

</springProfile>
1

There are 1 answers

0
user3397979 On

I did some analysis and found the issue is because of Spring boot version. There has been changes in directory structure of Spring boot after the introduction of Springboot 2.3.0. I am using Spring boot version 2.3.3. I did some changes in dockerfile and it worked. Maven docker plugin successfully created the image. Dockerfile :

FROM adoptopenjdk/openjdk14 as builder

WORKDIR application
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM adoptopenjdk/openjdk14
WORKDIR application
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "-Dspring.profiles.active=docker", "org.springframework.boot.loader.JarLauncher"]