Spring Boot Hello World

简介

Java Web 开发涉及的技术比较复杂,每一个框架都是独立的,虽然 Spring 实现了通过配置Xml的方式去整合各个框架,但是Xml的配置还是比较复杂的。
Spring Boot 实现了快速整合各个框架;
Spring Boot 采用“约定优于配置”的思想,简化配置,快速整合第三方框架(基于Maven的继承和依赖的特性),内置的http服务器等等,方便开发人员快速开发;

构建第一个Spring Boot 程序

开发工具:

  • IntelliJ IDEA
  • jdk 1.8
  • Maven 3.5

目录结构

在这里插入图片描述

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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.lgs.hello</groupId>
	<artifactId>spring-boot-hello</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-hello</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</artifactId>
		</dependency>

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

		<!--Web 依赖模块-->
		<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 启动类

@SpringBootApplication
public class HelloApplication {

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

}
  • @SpringBootApplication封装了以下三个注解:
    • @SpringBootConfiguration:配置类注解
    • @EnableAutoConfiguration:启动自动配置注解
    • @ComponentScan:组件扫描注解
注意:

@SpringBootApplication 自动扫描包的范围:
Application类的同级包和子包。

编写 API 接口

@RestController
public class HelloController {

    @GetMapping(value = "/")
    public String hello(){
        return "Hello World";
    }
}
  • @RestController 封装了以下两个注解 :
    • @Controller:将类标记为 spring mvc 的 controller 对象。
    • @ResponseBody:返回的数据是json格式的。
  • @GetMapping:以get请求访问。

启动

运行 HelloApplication 中的 main方法

浏览器中访问:http://localhost:8080/

源码:https://git.dev.tencent.com/lgs_/spring-boot-parent.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值