为啥我无法访问我的控制器?
Posted
技术标签:
【中文标题】为啥我无法访问我的控制器?【英文标题】:Why can't I access my controller?为什么我无法访问我的控制器? 【发布时间】:2016-01-23 07:43:14 【问题描述】:首先请注意,我在这方面完全是初学者。我开始了 spring maven web 项目并编写了 3 个类(发布在下面)。 我把项目放在我的tomcat服务器上运行(来自eclipse),当我启动它时,我可以访问localhost:8080(显示tomcat默认页面),但是当我尝试访问localhost:8080/greeting时出现404错误,我究竟做错了什么?我也会发布我的 pom.xml
谢谢
问候:
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;
问候控制器:
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("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name)
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
应用:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application
public static void main(String[] args)
SpringApplication.run(Application.class, args);
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<artifactId>WebTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<!-- Generic properties -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Web -->
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<servlet.version>2.5</servlet.version>
<!-- Spring -->
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<!-- Other Web dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>$jstl.version</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>$servlet.version</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>$jsp.version</version>
<scope>provided</scope>
</dependency>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>$spring-framework.version</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>$slf4j.version</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>$logback.version</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>$hibernate.version</version>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>$spring-framework.version</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>$junit.version</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
Application.java
和 GreetingController.java
在同一个包中吗?如果不尝试将它们放在同一个包中...这是@SpringBootApplication
注释的限制,要解决此问题,您可以尝试将此注释替换为@Configuration@EnableAutoConfiguration @ComponentScan
...
【参考方案1】:
除非您确实需要将应用程序部署到外部 Tomcat 服务器,否则您可以通过直接运行应用程序的 main 方法来解决您的问题。 Spring Boot 将为您启动一个嵌入式 Tomcat 实例。
如果您需要能够将应用程序部署到外部 Tomcat 服务器,那么您需要稍微更改您的 Application
类,以便它能够正确启动。您需要扩展SpringBootServletInitializer
并覆盖configure
方法:
@SpringBootApplication
public class Application extends SpringBootServletInitializer
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
return application.sources(Application.class);
public static void main(String[] args) throws Exception
SpringApplication.run(Application.class, args);
这在 Spring Boot 参考文档的 traditional deployment section 中有介绍。
【讨论】:
【参考方案2】:如果您将应用程序部署到独立的 Tomcat 实例中并且它没有部署为 ROOT 应用程序,则必须在 URL 中添加应用程序的名称。您可以检查 Tomcat 日志以获取确切名称。如果您将项目命名为“helloworld”,那么 Eclipse 通常会使用该名称作为应用程序名称来部署项目,因此您应该使用 URL http://localhost:8080/helloworld/greeting
调用您的应用程序
【讨论】:
以上是关于为啥我无法访问我的控制器?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的电脑里有一个文件夹无法访问,且大小为0,而且无法删除?
为啥我无法在我的 React 应用程序中访问 Konva Canvas 属性?
C++:为啥我的 DerivedClass 的构造函数无法访问 BaseClass 的受保护字段?