繁体   English   中英

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

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

这基本上是我的第一个 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 的本地实例正在运行,则上述注释是正确的。 如果您要测试,只需运行:

mvn spring-boot:run

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

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

您不必在外部 tomcat 中运行它即可使用 postman 进行测试。 您可以在 spring sts 或 intellij 中的等效项中使用引导仪表板。 如果你还想使用外置tomcat,那么你需要package作为war而不是嵌入式jar。 查看他的文章,将 jar 转换为战启动战部署

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM