英文:
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("/cghs")
public class HcoRestController {
@Autowired
private hcoService hcoSrvc;
@GetMapping("/cghsCity")
public List<CghsCity> 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("profileName"),
it needs to be passed in.
With the fat jar it was easy with the command line parameter --spring.profiles=<name>
When deployed in an external Tomcat that external tomcat has to to be modified (that is catalina.properties requires a -Dspring.profile=
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论