一、spring boot 2.x hello world
1、创建maven项目,结构如下:
2、在pom中引入相关依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ldy</groupId>
<artifactId>boot_v2_hello</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>boot_v2_hello</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、创建入口类:BootV2App.java
package com.ldy.bootv2.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BootV2App {
public static void main(String[] args) {
SpringApplication.run(BootV2App.class, args);
}
}
4、编写hello world 接口:HelloController.java
package com.ldy.bootv2.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String index(String name) {
return "hello " + name;
}
}
5、启动项目:运行BootV2App.java中的main方法
鼠标右键- ->Run As --> Java Application,启动成功后控制台显示信息如下:
6、测试hello world 接口:打开浏览器,在地址栏输入:127.0.0.1:8080/hello?name=ldy,如图
说明:可在官方网站或者利用开发工具(eclipse、ieda)生成spring boot项目,官方网站地址:https://start.spring.io