idea 创建maven的springboot项目

一、创建项目:

1.创建一个新项目:

2.点击next

3.选择依赖:

   这儿选择为一个web项目

4.自定义项目名称和路径:

创建出来的项目的结构路径如下:

项目启动:

点击run运行

启动日志如下:

说明运行成功了。

访问localhost:8080

显示:

说明项目创建成功

二、写个controller测试一下

创建一个demoController:

demoController代码如下:

@RestController
public class DemoController {

    @RequestMapping("test")
    public String demoTest(){
        return "test";
    }
}

再次启动 DemoApplication,启动成功后,访问  http://localhost:8080/test

显示为test,表明controller创建成功

到这,创建项目及controller测试已经成功

因习惯,我喜欢用idea外置tomcat来启动项目

三、将项目改造为idea外置tomcat启动的项目

参考这篇文章:https://blog.csdn.net/afadasdas/article/details/99200543

1.在 pom.xml 设置:

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>springbootdemo</finalName>
    </build>

2. 移除对SpringBoot中的嵌入式Tomcat的依赖

即将上图所示的类删掉

并在pom文件中添加:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

3.修改启动类:

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DemoApplication.class);
    }

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

}

4.通过tomcat启动项目,发现启动时报错:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultValidator' defined in class path resource

网上查了一下,是因为springboot版本过高导致

手动将springboot版本降为1.5.9.RELEASE

pom文件中:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

最后启动成功

第三部分参考了外来文章,文章链接已给出,关于如何配置tomcat和tomcat启动,引用的文章做了很好的说明

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值