博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot 快速入门(一)
阅读量:4115 次
发布时间:2019-05-25

本文共 1728 字,大约阅读时间需要 5 分钟。

代码实现

一、创建Maven工程

使用IDEA工具创建一个maven工程,该工程为普通工厂即可

二、添加SpringBoot的起步依赖

SpringBoot要求,项目要继承SpringBoot的起步依赖spring-boot-starter-parent

org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE

SpringBoot要集成SpringMVC进行Controller的开发,所以项目要导入web的启动依赖

org.springframework.boot
spring-boot-starter-web

三、 编写SpringBoot引导类

要通过SpringBoot提供的引导类起步SpringBoot才可以进行访问

package com.musk;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;// 声明该类是一个 SpringBoot引导类@SpringBootApplicationpublic class MySpringBootApplication {
// main 是java程序的入口 public static void main(String[] args) {
// run 方法: 表示要运行 springboot 的引导类,run参数就是SpringBoot引导类的字节码对象 SpringApplication.run(MySpringBootApplication.class); }}

四、编写Controller

在引导类MySpringBootApplication同级包或者子级包中创建QuickStartController

package com.musk.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * @Author Cmushi * @create 2020/4/13 16:29 */@Controllerpublic class QuickController {
@RequestMapping("/quick") @ResponseBody public String quick(){
return " 学习 springboot"; }}

五、测试

执行SpringBoot起步类的主方法,控制台打印日志如下:

在这里插入图片描述在这里插入图片描述
通过日志发现,Tomcat started on port(s): 8080 (http) with context path ‘’
tomcat已经起步,端口监听8080,web应用的虚拟工程名称为空 打开浏览器访问url地址为:http://localhost:8080/quick

在这里插入图片描述

快速入门完毕!

配置服务器端口

此外,经过源码查找,如图:

在这里插入图片描述
其中,prefix = “server” 表示SpringBoot配置文件中的前缀,SpringBoot会将配置文件中以server开始的属性映射到该类的字段中。

我们可以配置服务器的端口,如图:

在这里插入图片描述
我们不仅配置了服务器端口,还增加了当前Web应用的名称
在这里插入图片描述

转载地址:http://aswpi.baihongyu.com/

你可能感兴趣的文章
从Executor接口设计看设计模式之最少知识法则
查看>>
OKhttp之Call接口
查看>>
application/x-www-form-urlencoded、multipart/form-data、text/plain
查看>>
关于Content-Length
查看>>
WebRequest post读取源码
查看>>
使用TcpClient可避免HttpWebRequest的常见错误
查看>>
EntityFramework 学习之一 —— 模型概述与环境搭建 .
查看>>
C# 发HTTP请求
查看>>
启动 LocalDB 和连接到 LocalDB
查看>>
Palindrome Number --回文整数
查看>>
Reverse Integer--反转整数
查看>>
Container With Most Water --装最多水的容器(重)
查看>>
Longest Common Prefix -最长公共前缀
查看>>
Letter Combinations of a Phone Number
查看>>
Single Number II --出现一次的数(重)
查看>>
Valid Parentheses --括号匹配
查看>>
Remove Element--原地移除重复元素
查看>>
Remove Duplicates from Sorted Array--从有序数组中移除重复元素
查看>>
Count and Say
查看>>
Gas Station
查看>>