spring|spring boot 实例之 初体验
什么开发语言、框架的旅程都是从知道->了解->熟悉->深入。了解从helloworld开始。这是一个spring boot 框架的helloworld。体验一下spring boot是怎么使用的。
开发环境搭建
恩。总得先把编辑、编译的环境搭建起来。用记事本做开发,有点太……高调了。还是低调行事。
采用eclipse,感觉用它顺手。eclipse安装就不在这里详细描述了。
https://www.eclipse.org/downloads/eclipse-packages/
在这里选择一下版本,下载,解压,运行。
先建立一个工程目录,spring boot的DEMO都放在这个目录中。
E:\Source\javademos\springboot
启动eclipse
文章图片
选择工程目录
进入后配置好JDK,MAVEN等信息
创建项目
【spring|spring boot 实例之 初体验】右键->new->Other... (或者ctrl+n)
文章图片
选择maven
文章图片
创建空项目
文章图片
编辑项目信息
文章图片
完成工程项目的配置
这是工程的开始,项目的后续DEMO都在这个bhparent父工程中进行。
引入parent
当前最新版本的spring boot 是2.0.2
在父pom.xml中加入parent
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
加入一些子项目中必然会需要的组件。最终的pom.xml
4.0.0 com.biboheart.demos
bhparent
1.0.0-SNAPSHOT pom
bhparent
biboheart spring boot demos org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
junit
junit
test
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.apache.httpcomponents
httpclient
org.projectlombok
lombok
provided
org.springframework.boot
spring-boot-maven-plugin
保存后等待组件下载完成。
创建模块

文章图片
创建模块

文章图片
模块名称

文章图片
image.png

文章图片
配置信息

文章图片
完成后目录结构
pom.xml
4.0.0 com.biboheart.demos
bhparent
1.0.0-SNAPSHOT
helloworld
helloworld
http://maven.apache.org UTF-8
创建HelloworldApplication
package com.biboheart.demos.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloworldApplication {public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}
HelloworldApplication.java右键,Debug as -> Java application 执行java程序
结果:

文章图片
项目运行结果
看…… 服务已经成功启动,只是还没有放入页面。到少是正常启动了一个WEB服务,端口8080.
还可以看到,内嵌了tomcat。
到目前为止就配置了一些pom.xml文件,引入一些组件。然后建立一个HelloworldApplication.java类文件。写了几行代码。就可以通过运行java程序一样执行了一个服务。恩,瞬间觉得做个服务太简单了。
当然,既然是服务,总是要有api供外部调用的呢。
改造下HelloworldApplication
package com.biboheart.demos.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloworldApplication {public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}@RequestMapping(value = "https://www.it610.com/hello")
public String load(String name) {
return "hello " + name;
}
}
再次运行java application
在浏览器中输入:http://localhost:8080/hello?name=biboheart

文章图片
访问结果
一个可以传参的API就这么产生了。
今天就到这里了。
总结下:
- 创建一个maven父工程(pom项目)
- 创建一个maven子工程(模块)
- 在pom.xml中引入spring boot的包
- 创建一个类,写上必要的几行代码
- 完成一个API服务器的开发(只是功能有点简单)
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 2018-07-09|2018-07-09 Spring 的DBCP,c3p0
- LSTM网络层详解及其应用实例
- spring|spring boot项目启动websocket
- Spring|Spring Boot 整合 Activiti6.0.0
- Spring集成|Spring集成 Mina
- springboot使用redis缓存
- Spring|Spring 框架之 AOP 原理剖析已经出炉!!!预定的童鞋可以识别下发二维码去看了