什么开发语言、框架的旅程都是从知道->了解->熟悉->深入。了解从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等信息
创建项目
右键->new->Other... (或者ctrl+n)




这是工程的开始,项目的后续DEMO都在这个bhparent父工程中进行。
引入parent
当前最新版本的spring boot 是2.0.2
在父pom.xml中加入parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
加入一些子项目中必然会需要的组件。最终的pom.xml
<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.biboheart.demos</groupId>
<artifactId>bhparent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>bhparent</name>
<description>biboheart spring boot demos</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
保存后等待组件下载完成。
创建模块





pom.xml
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.biboheart.demos</groupId>
<artifactId>bhparent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>helloworld</artifactId>
<name>helloworld</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
</dependencies>
</project>
创建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 = "/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服务器的开发(只是功能有点简单)
网友评论