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

spring boot 实例之 初体验_第1张图片
选择工程目录

进入后配置好JDK,MAVEN等信息

创建项目

右键->new->Other... (或者ctrl+n)


spring boot 实例之 初体验_第2张图片
选择maven

spring boot 实例之 初体验_第3张图片
创建空项目

spring boot 实例之 初体验_第4张图片
编辑项目信息

spring boot 实例之 初体验_第5张图片
完成工程项目的配置

这是工程的开始,项目的后续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
            
        
    

保存后等待组件下载完成。

创建模块

spring boot 实例之 初体验_第6张图片
创建模块

spring boot 实例之 初体验_第7张图片
模块名称

spring boot 实例之 初体验_第8张图片
image.png

spring boot 实例之 初体验_第9张图片
配置信息

spring boot 实例之 初体验_第10张图片
完成后目录结构

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程序
结果:


spring boot 实例之 初体验_第11张图片
项目运行结果

看…… 服务已经成功启动,只是还没有放入页面。到少是正常启动了一个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

spring boot 实例之 初体验_第12张图片
访问结果

一个可以传参的API就这么产生了。
今天就到这里了。
总结下:

  1. 创建一个maven父工程(pom项目)
  2. 创建一个maven子工程(模块)
  3. 在pom.xml中引入spring boot的包
  4. 创建一个类,写上必要的几行代码
  5. 完成一个API服务器的开发(只是功能有点简单)

你可能感兴趣的:(spring boot 实例之 初体验)