一:SpringBoot是什么

springboot是对spring的缺点进行改善和优化,约定大于配置  开箱即用  没有代码生成 也无需xml 文件配置   可以修改属性值来满足需求

1) Spring Boot使编码变简单

2) Spring Boot使配置变简单

3) Spring Boot使部署变简单

4) Spring Boot使监控变简单

二:创建第一个SpringBoot工程

 1、点击File--->New--->Project...

 

2、输入MAVEN,组名、包名等相关参数

 

3、选择SpringBoot版本,选择项目需要依赖的相关骨架包

注意:有些版本此处显示的是SpringWeb是一样的

 

4、设置项目保存目录:

 

5、项目创建完成,工程主界面如下:

删除多余的这三个文件

 

6、项目说明

(1)、默认有个Demo001Application类,里面是spring boot的载入函数

(2)、resource目录下有个application.properties文件,这个是Spring boot的配置文件

(3)、test目录下有个测试类Demo001ApplicationTests,这个是spring boot的单元测试

(4)、pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.offcn</groupId>
<artifactId>firstdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>firstdemo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
</project>

 

注意观察

一个继承spring-boot-starter-parent,两个依赖,spring-boot-starter-web web项目依赖必须,spring-boot-starter-test spring boot项目单元测试依赖

 

注意,很多人配置的maven远程仓库地址,其中很多配置的阿里云maven镜像,在这会有找不到最新Springboot相关包的问题,请把远程仓库指向华为云:

<mirror>

    <id>huaweicloud</id>

    <mirrorOf>*</mirrorOf>

    <url>https://mirrors.huaweicloud.com/repository/maven/</url>

</mirror>

 

6、启动项目

找到如下文字,表明SpringBoot已经成功启动:

 

打开浏览器,输入地址:http://localhost:8080 ,出现如下画面

 

出现上图404错误是正常的,因为我们什么都没写。

7、编写HelloController

package com.offcn.demo.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

 

@Controller

public class HelloController {

 

@RequestMapping(value="/hello",method=RequestMethod.GET)

@ResponseBody

public String sayHello() {

return "hello spring Boot!";

}

}

 

注意HelloController所在包,必须在com.offcn.demo包,或者子包下面。

重启发现刚才写的hello已经映射出来了

访问http://localhost:8080/hello

 

 

 

 

 

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/wangju/p/11795767.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!