'Building a RESTful Web Service: https://spring.io/guides/gs/rest-service/ Tutorial

I'm following the Spring training guide „Build a restful web service“ The application throws an error when I try and access http://localhost:8080/greeting. The error is:

ERROR 17903 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.util.FormatFlagsConversionMismatchException: Conversion = s, Flags = ] with root cause

Working on Ubuntu 20.04, openjdk version "11.0.14.1" 2022-02-08, mvn -version:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Java version: 1.8.0_312, vendor: Private Build, runtime: /usr/lib/jvm/java-8-openjdk-amd64/jre

Here are the relevant classes:

public class GreetingController {

private static final String template = "Hello, % s!";
private final AtomicLong counter = new AtomicLong();

public Greeting greeting(@RequestParam(value = "name",
                         defaultValue = "World") String name) {
    return new Greeting(counter.incrementAndGet(), String.format(template, name));
}

}

public class Greeting {

private final long id;
private final String content;

public Greeting(long id, String content) {
    this.id = id;
    this.content = content;
}

public long getId() {
    return id;
}

public String getContent() {
    return content;
}

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

I used Spring Initializr and added the dependency Spring Web as in the guide. Also the line:

com.example.demo.DemoApplication : Starting DemoApplication using Java 17.0.1

Seems like it is expecting Java 17 to run it. I have used Java 11 and Maven 2.6.7 in initializr and java 11 is set on my local machine.

Thanks in advance for any help DataSperling



Solution 1:[1]

Solution: In the class:

public class GreetingController

The line:

private static final String template = "Hello, % s!";

Should read:

private static final String template = "Hello, %s!";

Also the url neads to be:

http://localhost:8080/greeting

NOT

https://localhost:8080/greeting

SSL is not required.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sperling