简体   繁体   English

使用弹簧启动时主控制器上出现404错误

[英]404 error on main controller when using spring boot

I m trying to do Spring MVC tutorial: 我正在尝试做Spring MVC教程:

It makes you create a main controller ad start a SpringApplication. 它使您创建一个主控制器广告启动SpringApplication。

When I run the SpringApplication, I can see that a Tomcat server gets started, a controller class gets instanced as it should. 当我运行SpringApplication时,我可以看到一个Tomcat服务器启动,一个控制器类得到实例化。 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. 但是,映射似乎失败了:@RequestMapping(“/ greeting”)当我尝试浏览http:// localhost:8080 / TestSpringOpenEMM2 / greetinghttp:// localhost:8080 / TestSpringOpenEMM2 / greeting时 ,我总是得到404错误。 (TestSpringOpenEMM2 is my project name). (TestSpringOpenEMM2是我的项目名称)。

I use eclipse IDE. 我使用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: 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. 如果你提高Spring日志级别,那么当web-app启动时,它应该显示哪些URL被映射到哪些类/方法。 That may help. 这可能有所帮助。

Also, if you're using Eclipse, right click project -> Properties -> Web Project Settings -> Context root. 此外,如果您正在使用Eclipse,请右键单击项目 - >属性 - > Web项目设置 - >上下文根。 Make sure it is as you expect it to be ie TestSpringOpenEMM2. 确保它符合您的预期,即TestSpringOpenEMM2。 I have seen plenty of examples of this (context root) not being as expected in Eclipse. 我已经看到很多这样的例子(上下文根)在Eclipse中没有像预期的那样。

The spring-boot-starter-parent is a great way to use Spring Boot, hence use a pom.xml like this: spring-boot-starter-parent是使用Spring Boot的好方法,因此使用像这样的pom.xml

<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. 默认情况下,使用嵌入式tomcat容器进行spring boot,只需运行main方法即可。 Read more on this topic on Spring Boot's Doc Spring Boot的Doc上阅读有关此主题的更多信息

Maybe you can use @RestController not @Controller . 也许你可以使用@RestController而不是@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. 对于REST api,如果我使用@Controller ,请求将导致404找不到错误,但在我将其更改为@RestController ,请求正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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