geek的技术日志,记录每一次技术思考的闪光点。

Spring Cloud学习第二课:Spring boot学习

创建一个restful接口的Spring boot项目

如果使用SPRING INITIALIZR之类的插件的话,很容易就会创建出来了,不过大家可能就不容易理解了,而且,插件不是哪里都有的,配置好插件也是个麻烦的事情。为了方便理解,我从最基础的开始讲。不利用任何插件创建Spring boot项目。

step1 新建一个maven项目

这个基本上所有的ide都有的了,就不细说了。

step2 给maven的pom.xml配置文件里加入Springboot相关的依赖包

<?xml version="1.0" encoding="UTF-8"?>
<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.later</groupId>
   <artifactId>spring-order-service</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>spring-order-service</name>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.6.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <spring-cloud.version>Dalston.SR3</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
   </dependencies>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>

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


</project>

step3 新建Springboot的main类

package com.later;

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


/**
 * @author later
 * @version 1.0
 * @date 2017/11/7
 */
@EnableAutoConfiguration
@SpringBootApplication
public class PhoneAreaServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(PhoneAreaServiceApplication.class, args);
    }
}

step4 编写controller类

package com.later.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 

@RestController
@RequestMapping
public class HelloWordController {
    /**
     * 测试输出HelloWord
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "HelloWord";
    }
}

step 5 编写yml配置文件

spring:
  profiles:
    active: ${SPRING_PROFILES_ACTIVE:dev}
  application:
    name: spring-cloud-order-service
server:
  port: 8089

执行完上面几步,跑main类,就可以通过localhost:8089访问到我们刚刚写好的微服务了。

未经允许不得转载:极客技术 » Spring Cloud学习第二课:Spring boot学习

分享到:更多 ()

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    学习学习Spring boot,TKS

    跨境电商2周前 (01-03)回复