【问题标题】:How to run spring boot maven project on local tomcat如何在本地tomcat上运行spring boot maven项目
【发布时间】:2020-10-30 14:37:49
【问题描述】:

这基本上是我的第一个 Spring 启动项目,我试图在我的本地 tomcat 上运行它。这样我就可以使用 Postman 使用 RESTful api 进行 GET 请求。

@RestController
@RequestMapping(value = "/shop")
public class ShopController {

    @Autowired
    ShopService shopService;

    @GetMapping(value = "/{id}")
    public @ResponseBody Shop getTestData(@PathVariable String id) {
        return shopService.getShopBasedOnId(id);
    }

}

基本上我想要做的是运行 sh 脚本或 intellij 配置来构建我的 maven 项目并将其部署到我的 localhost:8080

【问题讨论】:

  • 回答如何在本地tomcat上运行:直接打包成war放在/webapps/下,这样catalina就会爆掉。如果你想让你的 maven 做所有的事情,你需要在 pom.xml 中进行配置。
  • 答案是“不要”。 Spring Boot 消除了处理 Tomcat(或等效)的需要,这是多年来 Java Web 应用程序最头疼的问题。口号是“制造罐子,而不是战争。”

标签: java spring spring-boot maven tomcat


【解决方案1】:

如果您有本地 Tomcat 实例正在运行,则上述 cmets 是正确的。如果您要测试,只需运行:

mvn spring-boot:run

如果您使用 Spring 父 pom 作为依赖项,则该插件已配置。你需要一个 Main 函数。我的通常看起来像:

@SpringBootApplication                                                          
@NoArgsConstructor @ToString @Log4j2                                            
public class Launcher extends SpringBootServletInitializer {                                                                                           
    public static void main(String[] argv) throws Exception {                   
        SpringApplication application = new SpringApplication(Launcher.class);  
                                                                                
        application.run(argv);                                                  
    }                                                                           
                                                                                
    @Override                                                                   
    protected SpringApplicationBuilder configure(SpringApplicationBuilder appli\
cation) {                                                                       
        return application.sources(Launcher.class);                             
    }                                                                           
}

【讨论】:

  • SpringBootServletInitializer 需要作为传统的 webapp (war) 运行。 Spring Boot 默认使用嵌入式 tomcat。所以评论不正确。你的答案是正确的。
【解决方案2】:

您不必在外部 tomcat 中运行它来使用邮递员进行测试。您可以在 spring sts 中使用 boot dashboard 或在 intellij 中使用等效项。如果你还想使用外部tomcat,那么你需要打包成war而不是嵌入jar。看他的文章把jar转warboot war deploy

【讨论】:

    猜你喜欢
    • 2019-03-30
    • 1970-01-01
    • 2018-11-07
    • 2021-01-24
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2011-10-09
    相关资源
    最近更新 更多