新增restful模式

This commit is contained in:
六如
2025-02-02 15:51:47 +08:00
parent 1f04edeaff
commit ddc709ede4
97 changed files with 1487 additions and 867 deletions

View File

@@ -0,0 +1,16 @@
<?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.gitee.sop</groupId>
<artifactId>example-product</artifactId>
<version>5.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>product-api</module>
<module>product-service</module>
</modules>
</project>

View File

@@ -0,0 +1,16 @@
<?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.gitee.sop</groupId>
<artifactId>product-api</artifactId>
<version>5.0.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@@ -0,0 +1,12 @@
package com.gitee.sop.story.api;
import com.gitee.sop.story.api.resp.ProductResult;
/**
* @author 六如
*/
public interface ProductService {
ProductResult getById(Long id);
}

View File

@@ -0,0 +1,51 @@
package com.gitee.sop.story.api.resp;
import java.io.Serializable;
import java.util.Date;
/**
* @author 六如
*/
public class ProductResult implements Serializable {
private static final long serialVersionUID = -3743413007549072654L;
private Integer id;
private String name;
// 日期格式要用Date,暂不支持LocalDateTime
private Date addTime = new Date();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
@Override
public String toString() {
return "StoryResult{" +
"id=" + id +
", name='" + name + '\'' +
", addTime=" + addTime +
'}';
}
}

View File

@@ -0,0 +1,25 @@
target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/
/local-config/

View File

@@ -0,0 +1,125 @@
<?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.6.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gitee.sop</groupId>
<artifactId>product-service</artifactId>
<version>5.0.0-SNAPSHOT</version>
<name>story-service</name>
<properties>
<java.version>1.8</java.version>
<!-- dubbo版本 -->
<dubbo.version>3.2.16</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>product-api</artifactId>
<version>5.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-spring-boot-starter</artifactId>
<version>5.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos注册中心 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-nacos-spring-boot-starter</artifactId>
</dependency>
<!-- zookeeper注册中心 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-zookeeper-curator5-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- 打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 文档推送 -->
<plugin>
<groupId>com.ly.smart-doc</groupId>
<artifactId>smart-doc-maven-plugin</artifactId>
<version>3.0.9</version>
<configuration>
<!--指定生成文档的使用的配置文件-->
<configFile>./src/main/resources/smart-doc.json</configFile>
<!--指定项目名称-->
<projectName>${project.artifactId}</projectName>
</configuration>
<dependencies>
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-service-support</artifactId>
<version>5.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,16 @@
package com.gitee.sop.productweb;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ExampleProductApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleProductApplication.class, args);
}
}

View File

@@ -0,0 +1,20 @@
package com.gitee.sop.productweb.message;
import com.gitee.sop.support.message.I18nMessage;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author 六如
*/
@AllArgsConstructor
@Getter
public enum StoryMessageEnum implements I18nMessage {
/**
* 参数错误
*/
ISV_PARAM_ERROR("isv.invalid-parameter");
private final String configKey;
}

View File

@@ -0,0 +1,92 @@
package com.gitee.sop.productweb.open;
import com.gitee.sop.productweb.open.req.ProductSaveRequest;
import com.gitee.sop.productweb.open.resp.ProductResponse;
import com.gitee.sop.support.annotation.Open;
import com.gitee.sop.support.context.OpenContext;
import com.gitee.sop.support.dto.FileData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
/**
* 产品服务
*
* @author 六如
* @dubbo
*/
@Api("产品服务")
public interface OpenProduct {
/**
* 新增故事
*
* @param request 入参
* @return 返回id
*/
@Open("product.save")
Integer save(ProductSaveRequest request);
@Open("product.update")
Integer update(Integer id, ProductSaveRequest request);
// 演示抛出异常
@Open("product.updateError")
Integer updateError(Integer id);
@ApiOperation(value = "根据id获取故事")
@Open("product.get")
ProductResponse getById(@NotNull(message = "id必填") Integer id);
// 需要授权
@Open(value = "product.get", version = "2.0", permission = true)
ProductResponse getByIdV2(Long id);
@Open(value = "product.get.context")
ProductResponse getContext(Long id, OpenContext context);
// 默认方法,注解放在这里也有效
@Open("product.find")
default ProductResponse getById(Integer id, String name) {
ProductResponse storyResponse = new ProductResponse();
storyResponse.setId(id);
storyResponse.setName(name);
return storyResponse;
}
// 默认方法,注解放在这里也有效
@Open("alipay.story.find")
default ProductResponse findByName(String name) {
ProductResponse storyResponse = new ProductResponse();
storyResponse.setName(name);
return storyResponse;
}
// 演示单文件上传
@Open("product.upload")
ProductResponse upload(ProductSaveRequest request, FileData file);
// 演示多文件上传
@Open("product.upload.more")
ProductResponse upload2(
ProductSaveRequest request,
@NotNull(message = "身份证正面必填") FileData idCardFront,
@NotNull(message = "身份证背面必填") FileData idCardBack
);
// 演示多文件上传
@Open("product.upload.list")
ProductResponse upload3(
ProductSaveRequest request,
@Size(min = 2, message = "最少上传2个文件")
List<FileData> files
);
// 下载
@Open("product.download")
FileData download(Integer id);
}

View File

@@ -0,0 +1,152 @@
package com.gitee.sop.productweb.open.impl;
import com.gitee.sop.productweb.message.StoryMessageEnum;
import com.gitee.sop.productweb.open.OpenProduct;
import com.gitee.sop.productweb.open.req.ProductSaveRequest;
import com.gitee.sop.productweb.open.resp.ProductResponse;
import com.gitee.sop.support.context.OpenContext;
import com.gitee.sop.support.dto.CommonFileData;
import com.gitee.sop.support.dto.FileData;
import com.gitee.sop.support.exception.OpenException;
import org.apache.commons.io.IOUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Assert;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* 开放接口实现
*
* @author 六如
*/
@DubboService(validation = "true")
public class OpenProductImpl implements OpenProduct {
@Value("${dubbo.labels:}")
private String env;
@Override
public Integer save(ProductSaveRequest request) {
return 1;
}
@Override
public Integer update(Integer id, ProductSaveRequest request) {
System.out.println("update, id:" + id + ", storySaveDTO=" + request);
return 1;
}
@Override
public Integer updateError(Integer id) {
// 抛出业务异常
if (id == null || id == 0) {
throw new OpenException(StoryMessageEnum.ISV_PARAM_ERROR, "id is null or 0");
}
if (id < 0) {
// 自定义code, msg
throw new OpenException("4000", "id必须大于0");
}
return 1;
}
@Override
public ProductResponse getById(Integer id) {
ProductResponse storyResponse = new ProductResponse();
storyResponse.setId(id);
storyResponse.setName("冰箱-" + env);
return storyResponse;
}
@Override
public ProductResponse getByIdV2(Long id) {
ProductResponse storyResponse = new ProductResponse();
storyResponse.setId(2);
storyResponse.setName("冰箱2.0");
return storyResponse;
}
// 演示获取上下文
@Override
public ProductResponse getContext(Long id, OpenContext context) {
ProductResponse storyResponse = new ProductResponse();
storyResponse.setId(3);
storyResponse.setName(context.toString());
// 获取回调参数
String notifyUrl = context.getNotifyUrl();
System.out.println(notifyUrl);
// 方式2使用OpenContext.current()
String notifyUrl1 = OpenContext.current().getNotifyUrl();
System.out.println(Objects.equals(notifyUrl1, notifyUrl));
return storyResponse;
}
@Override
public ProductResponse upload(ProductSaveRequest storySaveDTO, FileData file) {
System.out.println("getName:" + file.getName());
System.out.println("getOriginalFilename:" + file.getOriginalFilename());
checkFile(Arrays.asList(file));
ProductResponse storyResponse = new ProductResponse();
storyResponse.setId(1);
storyResponse.setName(file.getOriginalFilename());
return storyResponse;
}
@Override
public ProductResponse upload2(ProductSaveRequest storySaveDTO, FileData idCardFront, FileData idCardBack) {
System.out.println("upload:" + storySaveDTO);
checkFile(Arrays.asList(idCardFront, idCardBack));
ProductResponse storyResponse = new ProductResponse();
storyResponse.setId(1);
storyResponse.setName(storySaveDTO.getProductName());
return storyResponse;
}
@Override
public ProductResponse upload3(ProductSaveRequest storySaveDTO, List<FileData> files) {
List<String> list = new ArrayList<>();
list.add("upload:" + storySaveDTO);
checkFile(files);
ProductResponse storyResponse = new ProductResponse();
storyResponse.setId(1);
storyResponse.setName(storySaveDTO.getProductName());
return storyResponse;
}
@Override
public FileData download(Integer id) {
CommonFileData fileData = new CommonFileData();
ClassPathResource resource = new ClassPathResource("download.txt");
fileData.setOriginalFilename(resource.getFilename());
try {
fileData.setData(IOUtils.toByteArray(resource.getInputStream()));
} catch (IOException e) {
throw new RuntimeException(e);
}
return fileData;
}
private void checkFile(List<FileData> fileDataList) {
for (FileData file : fileDataList) {
Assert.notNull(file.getName());
Assert.notNull(file.getOriginalFilename());
Assert.notNull(file.getBytes());
Assert.isTrue(!file.isEmpty());
}
}
}

View File

@@ -0,0 +1,31 @@
package com.gitee.sop.productweb.open.req;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
/**
* @author 六如
*/
@Data
public class ProductSaveRequest implements Serializable {
private static final long serialVersionUID = -1214422742659231037L;
/**
* 故事名称
*/
@NotBlank(message = "故事名称必填")
@Length(max = 64)
private String productName;
/**
* 添加时间
*/
@NotNull(message = "添加时间必填")
private Date addTime;
}

View File

@@ -0,0 +1,21 @@
package com.gitee.sop.productweb.open.resp;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* @author 六如
*/
@Data
public class ProductResponse implements Serializable {
private static final long serialVersionUID = -3743413007549072654L;
private Integer id;
private String name;
// 日期格式要用Date,暂不支持LocalDateTime
private Date addTime = new Date();
}

View File

@@ -0,0 +1,28 @@
package com.gitee.sop.productweb.rpc;
import com.gitee.sop.story.api.ProductService;
import com.gitee.sop.story.api.resp.ProductResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import java.util.Date;
/**
* @author 六如
*/
@DubboService
public class ProductServiceImpl implements ProductService {
@Value("${dubbo.labels:}")
private String env;
@Override
public ProductResult getById(Long id) {
System.out.println("StoryService.getById, env=" + env);
ProductResult storyResult = new ProductResult();
storyResult.setName("彩电-" + env);
storyResult.setId(id.intValue());
storyResult.setAddTime(new Date());
return storyResult;
}
}

View File

@@ -0,0 +1 @@
dubbo.registry.address=zookeeper://localhost:2181

View File

@@ -0,0 +1 @@
dubbo.registry.address=nacos://localhost:8848

View File

@@ -0,0 +1,9 @@
spring.profiles.active=dev
server.port=7071
spring.application.name=product-service
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.application.qos-enable=false
dubbo.registry.address=zookeeper://localhost:2181

View File

@@ -0,0 +1,27 @@
{
// 开启推送
"enable": true,
// 扫描package多个用;隔开
"basePackage": "com.gitee.sop.storyweb.open",
// 推送URLIP端口对应Torna服务器
"url": "http://localhost:7700/api",
// 模块token
"token": "d4f07434c9cf4a989e4c6d301684b357",
// 推送人
"author": "Jim",
// 打开调试:true/false
"debug": true,
// 是否替换文档true替换false不替换追加。默认true
"isReplace": true,
// 第三方jar中的class配置
"jarClass": {
"com.xx.Page": {
"records": { "value": "查询数据列表", "example": "" },
"total": { "value": "总数", "example": "100" },
"size": { "value": "页数", "example": "10" },
"current": { "value": "当前页", "example": "1" },
"countId": { "hidden": true },
"orders": { "hidden": true }
}
}
}

View File

@@ -0,0 +1 @@
abc,你好~!@#

View File

@@ -0,0 +1,2 @@
isv.invalid-parameter=Invalid parameter, {0}
isv.invalid-parameter.solution=Check doc api and input right parameter.

View File

@@ -0,0 +1,2 @@
isv.invalid-parameter=\u53c2\u6570\u65e0\u6548, {0}
isv.invalid-parameter.solution=\u67e5\u770b\u63a5\u53e3\u6587\u6863\u5e76\u8f93\u5165\u6b63\u786e\u7684\u53c2\u6570

View File

@@ -0,0 +1,13 @@
{
"framework": "sop",
"outPath": "target/doc",
"projectName": "项目",
"packageFilters": "com.gitee.sop.storyweb.open.*",
"openUrl": "http://localhost:7700/api", // torna服务器地址
"appToken": "a358e4059b17440aae66343f4ec89001", // torna应用token
"debugEnvName":"本地环境",
"debugEnvUrl":"http://127.0.0.1:8081",
"tornaDebug": true,
"replace": true,
"showValidation": false
}

View File

@@ -0,0 +1,14 @@
package com.gitee.sop.productweb;
import cn.torna.swaggerplugin.SwaggerPlugin;
/**
* 推送swagger文档
* @author thc
*/
public class DocPushTest {
public static void main(String[] args) {
SwaggerPlugin.pushDoc();
}
}