第一个boot项目

一、打开网址https://start.spring.io/ 进去springboot官网,根据自己实际情况选择所需组件,点击生成。

二、导入maven项目,但是pom.xml报Line1未知错误,检查完毕发现是版本问题,

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

改成<version>2.0.0.RELEASE</version>就不会报错或者 在pom.xml 文件中的 properties 加入maven jar 插件的版本号,如下所示:

<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>因2.1.5.RELEASE 升级到了3.1.2 造成的问题。

可以发现错误已经消失

三、springboot默认会生成启动入口

package com.bootdemo.bootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BootdemoApplication {

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

}

修改为pom类型项目

        <groupId>com.bootdemo</groupId>
    <artifactId>bootdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>bootdemo</name>
    <description>微服务总工程</description>

创建子工程module,创建后在pom.xml中会有

<modules>
        <module>spring-application</module>
    </modules>

添加启动类,添加pom依赖

扫描二维码关注公众号,回复: 6587024 查看本文章
     <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

四、启动boot项目,默认8080端口,访问得到以下信息

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Jun 23 23:37:59 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

 修改启动类,SpringApplicationBuilder方式,更改启动端口为8090,

server.port=0 代表随机向操作系统要一个空闲端口,一般单元测试时候用
//SpringApplication.run(BootdemoApplication.class, args);
new SpringApplicationBuilder(BootdemoApplication.class)//Fluent Api
            .properties("server.port=8090")
            .run(args);

若用SpringApplication实现,则比较麻烦,为

SpringApplication springApplication = new SpringApplication(BootdemoApplication.class);
        HashMap<String, Object> propertis = new LinkedHashMap<>();
        propertis.put("server.port", 8090);
        springApplication.setDefaultProperties(propertis);
        springApplication.run(args);

 

猜你喜欢

转载自www.cnblogs.com/flgb/p/11074928.html