I m trying to do Spring MVC tutorial:
It makes you create a main controller ad start a SpringApplication.
When I run the SpringApplication, I can see that a Tomcat server gets started, a controller class gets instanced as it should. However, the mapping seems to fail : @RequestMapping("/greeting") When I try to browse http://localhost:8080/TestSpringOpenEMM2/greeting or http://localhost:8080/TestSpringOpenEMM2/greeting , I always get a 404 error. (TestSpringOpenEMM2 is my project name).
I use eclipse IDE.
Here are my files:
package application;
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 starting" );
        SpringApplication.run(Application.class, args);
        System.out.println("App started" );
    }
}
controller:
import javax.servlet.annotation.WebServlet;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GreetingController {
static{
    System.out.println("Static init GreetingController");
}
    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        System.out.println("starting servlet (greeting)");
        model.addAttribute("name", name);
        return "greeting";
    }
    public GreetingController(){
        super();
        System.out.println("new GreetingController");
}
}
pom:
<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>TestSpringOpenEMM2</groupId>
  <artifactId>TestSpringOpenEMM2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>1.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>1.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-logging-juli</artifactId>
        <version>8.0.23</version>
    </dependency>
  </dependencies>
</project>
                Maybe you can use @RestController not @Controller. For REST api, if I use @Controller, the request will cause a 404 not found error, but after I change it to @RestController, the request works fine.
If you raise the Spring log level it should, when web-app starts, show you which URLs are being mapped to which classes/methods. That may help.
Also, if you're using Eclipse, right click project -> Properties -> Web Project Settings -> Context root. Make sure it is as you expect it to be i.e. TestSpringOpenEMM2. I have seen plenty of examples of this (context root) not being as expected in Eclipse.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With