开发工具:IntelliJ IDEA 2019.2.4(Ultimate Edition)
搭建一个简单的服务
1.File > New > Module
2.Spring Initializr > SDK版本 > NEXT
3.填写模块信息
Group
项目组织唯一标识符
Artifact
模块唯一标识符
Type
工程类型,选择Maven Project
Language
开发语言,选择Java
Packaging
包类型,选择Jar
Java Version
Java版本
Version
模块版本
Name
模块名称
Description
模块描述
Package
模块包
4.选择模块依赖
Web > 勾选Spring Web
Spring Cloud > 勾选Cloud Bootstrap
> NEXT
5.填写模块名称及存放目录 > FINISH
6.删除.mvn
目录、.gitignore
HELP.md
mvnw
mvnw.cmd
文件,oa-user-center.iml
文件可移动到根目录的.idea
目录
7.重命名oa-user-center/src/main/resources/
目录下的application.properties
文件为application.yml
,并写入以下内容
spring:
application:
# 服务名称
name: oa-user-center
server:
# 服务HTTP端口号
port: 8081
8.修改oa-user-center
下的pom.xml
文件的parent
节点,引入父工程的spring boot依赖,并删除和parent
节点同级的groupId
节点
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 引入父工程的spring boot依赖 -->
<parent>
<groupId>cn.libaiii</groupId>
<artifactId>oa-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>oa-user-center</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>oa-user-center</name>
<description>User Center</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<!-- spring mvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
9.在父工程的pom.xml
文件中添加modules
节点,并在下级添加用户中心模块
<?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>
<!-- 父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.libaiii</groupId>
<artifactId>oa-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<!-- 用户中心模块 -->
<module>oa-user-center</module>
</modules>
</project>
10.新建UserController
类,包路径cn.libaiii.user.center.rest
package cn.libaiii.user.center.rest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* 用户中心
*
* @author libai
* @version 1.0
* @date 2020/1/14
*/
@RestController
@RequestMapping("user")
public class UserController {
@RequestMapping(value = "/get", method = RequestMethod.GET)
public String get() {
return "hello world";
}
}
11.运行oa-user-center
服务,点击右上角Run
按钮
控制台中输出 Started UserCenterApplication in xxx seconds
,代表服务启动成功
访问http://localhost:8081/user/get,响应 hello world
至此,一个简单的服务搭建完成