SpringBoot2入门

需求:浏览器url发送localhost:8888/hello ,响应 hello,Spring boot 2!
1.创建Maven工程

2.在pom文件中引入依赖

    <!--    继承父包-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
    </parent>

    <!--    web启动器-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

3.创建主程序
 1 /**
 2  * 主程序类
 3  * @SpringBootApplication:这是一个springboot应用
 4  */
 5 @SpringBootApplication
 6 public class MainApplication {
 7     public static void main(String[] args) {
 8         SpringApplication.run(MainApplication.class, args);
 9     }
10 }


4.编写业务
//@ResponseBody 方法直接返回字符串给浏览器
//@Controller
//  @RestController  =  @ResponseBody + @Controller
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String handle01() {
        return "hello,Spring boot 2!";
    }
}

5.运行
直接运行主程序的main方法即可

6.简化配置
application.properties
server.port=8888

7.简化部署
1.pom文件中引入maven的打包插件
<!--  maven打jar包的插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2.使用maven,进行clean和package,生成jar包,直接在目标服务器执行即可

 
posted @ 2021-03-10 19:54  dog_IT  阅读(111)  评论(0编辑  收藏  举报