如何在外部Tomcat服务器上部署Spring Boot应用程序?

huangapple go评论71阅读模式
英文:

How to deploy spring boot app on external tomcat server?

问题

我有一个Spring Boot应用程序。它只有一些API。

它在嵌入式Tomcat服务器上运行良好。

现在我需要部署到我的外部Tomcat服务器。

所以我在pom文件中添加了打包为war的设置。

我尝试使用导出为war选项创建war文件,然后将这个war文件放入我的外部Tomcat的webapps文件夹,并尝试运行它。但是返回404状态。

War文件名称

CghsMobileApp.war

我的REST控制器

@RestController
@RequestMapping("/cghs")
public class HcoRestController {

    @Autowired
    private hcoService hcoSrvc;

    @GetMapping("/cghsCity")
    public List<CghsCity> getCghsCity() {
        return hcoSrvc.getCghsCity();
    }
}

我尝试访问的URL

http://localhost:8080/CghsMobileApp/cghs/cghsCity

我的API项目中没有HTML页面。我需要添加一个HTML页面才能让war文件正常工作吗?

我感到困惑。
任何帮助将不胜感激。

英文:

I have a spring boot app. It only has few api.

Its running fine on embedded tomcat server.

Now I need to deploy to my external tomcat server.

So I added packaging as war in pom file

I tried making a war using export as war option and put this war file inside my external tomcat webapps folder and tried running it. It failed with 404 status.

War file Name

CghsMobileApp.war

My rest controller

@RestController
@RequestMapping(&quot;/cghs&quot;)
public class HcoRestController {

	@Autowired
	private hcoService hcoSrvc;

	@GetMapping(&quot;/cghsCity&quot;)
	public List&lt;CghsCity&gt; getCghsCity() {
		return hcoSrvc.getCghsCity();
	}
}

URL I tried to hit

http://localhost:8080/CghsMobileApp/cghs/cghsCity

There is no html page inside my api project. Do I need to add one for war file to work.

I am lost here.
Any help will be appriciated.

答案1

得分: 1

根据Spring Boot规范,发现使用 @SpringBootApplication 注解的类必须扩展 SpringBootServletInitializer,以便进行外部服务器的WAR部署。

package gov.cghs.CghsMobileApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class CghsMobileAppApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(CghsMobileAppApplication.class, args);
    }
}
英文:

Looked at the spring boot specification, found out that in class annotated with @SpringBootApplication have to extend SpringBootServletInitializer, for external server war deployment.

package gov.cghs.CghsMobileApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class CghsMobileAppApplication extends SpringBootServletInitializer {

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

答案2

得分: 0

但不幸的是,仅仅这样做在使用Spring Profile时是不够的。如果你的任何类使用了@Profile("profileName"),它需要被传递。

使用fat jar 时,可以使用命令行参数--spring.profiles=<name>来轻松处理。

当部署在外部Tomcat时,需要修改外部Tomcat(即catalina.properties需要一个-Dspring.profile=)。

英文:

but unfortunately only this will not help when working with Spring Profile. If any of your classes are using @Profile(&quot;profileName&quot;), it needs to be passed in.

With the fat jar it was easy with the command line parameter --spring.profiles=&lt;name&gt;

When deployed in an external Tomcat that external tomcat has to to be modified (that is catalina.properties requires a -Dspring.profile=)

huangapple
  • 本文由 发表于 2020年8月7日 15:37:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63297295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定