百里登风

导航

1、SpringBoot入门

一、springboot简介

 

 

 

 

 

 

 

 

二、微服务

一个应用应该是一组小型服务,可以通过HTTP的方式进行互通

 

单体应用:ALL IN ONE

 

每一个功能元素最终都是一个可独立替换和独立升级的软件单元

 

 

 

 

 

 

三、开发环境

 

 

 

 

 四、Spring Boot HelloWorld

一个功能:

浏览器发送hello请求,服务器接受请求并处理,响应Hello World字符串;

 

1、创建一个maven工程;(jar)

 

 

 

 

 

 

 

 

2、导入spring boot相关依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

 

 

3、编写主程序

 

 

package com.gong;


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


/**
 *  @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {

    public static void main(String[] args) {

        // Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

 

 

4、编写相关的Controller、Service

 

 

 

 

package com.gong.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
   @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
       return "Hello World!!!!";
   }
}

 

 

5、主程序测试工作

启动主程序,我这里的8080端口被占用了,所以我把端口号修改了一下

修改端口号

 

 

 

 

启动主程序

 

 

 

在浏览器访问地址:http://localhost:8088/hello

 

 

 

6、简化部署
 <!-- 这个插件,可以将应用打包成一个可执行的jar包;-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

 

 

打包应用程序

 

 

 

 

这个是生成的jar包

 

 

五、Hello World探究

1、父项目
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

 

 

 

 

 

2、启动器

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

 

 

 

spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件
Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器

 

 

3、主程序类,入口类

/**
 *  @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {

    public static void main(String[] args) {

        // Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

 

 

@SpringBootApplication:  SpringBoot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的Main方法启动SpringBoot应用

 

 

六、使用Spring Initializer快速创建Spring Boot项目

 

 

 

 

 

 

 

 

 

 

 

导入单元测试模块

<!--Spring Boot进行单元测试的模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

 

 

创建controller层

 

 

package com.atguigu.springboot.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

//这个类的所有方法返回的数据直接写给浏览器,(如果是对象转为json数据)
/*@ResponseBody
@Controller*/
@RestController
public class HelloController {


    @RequestMapping("/hello")
    public String hello(){
        return "hello world quick!";
    }

    // RESTAPI的方式
}

 

 

主程序

 

 

package com.atguigu.springboot;

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

@SpringBootApplication
public class SpringBoot01HelloworldQuickApplication {

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

 

 

启动主程序

 

 

 

在浏览器访问  http://localhost:8081/hello

 

posted on 2021-06-10 16:42  百里登峰  阅读(104)  评论(0编辑  收藏  举报