SpringBoot学习Demo:hello world
一、IDEA 2020创建SpringBoot项目
1、创建步骤
2、项目目录结构
二、配置文件
1、配置文件application.yml
server:
port: 8080
servlet:
context-path: /helloworld
2、maven依赖文件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.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dashu</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloword</name>
<description>Demo helloWorld project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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>
</dependency>
<!--Lombok能以简单的注解形式来简化java代码,提高开发人员的开发效率。-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--Hutool是一个小而全的Java工具类库,
通过静态方法封装,降低相关API的学习成本,
提高工作效率,使Java拥有函数式语言般的优雅,
让Java语言也可以“甜甜的”。-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、代码
1、springboot启动类HelloworldApplication.java
package com.dashu.helloworld;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* <p>
* SpringBoot启动类
* </p>
*
* @package: com.dashu.helloworld
* @description: SpringBoot启动类
* @author: dashu
* @date: Created in 2020/11/19
* @copyright: Copyright (c)
* @version: V1.0
* @modified: dashu
*/
@SpringBootApplication
@Slf4j
public class HelloworldApplication {
public static void main(String[] args) throws UnknownHostException {
ConfigurableApplicationContext application = SpringApplication.run(HelloworldApplication.class, args);
Environment env = application.getEnvironment();
//获取本机ip
String ip = InetAddress.getLocalHost().getHostAddress();
//获取配置文件端口
String port = env.getProperty("server.port");
//获取配置文件项目启动路径
String path = env.getProperty("server.servlet.context-path");
//日志输出访问路径
log.info("\n----------------------------------------------------------\n\t" +
"Application helloword is running! Access URLs:\n\t" +
"URL: \t\thttp://"+ip+":" + port + path + "\n\t" +
"----------------------------------------------------------");
}
}
2、控制类HelloworldController.java
package com.dashu.helloworld.controller;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 控制类
* </p>
*
* @package: com.dashu.helloworld.controller
* @description: 访问控制类
* @author: dashu
* @date: Created in 2020/11/19
* @copyright: Copyright (c)
* @version: V1.0
* @modified: dashu
*/
@RestController
@Slf4j
public class HelloworldController {
/**
*
* Hello ${str}
*
* @param str:字符串参数
* @return java.lang.String
*/
@GetMapping("/hello")
public String helloWorld(@RequestParam(required = false, name = "str") String str){
//判断str是否为空
if(StrUtil.isBlank(str)){
str = "world";
}
//字符替换
str = StrUtil.format("Hello, {}!", str);
//日志输出
log.info("接口("+Thread.currentThread().getStackTrace()[1].getMethodName()+")--->返回值:"+str);
return str;
}
}
四、运行
1、项目启动
2、访问接口
五、诗词欣赏
将进酒·君不见黄河之水天上来
[唐] 李白
君不见黄河之水天上来,奔流到海不复回。
君不见高堂明镜悲白发,朝如青丝暮成雪。
人生得意须尽欢,莫使金樽空对月。
天生我材必有用,千金散尽还复来。
烹羊宰牛且为乐,会须一饮三百杯。
岑夫子,丹丘生,将进酒,杯莫停。
与君歌一曲,请君为我倾耳听。
钟鼓馔玉不足贵,但愿长醉不复醒。
古来圣贤皆寂寞,惟有饮者留其名。
陈王昔时宴平乐,斗酒十千恣欢谑。
主人何为言少钱,径须沽取对君酌。
五花马,千金裘,
呼儿将出换美酒,与尔同销万古愁。