前置准备

在此之前,我们假设您已经:

检测java版本
java -version

查看 MavenGradle 版本

Maven
mvn -v
Gradle
gradle -v

初始化工程

新项目/工程

✨方式一:SpringBoot 式创建

Shoulder 提供了 start.itlym.cn 以便您可以像 start.spring.io 一样使用。无论是直接访问网页创建、预览、下载工程,还是在您的 IDE 中使用。

详细使用方式可参照 start.spring.io 在 GITHUB 的使用说明,只需要把其中的 url 改为 start.itlym.cn 即可。

在线创建

start-shoulder 页面 UI

IDE 中创建

支持的 IDE 有:

  • Eclipse / STS

  • IntelliJ IDEA (Ultimate Edition)

  • NetBeans, using the NB SpringBoot plugin)

  • Microsoft VSCode, using the Spring Initializr Java Support.

使用 Spring Boot CLI 创建

若您倾向于采用此创建方式,说明您已经 安装 Spring Boot CLI,并了解 Spring Boot CLI使用的方式

与 Spring 官网教程唯一区别是,您需要在所有的命令中显示声明依赖的网站,命令中加入后缀 --target https://start.itlym.cn,使用举例:

HTTPie
# 查看所有可用项
$ spring init --list --target https://start.itlym.cn

# 通过默认参数创建
$ spring init --target https://start.itlym.cn

# 创建一个带有 web 的 my-app.zip:
$ spring init -d=web my-app.zip --target https://start.itlym.cn

To 创建一个带有 web、data-jpa 的 gradle 项目目录:
$ spring init -d=web,jpa --build=gradle my-dir --target https://start.itlym.cn

使用 cURL/HTTPie 创建

cURL
# 查看更丰富的可用项介绍
curl https://start.spring.io
# 创建并下载
curl https://start.itlym.io/starter.zip -d dependencies=web,devtools \
           -d bootVersion=3.3.7 -o my-project.zip
HTTPie
# 查看更丰富的可用项介绍
http https://start.spring.io
# 创建并下载
http https://start.spring.io/starter.zip dependencies==web,devtools \
           bootVersion==3.3.7 -d

✨方式二:一条命令创建

可以运行以下命令快速创建一个 Shoulder 工程,Shoulder 将自动管理其依赖版本,同时已为您创建好目录结构。
mvn archetype:generate \
  -DarchetypeGroupId=cn.itlym \
  -DarchetypeArtifactId=shoulder-archetype-simple \
  -DarchetypeVersion=1.0.0 \
  -DgroupId=com.yourcompany \
  -DartifactId=yourappName \
  -Dversion=1.0-SNAPSHOT

方式三:手动创建

INFO: 这种方式创建需要您熟悉或创建锅 maven 工程。

Maven(pom.xml)
<?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>

	<groupId>com.example</groupId><!-- 您的 groupId -->
	<artifactId>myproject</artifactId><!-- 您的 artifactId -->
	<version>0.0.1-SNAPSHOT</version><!-- 您的 version -->

    <!-- 继承 shoulder 提供的父工程,自动管理版本号,包含了 spring-boot-parent -->
    <parent>
        <groupId>cn.itlym</groupId>
        <artifactId>shoulder-parent</artifactId>
        <version>1.0.0</version><!-- shoulder-version -->
    </parent>
    <dependencies>
        <!-- 版本号、web 相关依赖 自动管理。已自动引入对应的 spring-boot-starter-web -->
        <dependency>
            <groupId>cn.itlym</groupId>
            <artifactId>shoulder-starter-web</artifactId>
        </dependency>

    </dependencies>

    <!-- Additional lines to be added here... -->

</project>
Gradle(build.gradle)
# TODO coming soon
plugins {
	id 'java'
	id 'org.springframework.boot' version '3.4.1'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
	mavenCentral()
}

dependencies {
}

在已有的 Spring Boot 项目中使用

如您已创建好 Spring-Boot 3.x 工程,您只需要在 pom.xml 中加入以下内容,即可享受 shoulder-web 带来的增强的啦。

Maven
<dependency>
    <groupId>cn.itlym</groupId>
    <artifactId>shoulder-starter-web</artifactId>
    <version>1.0.0<version>
</dependency>
Gradle
compile 'cn.itlym:shoulder-starter-web:1.0.0'
  • Shoulder 作为 Spring Boot 的插件,您自然可以像其他 starter 一样无缝集成使用。

推荐:在 pom.xml 中加入 dependenciesManager 标签,就不用写 shoulder 相关依赖的版本号啦。
Maven
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>cn.itlym.shoulder</groupId>
            <artifactId>shoulder-dependencies</artifactId>
            <version>1.0.0</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    <dependencies>

    <dependency>
        <groupId>cn.itlym</groupId>
        <artifactId>shoulder-starter-web</artifactId>
        <!-- 就像 spring boot starter-xxx,不需要写版本号啦 -->
    </dependency>

  </dependencies>

配置

不需要任何配置,直接启动运行即可。

如同 Spring-Boot,约定大于配置。

编码

您可以尝试将以下内容复制进您的工程并运行查看。

Java
// imports...

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <a href="http://localhost:8080/rest/hello">去查看我的第一个接口</a>
 */

@RestController
@RequestMapping("rest")
public class MyRestController {

    @GetMapping("hello")
    public String hello() {
        return "shoulder";
    }
}
Kotlin
// imports...

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("rest")
class MyRestController {
	@GetMapping("hello")
	fun hello(): String {
		return "shoulder"
	}
}

运行

Shoulder 只是 Spring Boot 一个增强的 插件,实际您运行的仍然是 Spring Boot 应用,所有使用完全同 Spring Boot

IDE

在 IDE 中点击 main 方法的运行即可。

非 IDE

没有 IDE,用 maven / gradle 命令也可以通过命令轻松编译、打包、运行。

Maven
mvn spring-boot:run
Gradle
gradle bootJar

若要退出运行,同其他程序,按 ctrl-c 即可。

小结

通过简单几下操作,我们就实现了 Shoulder 的启动,甚至一行配置都不需要!

集成 Shoulder 非常的简单,只需要引入依赖即可使用!

Shoulder 的强大远不止这些功能,想要详细了解 Shoulder 的强大功能?那就继续往下看吧!

跟着 Demo 快速认识强大的 Shoulder