1

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);

    }

}
Community
  • 1
  • 1
antobbo
  • 255
  • 1
  • 4
  • 21
  • 1
    you should post your code in your questions not using external links, this is what stackoverflow is for, don't be afraid to post code, the default port is 8080, seems there's no specific page to display that's why you get the callback, so you are trying to use thymeleaf as rendering engine? The post does say you have to add the Maven dependency. – viruskimera Jan 19 '17 at 15:01
  • Thanks, updated. Yes the default is 8080 but I can't run it on that port that's why I tried to change it with the application.property document. Also, from what I understand, it's not supposed to display a specific page as such but, if you have a quick look at the end of the tutorial, only a json object with data in, I presume that's why they didn't include any specific page. The tutorial doesn't mention any page I'm afraid. The other thread does indeed say to add that dependency but I thought that was because the OP had a specific page in his app whereas I don't. – antobbo Jan 19 '17 at 15:13
  • Is your @RequestMapping in a bean ? (@ RestControler at class level, or @ Controller, @ Component, etc.) – Nicolas Jan 19 '17 at 15:18
  • it's exactly like the tutorial, the method with the `@RequestMapping` annotation sits inside the class with a `@RestController`, i'll edit the threads with the files too – antobbo Jan 19 '17 at 15:22

1 Answers1

2

OK, it seems I manage to get it to work in the end. In my case, it was a combination of things: first the port, I kept changing port at random and then settled for 8900, but I'm sure that any would do.

Then this thymeleaf dependency: I didn't add it straightaway as suggested in here simply because the OP there had an html file (a view) in his project whereas I didn't and the way I interpreted it was that you need that dependency only to display the pre-existing view if you have any. Besides, and this is key, the tutorial followed does not mention this dependency. You'd think that if it was so important they would add it?! In the end I added it to my pom, rebuilt the app and now I get what the the tutorial say: I navigate to /greeting and I get this in the page: {"id":4,"content":"Hello, World!"}. So the take for me here is that, no matter what, you need that dependency and it'll be the first thing to add to my pom next time (shortly) I (attempt to) build a Restful service. Thanks to all the contributors for the help

EDIT: I thought I'd update this with something else I discovered. The tutorial doesn't say what kind of Maven project (read what archetype) to use, so I tried all of them with mixed results. It turned out - and apologies to those of you who know that already - that it seems best to use a "Spring starter Project" and then select the web dependency. This gives you the project structure you need for the restful service you need to build: it also gives you the src/main/resources folder which wasn't there before (with an empty application.property file where you can specify the port), it gives you a pom file complete with all the relevant dependencies (before I had to add quite a few dependencies manually) and, perhaps more importantly, you will not need the thymeleaf dependency in the pom.xml, it works without it (tested). Hopefully this will help other beginners.

Community
  • 1
  • 1
antobbo
  • 255
  • 1
  • 4
  • 21
  • so you added: org.springframework.boot spring-boot-starter-thymeleaf – viruskimera Jan 19 '17 at 16:04
  • I've also discovered something else about what type of application needs to be created, I've updated the main thread with it as I think it's really interesting and important. – antobbo Jan 20 '17 at 08:21
  • Thanks for posting this, @antobbo I have been struggling with this all afternoon. Unfortunately, even adding the thymeleaf dependency has not worked for me. I will take a break now, and try afresh with the "Spring starter project" Maven archetype when I come back. – Ajoy Bhatia May 30 '18 at 23:57
  • I wonder if the people who maintain that Spring guide document know about all the struggles that people are going through with it. It has definitely been more than 15 minutes for me, and still not there yet. Maybe if I get it working, I should find a way to contact them and let them know. – Ajoy Bhatia May 31 '18 at 00:13
  • @antobbo - Your Edit says to use "Spring starter Project" and then select the web dependency. Could you tell me how to get this "Spring starter Project" archetype in the list when creating Maven project in Eclipse? It is not there in the catalog that is shown by default, so I guess I need the archetype catalog URL to add it. Were you using STS, which might have that archetype by default? – Ajoy Bhatia May 31 '18 at 17:23
  • @antobbo - Never mind. I went to Spring Initializr at https://start.spring.io/ , entered Group & Artifact names, typed "Web" in the _Search for dependencies_ textbox and selected the **Web** option from the list that came up. Then clicked on **Generate Project**, saved generated zip file and unzipped it. Then I copied all the source files, keeping same package name "hello". Loaded it in Eclipse, opened Application.java and selected _Run As_ -> _Java Application_ from context menu. Could see the path **/greeting** being loaded. Then, hitting http://localhost:8008/greeting gave correct response. – Ajoy Bhatia May 31 '18 at 20:45