SpringBoot笔记之Hello World
来源:互联网 发布:up安全卫士炒股软件 编辑:程序博客网 时间:2024/05/16 09:05
SpringBoot作为微服务的入门级程序,其重要性毋庸置疑,最近工作较少,偷偷的学习了下SpringBoot,这是一个Spring Boot最简单的示例
环境:根据官方文档推荐,这里采用JDK 1.8 , 项目构建工具使用maven 3.5。这里脱离了IDE环境,直接创建一个项目文件夹
在项目文件夹下新建一个pom.xml
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>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 指明编译源代码时使用的字符编码,maven编译的时候默认使用的GBK编码, 通过project.build.sourceEncoding属性设置字符编码,告诉maven这个项目使用UTF-8来编译 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- Inherit defaults from Spring Boot --> <!-- 提供了Maven默认配置--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.0.BUILD-SNAPSHOT</version> </parent> <!-- Add typical dependencies for a web application --> <!-- 支持web--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- Add Spring repositories --> <!-- (you don't need this if you are using a .RELEASE version) --> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories></project>
在项目文件夹下依次创建src/main/java文件夹,在java文件夹下创建Example.java文件
import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@RestController@EnableAutoConfigurationpublic class Example{ @RequestMapping("/") public String home(){ return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); }}
@RestController 以字符串的形式渲染结果,并直接返回给调用者。
@EnableAutoConfiguration 这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于 spring-boot-starter-web 添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地 对Spring进行设置。
Main方法通过调用run,将业务委托给SpringApplication类,SpringApplication启动应用并将应用加载到被自动配置的Tomcat容器中。将Example.class传递给SpringApplication,指定谁是主要的Spring组件。此外,传递args数组,暴露命令行参数。
命令行进行项目文件夹根目录,输入mvn spring-boot:run启动项目
在浏览器输入localhost:8080/即可访问
资料来源:SpringBoot官方文档
0 0
- SpringBoot笔记之Hello World
- springboot之hello world搭建学习笔记
- SpringBoot学习之hello world
- springboot入门之hello world
- SpringBoot之Hello world篇
- SpringBoot之Hello World&热启动插件
- SpringBoot入门(一)之Hello World
- 一头扎进springboot之Hello World
- SpringBoot的 Hello World
- springboot开发 第一个案例之hello,world!
- gradle, springboot,cfx-- Hello World
- SpringBoot入门:Hello World(一)
- SpringBoot框架搭建 Hello World
- SpringBoot入门系列:Hello World
- SpringBoot入门系列:Hello World
- QT学习笔记之零 Hello World
- QT学习笔记之零 Hello World
- C# 学习笔记之 Hello World
- Test
- Java编程原则简述
- js基本知识比较系列四:表单和表格
- 堆排的实现
- 第一篇立个flag
- SpringBoot笔记之Hello World
- 20170428 C指针的理解
- fread fwrite之后文件指针位置
- MySQL
- 多标记线段树处理法(洛谷3373,codevs4927)
- IDEA安装scala插件 以及 创建 scala工程
- Android开源项目-gnucash的本地环境配置
- QT——QChar
- (翻译整理)如何简单、高效地使用位操作解决问题