A RESTful Web Service saying hello

Here I am completing the example started in the previous post, aimed at writing a simple Spring application using STS. I have ensured the app framework is working fine, now I write a very simple REST Controller.

I have already pushed the code on GitHub, however the job is so easy I don't mind to redo it from scratch. So, I create a Java class named GreetingController in the package controller under my already existing hello package
I annotate it as RestController and I provide it with a public method that returns a String, annotated as RequestMapping:
public class GreetingController {
    public String greeting() {
        return "Hello";
And that's it!

Let's see it in action. I go to the Boot Dashboard, and I start my helloSpring application.
Whoopsie daisy, I forgot to set the port of my Tomcat server to something different from the default 8080, that is already taken on this box. The result is a long scroll of complaints in the Console window, starting with an exception dump:
java.net.BindException: Address already in use: bind
Ending with an error description (The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.) and the action (Verify the connector's configuration, identify and stop any process that's listening on port 8080, or configure this application to listen on another port.) we should take to solve it.
There is a number of ways you could follow to solve this issue. Here I set the server port in the Spring application properties file, defined in the src/main/resources folder
When I restart Tomcat, I see I have a clean log
And now I can check on a browser how it works

No comments:

Post a Comment