单条发送接口,参数校验

master
kezhen0805 2 years ago
parent b3ac672edb
commit 39b99b25ba

@ -47,6 +47,11 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
</dependency> </dependency>
<!-- 参数校验-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies> </dependencies>
<properties> <properties>

@ -0,0 +1,40 @@
package com.mashibing.api.controller;
import com.mashibing.api.enums.SmsCodeEnum;
import com.mashibing.api.from.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 kezhen
* @date 2022/12/23
* @description
*/
@RestController
@RequestMapping("/sms")
@Slf4j
public class SmsController {
/**
* @param singleSendForm
* @return
*/
@PostMapping(value = "/single_send", produces = "application/json;charset=utf-8")
public ResultVO signleSend(@RequestBody @Validated SingleSendForm singleSendForm, BindingResult bindingResult) {
System.out.println(singleSendForm);
System.out.println("SmsController.signleSend");
if (bindingResult.hasErrors()) {
String message = bindingResult.getFieldError().getDefaultMessage();
log.info("【接口模块-单条短信Contorller】 参数不合法msg = {}", message);
return R.error(SmsCodeEnum.PARAMETER_ERROR.getCode(),message);
}
return R.ok();
}
}

@ -0,0 +1,20 @@
package com.mashibing.api.enums;
import lombok.Getter;
/**
* @author kezhen
* @date 2022/12/24
* @description codemessage
*/
@Getter
public enum SmsCodeEnum {
PARAMETER_ERROR(-10,"参数不合法");
private Integer code;
private String meg;
SmsCodeEnum(Integer code, String meg) {
this.code = code;
this.meg = meg;
}
}

@ -9,7 +9,7 @@ import java.util.Map;
/** /**
* @author kezhen * @author kezhen
* @description * @description
*/ */
@Component @Component
@RefreshScope @RefreshScope

@ -0,0 +1,33 @@
package com.mashibing.api.from;
import lombok.Data;
import lombok.ToString;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
* @author kezhen
* @date 2022/12/23
* @description
*/
@Data
@ToString
public class SingleSendForm {
// 客户的apikey
@NotBlank(message = "apikey不允许为空")
private String apikey;
// 手机号
@NotBlank(message = "手机号不允许为空!")
private String mobile;
// 短信内容
@NotBlank(message = "短信内容不允许为空!")
private String text;
// 客户业务内的uid
private String uid;
// 短信类型 0-验证码短信 1-通知类短信 2-营销类短信
@Range(min = 0,max = 2,message = "短信类型只能是02的整数")
@NotNull(message = "短信类型不允许为空")
private Integer state;
}

@ -0,0 +1,24 @@
package com.mashibing.api.util;
import com.mashibing.api.vo.ResultVO;
/**
* @author kezhen
* @date 2022/12/23
* @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 message) {
ResultVO r = new ResultVO();
r.setCode(code);
r.setMsg(message);
return r;
}
}

@ -0,0 +1,18 @@
package com.mashibing.api.vo;
import lombok.Data;
/**
* @author kezhen
* @date 2022/12/23
* @description
*/
@Data
public class ResultVO {
private Integer code;
private String msg;
private Integer count;
private Long fee;
private String uid;
private String sid;
}
Loading…
Cancel
Save