设为首页 加入收藏

TOP

SpringBoot系列一:SpringBoot入门(一)
2018-11-21 14:08:43 】 浏览:340
Tags:SpringBoot 系列 入门

1 SpringBoot HelloWorld

功能:浏览器发送 sayHello 请求,服务器接受请求并处理,响应 Hello。

1.1 创建一个maven工程

<groupId>com.seagetech</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>1.0.0</version>

1.2 下载官方参考文档

在官网找到相应的下载地址(官网地址:https://spring.io/projects)或者直接使用如下地址下载:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/pdf/spring-boot-reference.pdf。文档名称 “spring-boot-reference.pdf”,版本2.10.RELEASE。

1.3 使用官方参考文档

Part II. Getting Started > 11. Developing Your First Spring Boot Application。

1.3.1 创建POM
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.0.RELEASE</version>
</parent>
1.3.2 添加 ClassPath 依赖
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

1.3.3 编写代码

创建一个主程序类,类名随便取。因为我们是做一个 HelloWorld 的项目,所有我们创建一个 HelloWorldApplication 类:

/**
 * @auther wangzb
 * @date 2018/11/9 21:32
 */
@Controller
@EnableAutoConfiguration
public class HelloWorldApplication {

    @RequestMapping(value = "/sayHello")
    @ResponseBody
    public String sayHello(String name){
        return "Hello," + name;
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}
1.3.4 运行例子

启动 main 方法后,查看 SpringBoot 启动日志:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

....... . . .
....... . . . (log output here)
....... . . .  

... Tomcat started on port(s): 8080 (http) with context path ''
... Started HelloWorldApplication in 3.577 seconds (JVM running for 4.446)

可以看到Tomcat的启动日志:

Tomcat started on port(s): 8080 (http) with context path ”

即,Tomcat 默认启动端口为 8080,项目根路径为 “”,所以在浏览器或者 Postman 中输入地址:http://localhost:8080/sayHello?name=wangzb,浏览器或Postman响应:

Hello,wangzb
1.3.5 创建一个可执行的 jar 文件

要创建一个可执行 jar,我们需要将 spring-boot-maven-plugin 添加到我们的 pom.xml 中。在dependencies部分下面插入以下几行:

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

接下来就可以打包,在 Idea 中点击 package,执行打包命令,之后我们在项目的 target 包下就可以看到刚打包好的 springboot-helloworld-1.0.0.jar 包。在包的路径下,打开 CMD 命令行,执行如下命令:

$ java -jar springboot-helloworld-1.0.0.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.536 seconds (JVM running for 2.864)

要退出应用程序,请按 Ctrl+c 或者直接关闭命令窗口,再次在浏览器中访问上面地址,发现链接已不能再访问,如果在 Linux 系统中,且要后台长期运行,可以在命令后加&符号,即:

java -jar springboot-helloworld-1.0.0.jar &

2 HelloWorld探究

2.1 使用IntelliJ IDEA快速创建SpringBoot项目

1) 新建项目

2) 左边菜单选择 Spring Ini

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java反转字符串的10种方法(代码.. 下一篇谈谈 Java 类加载机制

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目