I’ve been looking at some Restful services tutorial, and now I’m following this one
I’ve built the application according to it (I’m using eclipse and maven) by importing the guide and taking the code from there as in here
.
To run it I created the Application class with a main method, built the maven application but I had problems running it in the browser because I couldn’t find which port it was running on, so I created a src/main/resources
folder (which was missing by the way) and I added an application.properties file with server.port = 8989
in it.
Now, when I log on to http://localhost:8989/greeting (according to the tutorial this is the page that should display the application because we’re using @RequestMapping("/greeting"))
I get a
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
I had a look online, run into this thread but the tutorial I linked to above doesn’t list or mention the thymeleaf dependency at all, and I don’t have any HTML file like the OP in that thread, that’s why I posted here.
Also the console log is interesting, as it has a warning about my pom.xml file:
“[WARNING] The requested profile "pom.xml" could not be activated because it does not exist”:
and it does mention something about the folder that I’ve created too:
“skip non existing resourceDirectory C:\workspace\gs-consuming-rest-initial\src\test\resources”.
Here is the full log:
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building gs-consuming-rest 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.4.3.RELEASE:run (default-cli) @ gs-consuming-rest >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ gs-consuming-rest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ gs-consuming-rest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ gs-consuming-rest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\workspace\gs-consuming-rest-initial\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ gs-consuming-rest ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.4.3.RELEASE:run (default-cli) @ gs-consuming-rest <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:1.4.3.RELEASE:run (default-cli) @ gs-consuming-rest ---
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.3.RELEASE)
2017-01-19 14:09:57.701 INFO 23288 --- [ main] hello.Application : Starting Application on XXXXXX with PID XXXXXX (C:\workspace\gs-consuming-rest-initial\target\classes started by XXXXXX in C:\workspace\gs-consuming-rest-initial)
2017-01-19 14:09:57.704 INFO 23288 --- [ main] hello.Application : No active profile set, falling back to default profiles: default
2017-01-19 14:09:57.738 INFO 23288 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1c2fd2b6: startup date [Thu Jan 19 14:09:57 GMT 2017]; root of context hierarchy
2017-01-19 14:09:58.331 INFO 23288 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-01-19 14:09:58.338 INFO 23288 --- [ main] hello.Application : Started Application in 0.983 seconds (JVM running for 3.448)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.714 s
[INFO] Finished at: 2017-01-19T14:09:58+00:00
[INFO] Final Memory: 26M/327M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.
2017-01-19 14:09:58.399 INFO 23288 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1c2fd2b6: startup date [Thu Jan 19 14:09:57 GMT 2017]; root of context hierarchy
2017-01-19 14:09:58.400 INFO 23288 --- [ Thread-1] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
And my pom.xml is here for completeness:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-consuming-rest</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
As I’m new to restful services, is there anything else I should do that the tutorial isn’t mentioning?
As the files are rather short, I'll include them: GreetingController:
package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController
{
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
//@RequestMapping maps all HTTP operations
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
Greeting:
package hello;
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;
}
}
Application:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application
{
public static void main(String[] args)
{
System.out.println("App Started");
SpringApplication.run(Application.class, args);
}
}