2015-10-23 72 views
1

首先请注意,我是一个初学者。我开始了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; 
    } 
} 

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("/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> 
+0

是'Application.java'和'GreetingController.java'在同一个包中?如果不尝试将它们放在同一个包中......这是'@ SpringBootApplication'注释的一个限制,要解决这个问题,您可以尝试用'@ Configuration @ EnableAutoConfiguration @ ComponentScan'替换此注释... –

回答

0

如果您将应用程序部署到一个独立的Tomcat实例,它是不是部署为根应用程序,你必须在你的URL添加应用程序的名称。您可以检查Tomcat日志中的确切名称。如果您将项目命名为“helloworld”,那么Eclipse通常会使用该名称作为应用程序名称部署项目,因此您应该使用URL调用您的应用程序http://localhost:8080/helloworld/greeting

0

除非您确实需要将应用程序部署到外部Tomcat服务器,您可以直接运行应用程序的主要方法来解决您的问题。 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); 
    } 

} 

这是覆盖在春季启动参考文档的traditional deployment section

相关问题