视频024~030(接口模块基本操作)

master
Administrator 2 years ago
commit fd5938cae3

21
.gitignore vendored

@ -0,0 +1,21 @@
# Created by .ignore support plugin (hsz.mobi)
### Example user template template
### Example user template
# IntelliJ project files
.idea
*.iml
out
gen
### Maven template
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar

@ -0,0 +1,58 @@
<?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">
<parent>
<artifactId>beacon-cloud</artifactId>
<groupId>com.mashibing</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>beacon-api</artifactId>
<dependencies>
<!-- web项目-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 注册到nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos拉取配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- openFeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- RabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- 记录日志等工具-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 测试的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 参数校验-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,18 @@
package com.mashibing.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author zjw
* @description
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ApiStarterApp {
public static void main(String[] args) {
SpringApplication.run(ApiStarterApp.class,args);
}
}

@ -0,0 +1,35 @@
package com.mashibing.api.controller;
import com.mashibing.api.enums.SmsCodeEnum;
import com.mashibing.api.form.SingleSendForm;
import com.mashibing.api.util.R;
import com.mashibing.api.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zjw
* @description
*/
@RestController
@RequestMapping("/sms")
@Slf4j
public class SmsController {
@PostMapping(value = "/single_send",produces = "application/json;charset=utf-8")
public ResultVO singleSend(@RequestBody @Validated SingleSendForm singleSendForm, BindingResult bindingResult){
//1. 校验参数
if (bindingResult.hasErrors()){
String msg = bindingResult.getFieldError().getDefaultMessage();
log.info("【接口模块-单条短信Controller】 参数不合法 msg = {}",msg);
return R.error(SmsCodeEnum.PARAMETER_ERROR.getCode(),msg);
}
return R.ok();
}
}

@ -0,0 +1,23 @@
package com.mashibing.api.controller;
import com.mashibing.api.filter.CheckFilterContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author zjw
* @description
*/
@Controller
public class TestController {
@Autowired
private CheckFilterContext checkFilterContext;
@GetMapping("/api/test")
public void test(){
System.out.println("====================================");
checkFilterContext.check(new Object());
}
}

@ -0,0 +1,21 @@
package com.mashibing.api.enums;
import lombok.Getter;
/**
* codemsg
* @author zjw
* @description
*/
@Getter
public enum SmsCodeEnum {
PARAMETER_ERROR(-10,"参数不合法!");
private Integer code;
private String msg;
SmsCodeEnum(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}

@ -0,0 +1,17 @@
package com.mashibing.api.filter;
import com.mashibing.api.form.SmsForm;
/**
*
* @author zjw
* @description
*/
public interface CheckFilter {
/**
*
* @param smsForm
*/
void check(SmsForm smsForm);
}

@ -0,0 +1,44 @@
package com.mashibing.api.filter;
import com.mashibing.api.form.SingleSendForm;
import com.mashibing.api.form.SmsForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author zjw
* @description
*/
@Component
@RefreshScope
public class CheckFilterContext {
// Spring的IOC会将对象全部都放到Map集合中
// 基于4.x中Spring提供的反省注解基于Map只拿到需要的类型对象即可
@Autowired
private Map<String,CheckFilter> checkFiltersMap;
// 基于Nacos获取到执行的顺序和需要执行的校验对象
@Value("${singleSendFilters:apikey,ip,sign,template}")
private String singleSendFilters;
/**
* check
*/
public void check(SmsForm smsForm){
if (smsForm instanceof SingleSendForm) {
// 单挑短信发送的策略
//1. 将获取到filters基于,做切分
String[] filterArray = singleSendFilters.split(",");
//2. 遍历数组即可
for (String filter : filterArray) {
CheckFilter checkFilter = checkFiltersMap.get(filter);
checkFilter.check(smsForm);
}
}
}
}

@ -0,0 +1,21 @@
package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter;
import com.mashibing.api.form.SmsForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @author zjw
* @description apikey
*/
@Service(value = "apikey")
@Slf4j
public class ApiKeyCheckFilter implements CheckFilter {
@Override
public void check(SmsForm smsForm) {
log.info("【接口模块-校验apikey】 校验ing…………");
}
}

@ -0,0 +1,21 @@
package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter;
import com.mashibing.api.form.SmsForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @author zjw
* @description
*/
@Service(value = "fee")
@Slf4j
public class FeeCheckFilter implements CheckFilter {
@Override
public void check(SmsForm smsForm) {
log.info("【接口模块-校验客户余额】 校验ing…………");
}
}

@ -0,0 +1,21 @@
package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter;
import com.mashibing.api.form.SmsForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @author zjw
* @description ip
*/
@Service(value = "ip")
@Slf4j
public class IPCheckFilter implements CheckFilter {
@Override
public void check(SmsForm smsForm) {
log.info("【接口模块-校验ip】 校验ing…………");
}
}

@ -0,0 +1,21 @@
package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter;
import com.mashibing.api.form.SmsForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @author zjw
* @description
*/
@Service(value = "mobile")
@Slf4j
public class MobileCheckFilter implements CheckFilter {
@Override
public void check(SmsForm smsForm) {
log.info("【接口模块-校验手机号】 校验ing…………");
}
}

@ -0,0 +1,21 @@
package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter;
import com.mashibing.api.form.SmsForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @author zjw
* @description
*/
@Service(value = "sign")
@Slf4j
public class SignCheckFilter implements CheckFilter {
@Override
public void check(SmsForm smsForm) {
log.info("【接口模块-校验签名】 校验ing…………");
}
}

@ -0,0 +1,21 @@
package com.mashibing.api.filter.impl;
import com.mashibing.api.filter.CheckFilter;
import com.mashibing.api.form.SmsForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* @author zjw
* @description
*/
@Service(value = "template")
@Slf4j
public class TemplateCheckFilter implements CheckFilter {
@Override
public void check(SmsForm smsForm) {
log.info("【接口模块-校验模板】 校验ing…………");
}
}

@ -0,0 +1,26 @@
package com.mashibing.api.form;
import lombok.Data;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
* @author zjw
* @description
*/
@Data
public class SingleSendForm extends SmsForm {
/** 短信内容 */
@NotBlank(message = "短信内容不能为空!")
private String text;
/** 0-验证码短信 1-通知类短信 2-营销类短信 */
@Range(min = 0,max = 2,message = "短信类型只能是0~2的整数")
@NotNull(message = "短信类型不允许非空!")
private Integer state;
}

@ -0,0 +1,25 @@
package com.mashibing.api.form;
import lombok.Data;
import javax.validation.constraints.NotBlank;
/**
*
* @author zjw
* @description
*/
@Data
public class SmsForm {
/** 客户的apikey */
@NotBlank(message = "apikey不允许为空")
private String apikey;
/** 手机号 */
@NotBlank(message = "手机号不能为空!")
private String mobile;
/** 客户业务内的uid */
private String uid;
}

@ -0,0 +1,24 @@
package com.mashibing.api.util;
import com.mashibing.api.vo.ResultVO;
/**
* @author zjw
* @description
*/
public class R {
public static ResultVO ok(){
ResultVO r = new ResultVO();
r.setCode(0);
r.setMsg("接收成功");
return r;
}
public static ResultVO error(Integer code ,String msg) {
ResultVO r = new ResultVO();
r.setCode(code);
r.setMsg(msg);
return r;
}
}

@ -0,0 +1,23 @@
package com.mashibing.api.vo;
import lombok.Data;
/**
* @author zjw
* @description
*/
@Data
public class ResultVO {
private Integer code;
private String msg;
private Integer count;
private Long fee;
private String uid;
private String sid;
}

@ -0,0 +1,18 @@
# 服务名称
spring:
application:
name: beacon-api
# 多环境
profiles:
active: dev
# nacos注册中心地址
cloud:
nacos:
discovery:
server-addr: 114.116.226.76:8848
# nacos配置中心地址:
config:
server-addr: 114.116.226.76:8848
file-extension: yml
# beacon-api-dev.yml

@ -0,0 +1,22 @@
package com.mashibing.api.filter;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
@SpringBootTest
@RunWith(SpringRunner.class)
public class CheckFilterContextTest {
@Autowired
private CheckFilterContext checkFilterContext;
@org.junit.Test
public void check() {
Object obj = new Object();
checkFilterContext.check(obj);
}
}

@ -0,0 +1,43 @@
<?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>
<modules>
<module>beacon-api</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath />
</parent>
<groupId>com.mashibing</groupId>
<artifactId>beacon-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<spring.cloud-version>Hoxton.SR12</spring.cloud-version>
<spring.cloud.alibaba-version>2.2.6.RELEASE</spring.cloud.alibaba-version>
</properties>
<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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Loading…
Cancel
Save