2016-11-21 160 views
0

我有一個在我的本地機器上運行的Spring Boot Rest服務項目。當我運行該應用程序爲「春啓動應用程序」無法在Tomcat上訪問已部署的WAR文件

我可以去訪問其他服務

http://127.0.0.1:8080/persons/all

和它應該返回JSON。

我將pom.xml打包改成了war,然後我在Spring工具套裝中以Run as - > Maven構建了一場戰爭。它創建一個戰爭文件。當我上傳戰爭檔案http://myserverip:8080/manager/我沒有得到任何錯誤,它顯示在應用程序

上傳它後,戰爭文件名稱是「myapp-0.0.1-SNAPSHOT.war」,我試圖訪問它

http://myserverip:8080/myapp-0.0.1-SNAPSHOT/persons/all

但我得到一個404錯誤

我在做什麼錯在這裏?

我的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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>com.mycompany</groupId> 
    <artifactId>myapp</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <name>myapp</name> 
    <description>myappapi</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.2.RELEASE</version> 
     <relativePath /> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
      <scope>provided</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 


    <packaging>war</packaging> 
</project> 

當我構建應用程序,我得到以下日誌

[INFO] Scanning for projects... 
[INFO]                   
[INFO] ------------------------------------------------------------------------ 
[INFO] Building myapp 0.0.1-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myapp --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 1 resource 
[INFO] Copying 0 resource 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapp --- 
[INFO] Nothing to compile - all classes are up to date 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myapp --- 
[INFO] Not copying test resources 
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myapp --- 
[INFO] Not compiling test sources 
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ myapp --- 
[INFO] Tests are skipped. 
[INFO] 
[INFO] --- maven-war-plugin:2.6:war (default-war) @ myapp --- 
[INFO] Packaging webapp 
[INFO] Assembling webapp [myapp] in [C:\Users\me\Documents\workspace-sts-3.8.2.RELEASE\myapp\target\myapp-0.0.1-SNAPSHOT] 
[INFO] Processing war project 
[INFO] Webapp assembled in [586 msecs] 
[INFO] Building war: C:\Users\me\Documents\workspace-sts-3.8.2.RELEASE\myapp\target\myapp-0.0.1-SNAPSHOT.war 
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) @ myapp --- 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 3.300 s 
[INFO] Finished at: 2016-11-21T10:12:03-05:00 
[INFO] Final Memory: 20M/243M 
[INFO] ------------------------------------------------------------------------ 
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist. 

我MyappApplication.java

package com.mycompany; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
@EnableAutoConfiguration 
@ComponentScan 
public class MyappApplication { 
    public static void main(String[] args) { 
     SpringApplication.run(MyappApplication.class, args); 
    } 
} 
+0

您可以在pom.xml中將您的buid名稱更改爲myapp。檢查服務器啓動時是否註冊了「/ persons/all」。如果沒有,則會顯示404錯誤,因爲您的控制器未正確註冊。 – Lucky

+0

@Lucky,但應用程序在我的本地機器上正常工作。它有可能會在Spring本地註冊,但在Tomcat上部署war文件時不能註冊? – Arya

+0

是的,請檢查您的tomcat服務器日誌。 Spring引導使用嵌入式tomcat。您正在部署到一個單獨的tomcat實例中。兩者都不同。 – Lucky

回答

1

延長SpringBootServletInitializer參見:Packaging executable jar and war files

正如評論中所提到的,傳統上可以通過將war部署到獨立的tomcat實例來部署spring引導應用程序。您需要對MyappApplication.java課程進行一些更改,該課程有main()方法。

  • 讓您MyappApplication延長SpringBootServletInitializer並覆蓋其configure()方法,

    @SpringBootApplication 
    public class MyappApplication extends SpringBootServletInitializer { 
    
        @Override 
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
         return application.sources(Application.class); 
        } 
    
        public static void main(String[] args) throws Exception { 
         SpringApplication.run(MyappApplication.class, args); 
        } 
    
    } 
    
  • 下一個步驟是有包裝的戰爭,而不是你已經有默認的罐子。

  • 最後在您的pom.xml中將嵌入式tomcat容器的作用域更改爲provided,以便在部署到單獨的實例時不會發生衝突。

+0

謝謝您現在可以使用它: )但SpringBootServletInitializer顯示爲折舊。 – Arya

+0

@Arya您可能會使用'org.springframework.boot.context.web.SpringBootServletInitializer'來代替使用'org.springframework.boot.web.support.SpringBootServletInitializer'。 – Lucky

0

清單改變彈簧啓動可執行的JAR文件到一個可部署的戰爭。

  1. 更改包裝的pom.xml到 「戰爭」,從 「罐子」

  2. 正如kuhajeyan提到,標誌着嵌入式tomcat的依賴性,在pom.xml提供。

    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-tomcat</artifactId> 
        <scope>provided</scope> 
    

  3. 讓你的主彈簧引導應用類這樣

    @SpringBootApplication 
    public class Application extends SpringBootServletInitializer { 
    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
    return application.sources(Application.class); 
    } 
    } 
    

From the documentations