简体   繁体   中英

404 error on main controller when using spring boot

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>

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 ie TestSpringOpenEMM2. I have seen plenty of examples of this (context root) not being as expected in Eclipse.

The spring-boot-starter-parent is a great way to use Spring Boot, hence use a pom.xml like this:

<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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
</project>

By default spring boot using a embedded tomcat container, you just need to run the main method. Read more on this topic on Spring Boot's Doc

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM