springBoot 随笔(一)

服务化的世界,越来越多应用拆分为微服务,有些为了业务而拆,也有为了技术而拆,也有什么都不知道就瞎拆的,反正就是要微服务。

以下为一个认识springBoot的简单过程

1/eclipse 新建 maven项目,pom文件添加依赖
<!-- 引用父工程,里面包含一些基础的类库引用,当然也可以使用自定义 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

    <dependencies>
              <!--  web组件 提供web相关类库支持 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
               <!--  前台模板组件 thymeleaf前台渲染-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
                       <!--  maven运行插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>    

2 / 新建资源文件夹 src/main/resources 

    新建配置文件 application.properties  ,新增配置

#设置端口 
server.port=8081

3/项目根目录设置启动类

package com.hux.stu.dydatasorce;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

4/启动方式   直接main方法启动 run as

启动成功 
Tomcat started on port(s): 8081 (http)
 Started App in 3.461 seconds (JVM running for 3.789)

5/此应用开始发布服务

package com.hux.stu.dydatasorce.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/** rest请求 响应字符串 */
@RestController
public class AppController {
    /**
     * 带输入参数设置方法
     * @param pathParam
     * @return
     */
    @RequestMapping("hello/{pathParam}")
    public String MyDemoInfo(@PathVariable("pathParam")String pathParam) {
        
        return "测试服务发布"+pathParam;
    }
    
}

6/访问 http://127.0.0.1:8081/hello/hux 会正常响应数据: 测试服务发布hux

到此一个简单的springBoot发布的服务完成。是不是跟以前的服务简单许多了

原文地址:https://www.cnblogs.com/huxdiy/p/10787691.html