简体   繁体   English

如何在本地 tomcat 上运行 spring 启动 maven 项目

[英]How to run spring boot maven project on local tomcat

This is basically my first Spring boot project and im trying to run it on my local tomcat.这基本上是我的第一个 Spring 启动项目,我试图在我的本地 tomcat 上运行它。 So that I can use Postman for GET requests using RESTful api.这样我就可以将 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);
    }

}

Basically what I'd want to do is run an sh script or intellij configuration to build my maven project and deploy it to my localhost:8080基本上我想要做的是运行一个 sh 脚本或 intellij 配置来构建我的 maven 项目并将其部署到我的 localhost:8080

The above comments are correct if you have a local instance of Tomcat running.如果您有 Tomcat 的本地实例正在运行,则上述注释是正确的。 If you're looking to test, simply run:如果您要测试,只需运行:

mvn spring-boot:run

If you're using the Spring parent pom for dependencies, the plugin is configured.如果您使用 Spring 父 pom 作为依赖项,则已配置插件。 You'll need a Main function.您需要一个主 function。 Mine typically looks like:我的通常看起来像:

@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);                             
    }                                                                           
}

You don't have to run it in external tomcat to test using postman.您不必在外部 tomcat 中运行它即可使用 postman 进行测试。 You can use boot dash board in spring sts or equivalent in intellij.您可以在 spring sts 或 intellij 中的等效项中使用引导仪表板。 If you still want to use external tomcat, then you need to package as war instead of embedded jar.如果你还想使用外置tomcat,那么你需要package作为war而不是嵌入式jar。 Look at his article for converting jar to war boot war deploy查看他的文章,将 jar 转换为战启动战部署

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过添加 Spring 引导依赖项在没有 Tomcat 服务器的本地计算机上运行 Spring 项目 - How to run Spring project on my local machine without Tomcat server by adding Spring Boot dependencies 在 Intellij 上运行但不在 Tomcat 上运行的 Spring Boot 2 Maven 项目 - Spring Boot 2 Maven project running on Intellij but not on Tomcat 从Maven或IDE运行Spring引导项目 - Run Spring boot project from Maven or IDE 如何使用Tomcat 8 + Spring Boot + Maven - How to use Tomcat 8 + Spring Boot + Maven 如何在VSCode中运行Spring Boot Maven项目以及如何配置Spring Boot Web应用程序的基本URL - How to run a Spring Boot maven project in VSCode and how to config the base url of a spring boot web application Maven Spring Boot插件:如何从另一个项目运行spring boot - Maven Spring Boot Plugin : how to run spring boot from another project 如何在Eclipse tomcat中运行spring boot app? - How to run spring boot app in Eclipse tomcat? 崇高设置,以运行带有tomcat 7本地主机的Maven项目 - sublime set up, to run a maven project with a tomcat 7 local host Maven/Spring 启动项目——如何跳过@SpringBootTest - Maven / Spring boot project - how to skip @SpringBootTest 如何创建多模块 Spring Boot Maven 项目? - How to create Multimoduling Spring Boot Maven project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM