2016-03-17 58 views
2

我已經使用Spring引導創建了一個REST API。我使用Gradle作爲我的構建工具。我已經使用嵌入式容器測試了應用程序,並且它工作正常。我可以通過localhost:8080/foo/bar訪問我的應用程序。在tomcat中部署後無法訪問spring引導應用程序

現在我已經部署它到tomcat沒有任何錯誤。

Mar 17, 2016 1:58:47 PM org.apache.catalina.startup.HostConfig deployWAR 
INFO: Deploying web application archive /usr/local/apache-tomcat-7.0.65/webapps/foo.war 
Mar 17, 2016 1:58:48 PM org.apache.catalina.startup.TldConfig execute 
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 
Mar 17, 2016 1:58:48 PM org.apache.catalina.startup.HostConfig deployWAR 
INFO: Deployment of web application archive /usr/local/apache-tomcat-7.0.65/webapps/foo.war has finished in 623 ms 

但我現在無法訪問應用程序在localhost:8080/foo/bar。我可以看到,tomcat運行在localhost:8080

這裏是build.gradle文件。

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE") 
    } 
} 

apply plugin: 'java' 
apply plugin: 'eclipse' 
apply plugin: 'spring-boot' 
apply plugin: 'war' 

war { 
    baseName = 'foo' 
} 

repositories { 
    mavenCentral() 
} 

sourceCompatibility = 1.8 
targetCompatibility = 1.8 

configurations{ 
    providedRuntime 
} 

dependencies { 
    compile 'org.springframework.boot:spring-boot-starter-web' 
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat") 
    compile 'com.google.code.gson:gson:2.6.2' 
    compile 'org.springframework:spring-webmvc:4.2.5.RELEASE' 
    compile 'org.json:json:20151123' 

    providedRuntime 'javax.ws.rs:javax.ws.rs-api:2.0.1' 
    providedRuntime 'javax.servlet:javax.servlet-api:3.1.0' 
    providedRuntime 'javax.servlet:jstl:1.2' 

    testCompile("junit:junit") 
} 

task wrapper(type: Wrapper) { 
    gradleVersion = '2.11' 
} 

我有一個application.properties文件用下面的代碼

server.context-path=/foo 

,並在控制器我的請求映射設置爲

@CrossOrigin 
    @RequestMapping("/bar") 
    public String bar(){ 
     // Code 
    } 

我試了一下在此post提到,但也不起作用。

@SpringBootApplication 
public class FooApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(FooApplication.class, args); 
    } 

    // Used when deploying to a standalone servlet container 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(FooApplication.class); 
    } 
} 
+0

爲什麼不使用嵌入Tomcat和創建可執行的jar? – SMA

+0

@SMA我想讓這個應用程序在服務器上運行。我不知道是否應用程序是一個可執行的jar將在這種情況下有所幫助 – mahacoder

+0

@ ak31它和will將會更容易維護,因爲可執行jar已準備好作爲服務來安裝。 –

回答

5

要部署春季啓動應用程序,你必須有延伸org.springframework.boot.context.web.SpringBootServletInitializer和覆蓋org.springframework.boot.context.web.SpringBootServletInitializer.configure(SpringApplicationBuilder application)

所以當應用程序部署在JEE Servlet容器春季啓動應用程序將被部署並擔任適當的配置類。

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

Application.class是同一類如SpringApplication.run(Application.class, args);在主方法

REF:What about the Java EE Application Server?

相關問題