Spring Boot (1) 快速入门

本文介绍如何使用Spring Boot 2.1.0版本创建并运行一个简单的Hello World项目。从搭建Maven项目到配置POM文件,再到编写主程序和控制器,最后通过浏览器访问应用,完整展示Spring Boot的快速开发流程。

2.1.0.RELEASE】官方参考文档:

https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/

1、快速入门

新建一个maven项目spring-boot-helloworld

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>spring-boot-helloword</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-helloword</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.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</artifactId>
		</dependency>

	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
 	</dependency>

	</dependencies>

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


</project>

spring-boot-starter: 核心模板

spring-boot-starter-test: 测试模块

spring-boot-starter-web : web相关模块

 

主程序Application:

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

编写控制器HelloWordController:

@RestController
public class HelloWordController {
	
    @GetMapping("/")
    public String hello() {
        return "Hello World";
    }

}

@RestController注解相当于@ResponseBody + @Controller

写到这里,一个简单的spring-boot例子就完成了

运行main方法

浏览器访问 http://localhost:8080/,可以看到输出的结果Hello World

Github源码 https://github.com/johnwongz/spring-boot-demo

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值