Spring Boot Getting a second case

One: What is SpringBoot

springboot disadvantage is to improve and optimize the spring, is greater than the agreed-box configuration generates no code need not xml configuration file can be modified to meet the needs of property values

1) Spring Boot coding becomes simple

2) Spring Boot make configuration simple

3) Spring Boot the deployment made simple

4) Spring Boot monitor becomes so simple

Two: Create the first SpringBoot project

 1. Click on File ---> New ---> Project ...

 

2, input MAVEN, group name, package names, and other related parameters

 

3. Select SpringBoot version, select the item need to rely on the relevant skeleton package

Note: Some versions shown here is the same SpringWeb

 

4, set up to save the project directory:

 

5, the project is created, the main interface works as follows:

Remove the extra three files

 

6, Item Description

(1), there is a default Demo001Application class, which is a function spring boot loading

(2), there is a application.properties file resource directory, this is Spring boot configuration file

(3), there is a test class Demo001ApplicationTests directory test, the test unit is spring boot

(4), pom.xml file

<?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

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/wangju/p/11795767.html