当前位置:   article > 正文

springboot整合redis实现发送短信验证码_springboot redis批量发送短信池

springboot redis批量发送短信池

    我用的短信平台是阿里云的,需要付费购买服务,购买地址:https://common-buy.aliyun.com/?spm=5176.8195934.907839.sms6.312c4183mzE9Yb&&commodityCode=newdysmsbag#/buy

    付费完成后,首先申请短信签名和短信模板:https://help.aliyun.com/document_detail/55327.html?spm=a2c4g.11186623.6.549.huzd56。

短信签名:根据用户属性来创建符合自身属性的签名信息。企业用户需要上传相关企业资质证明,个人用户需要上传证明个人身份的证明。注意:短信签名需要审核通过后才可以使用。

短信模板:短信模板,即具体发送的短信内容。短信模板可以支持验证码、短信通知、推广短信、国际/港澳台消息四种模式。验证码和短信通知,通过变量替换实现个性短信定制。推广短信不支持在模板中添加变量。短信模板需要审核通过后才可以使用。

短信示例:【阿里云】 验证码${number},您正进行支付宝的身份验证,打死不告诉别人!这里的短信签名:阿里云,短信模板: 验证码${number},您正进行支付宝的身份验证,打死不告诉别人!

    最后获取 asscessKeyId 和 accessKeySecret 。结合阿里云提供的开发者文档即可进行接口开发,短信开发api文档:https://help.aliyun.com/product/44282.html?spm=a2c4g.750001.6.1.T84wBi

一、安装redis

下载地址:Redis,下载最新稳定版本。

在/usr/local创建redis文件夹上传下载的安装包到redis目录下:

安装:

$ tar -zxvf redis-4.0.9.tar.gz
$ cd redis-4.0.9
$ make

安装完成后的目录:

$ cd  src
$ ./redis-server  ../redis.conf
./redis-server 这种方式启动redis 使用的是默认配置。可以通过启动参数告诉redis使用指定配置文件使用下面命令启动。

二、创建maven工程,目录结构如下:

三、pom.xml文件添加redis依赖

  1. <!--redis-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>

四、application.properties添加redis配置

五、随机生成验证码工具类

  1. public class IdentifyCodeUtil {
  2. public static String getRandom() {
  3. String num = "";
  4. for (int i = 0; i < 6; i++) {
  5. num = num + String.valueOf((int) Math.floor(Math.random() * 9 + 1));
  6. }
  7. return num;
  8. }
  9. }

六、redis缓存配置类

     sprringboot启动类Application.java加入注解:@EnableCaching

   配置redis采用缓存,设置key和value的序列化方式

  1. package com.jp.tech.applet.ms.sms;
  2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  3. import com.fasterxml.jackson.annotation.PropertyAccessor;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.springframework.cache.CacheManager;
  6. import org.springframework.cache.annotation.CachingConfigurerSupport;
  7. import org.springframework.cache.annotation.EnableCaching;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.data.redis.cache.RedisCacheManager;
  11. import org.springframework.data.redis.connection.RedisConnectionFactory;
  12. import org.springframework.data.redis.core.RedisTemplate;
  13. import org.springframework.data.redis.core.StringRedisTemplate;
  14. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  15. /**
  16. * Redis缓存配置类
  17. *
  18. * @author yangfeng
  19. */
  20. @Configuration
  21. @EnableCaching
  22. public class RedisConfig extends CachingConfigurerSupport {
  23. //缓存管理器
  24. @Bean
  25. public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
  26. RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
  27. //设置缓存过期时间
  28. //cacheManager.setDefaultExpiration(20);
  29. return cacheManager;
  30. }
  31. @Bean
  32. public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
  33. StringRedisTemplate template = new StringRedisTemplate(factory);
  34. setSerializer(template);//设置序列化工具
  35. template.afterPropertiesSet();
  36. return template;
  37. }
  38. private void setSerializer(StringRedisTemplate template) {
  39. @SuppressWarnings({"rawtypes", "unchecked"})
  40. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  41. ObjectMapper om = new ObjectMapper();
  42. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  43. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  44. jackson2JsonRedisSerializer.setObjectMapper(om);
  45. template.setValueSerializer(jackson2JsonRedisSerializer);
  46. }
  47. }

七、redis接口类

      此接口用于连接redis操作生成的验证码,设置过期时间。

  1. public interface IRedisService {
  2. /**
  3. * 设置key-value
  4. * @param key
  5. * @param value
  6. */
  7. void setKey(String key, String value);
  8. /**
  9. * 获取key
  10. * @param key
  11. * @return
  12. */
  13. String getValue(String key);
  14. /**
  15. * 删除key
  16. * @param key
  17. */
  18. void delete(String key);
  19. }

八、redis接口实现类

      保存、获取、删除验证码接口实现方法。

  1. import com.jp.tech.applet.ms.sms.service.IRedisService;
  2. import org.springframework.data.redis.core.*;
  3. import org.springframework.stereotype.Service;
  4. import javax.annotation.Resource;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.concurrent.TimeUnit;
  9. @Service
  10. public class RedisService implements IRedisService {
  11. @Resource
  12. private RedisTemplate redisTemplate;
  13. @Override
  14. public void setKey(String key, String value) {
  15. ValueOperations<String, String> ops = redisTemplate.opsForValue();
  16. ops.set(key, value, 900, TimeUnit.SECONDS);//15分钟过期
  17. }
  18. @Override
  19. public String getValue(String key) {
  20. ValueOperations<String, String> ops = redisTemplate.opsForValue();
  21. return ops.get(key);
  22. }
  23. @Override
  24. public void delete(String key) {
  25. redisTemplate.delete(key);
  26. }
  27. }

九、发送短信接口类

  1. package com.jp.zpzc.service;
  2. import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
  3. import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
  4. import com.aliyuncs.exceptions.ClientException;
  5. import com.jp.framework.common.model.ServiceResult;
  6. public interface ISmsService {
  7. /**
  8. * 发送短信接口
  9. *
  10. * @param phoneNums 手机号码
  11. * @param signName 模板签名
  12. * @param templeteCode 模板代码
  13. * @param templateParam 模板替换参数
  14. * @param outId 提供给业务方扩展字段
  15. * @return
  16. * @throws ClientException
  17. */
  18. SendSmsResponse sendSms(String phoneNums, String signName, String templeteCode,
  19. String templateParam, String outId) throws ClientException;
  20. /**
  21. * 查询短信发送明细
  22. *
  23. * @param phoneNumber
  24. * @param bizId 业务流水号
  25. * @return
  26. * @throws ClientException
  27. */
  28. QuerySendDetailsResponse querySendDetails(String phoneNumber, String bizId) throws ClientException;
  29. /**
  30. * 发送短信服务
  31. *
  32. * @param mobile 手机号
  33. * @return
  34. */
  35. ServiceResult<Object> sendMessage(String mobile);
  36. /**
  37. * 判断验证码是否正确
  38. *
  39. * @param mobile
  40. * @param identifyCode
  41. * @return
  42. */
  43. ServiceResult<Boolean> checkIsCorrectCode(String mobile, String identifyCode);
  44. }

十、短信接口实现类

pom中引入阿里云短信SDK:

  1. <!--阿里云短信服务平台-->
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-core</artifactId>
  5. <version>3.3.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.aliyun</groupId>
  9. <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
  10. <version>1.1.0</version>
  11. </dependency>

application.properties加入调用接口的认证信息:

  1. aliyun.sms.accessKeyId=LTAIr0fPaMi66tCy111
  2. aliyun.sms.accessKeySecret=bQ9Re8uV14yRKKhWdWAbbUO15EM7w1​111

这里我用的是阿里云短信平台,根据提供的demo自己整理后的接口实现。

smsService.sendSms(phoneName, "XXXXXX", "XXXXXXX", JSON.toJSONString(codeMap), null)

第一个XXXXX代表申请的短信签名名称,第二个代表申请的短信模板编码,改成自己申请的即可。

  1. package com.jp.zpzc.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.aliyuncs.DefaultAcsClient;
  4. import com.aliyuncs.IAcsClient;
  5. import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
  6. import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
  7. import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
  8. import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
  9. import com.aliyuncs.exceptions.ClientException;
  10. import com.aliyuncs.profile.DefaultProfile;
  11. import com.aliyuncs.profile.IClientProfile;
  12. import com.jp.framework.common.model.ServiceResult;
  13. import com.jp.framework.common.model.ServiceResultHelper;
  14. import com.jp.framework.common.util.Constant;
  15. import com.jp.framework.common.util.IdentifyCodeUtil;
  16. import com.jp.zpzc.service.IRedisService;
  17. import com.jp.zpzc.service.ISmsService;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.stereotype.Service;
  23. import javax.annotation.Resource;
  24. import java.text.SimpleDateFormat;
  25. import java.util.Date;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. @Service
  29. public class SmsService implements ISmsService {
  30. private Logger logger = LoggerFactory.getLogger(this.getClass());
  31. //产品名称:云通信短信API产品,开发者无需替换
  32. static final String product = "Dysmsapi";
  33. //产品域名,开发者无需替换
  34. static final String domain = "dysmsapi.aliyuncs.com";
  35. @Value("${aliyun.sms.accessKeyId}")
  36. private String accessKeyId;
  37. @Value("${aliyun.sms.accessKeySecret}")
  38. private String accessKeySecret;
  39. @Resource
  40. private IRedisService redisService;
  41. /**
  42. * 发送短信服务
  43. *
  44. * @param mobile
  45. * @return
  46. */
  47. public ServiceResult<Object> sendMessage(String mobile) {
  48. if (StringUtils.isEmpty(mobile)) {
  49. return ServiceResultHelper.genResultWithFaild(Constant.ErrorCode.INVALID_PARAM_MSG, Constant.ErrorCode.INVALID_PARAM_CODE);
  50. }
  51. String identifyCode;
  52. //1. 判断是否缓存该账号验证码
  53. String returnCode = redisService.getValue(mobile + Constant.SMS_LOGIN_IDENTIFY_CODE);
  54. if (!StringUtils.isEmpty(returnCode)) {
  55. identifyCode = returnCode;
  56. } else {
  57. identifyCode = IdentifyCodeUtil.getRandom();
  58. }
  59. //2.发送短信
  60. Map<String, String> codeMap = new HashMap<>();
  61. codeMap.put("code", identifyCode);
  62. SendSmsResponse response;
  63. try {
  64. response = sendSms(mobile, XXXXX, XXXXX, JSON.toJSONString(codeMap), null);
  65. //短信发送成功后存入redis
  66. if (response != null && Constant.SMS_SEND_STATUS_OK.equalsIgnoreCase(response.getCode()) && StringUtils.isEmpty(returnCode)) {
  67. redisService.setKey(mobile + Constant.SMS_LOGIN_IDENTIFY_CODE, identifyCode);
  68. }
  69. return ServiceResultHelper.genResultWithSuccess(response);
  70. } catch (Exception e) {
  71. logger.error("sendMessage method invoke error: {}", e.getMessage());
  72. }
  73. return ServiceResultHelper.genResultWithFaild("短信发送失败", null);
  74. }
  75. /**
  76. * 发送短信接口
  77. *
  78. * @param phoneNums
  79. * @param signName 模板签名
  80. * @param templeteCode 模板代码
  81. * @param templateParam 模板替换参数
  82. * @param outId 提供给业务方扩展字段
  83. * @return
  84. * @throws ClientException
  85. */
  86. @Override
  87. public SendSmsResponse sendSms(String phoneNums, String signName, String templeteCode, String templateParam, String outId) throws ClientException {
  88. //可自助调整超时时间
  89. System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
  90. System.setProperty("sun.net.client.defaultReadTimeout", "10000");
  91. //初始化acsClient,暂不支持region化
  92. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
  93. DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
  94. IAcsClient acsClient = new DefaultAcsClient(profile);
  95. //组装请求对象-具体描述见控制台-文档部分内容
  96. SendSmsRequest request = new SendSmsRequest();
  97. //必填:待发送手机号
  98. request.setPhoneNumbers(phoneNums);
  99. //必填:短信签名-可在短信控制台中找到
  100. request.setSignName(signName);//众评众测
  101. //必填:短信模板-可在短信控制台中找到
  102. request.setTemplateCode(templeteCode);
  103. //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
  104. request.setTemplateParam(templateParam);//{"code":"152745"}
  105. //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
  106. //request.setSmsUpExtendCode("90997");
  107. //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
  108. request.setOutId(outId);//zpzc
  109. //hint 此处可能会抛出异常,注意catch
  110. SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
  111. acsClient.getAcsResponse(request);
  112. return sendSmsResponse;
  113. }
  114. /**
  115. * 判断验证码是否正确
  116. *
  117. * @param mobile
  118. * @param identifyCode
  119. * @return
  120. */
  121. public ServiceResult<Boolean> checkIsCorrectCode(String mobile, String identifyCode) {
  122. if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(identifyCode)) {
  123. return ServiceResultHelper.genResultWithFaild(Constant.ErrorCode.INVALID_PARAM_MSG, Constant.ErrorCode.INVALID_PARAM_CODE);
  124. }
  125. String returnCode = redisService.getValue(mobile + Constant.SMS_LOGIN_IDENTIFY_CODE);
  126. if (!StringUtils.isEmpty(returnCode) && returnCode.equals(identifyCode)) {
  127. return ServiceResultHelper.genResultWithSuccess();
  128. }
  129. return ServiceResultHelper.genResultWithFaild();
  130. }
  131. /**
  132. * 查询短信发送明细
  133. *
  134. * @param phoneNumber
  135. * @param bizId
  136. * @return
  137. * @throws ClientException
  138. */
  139. @Override
  140. public QuerySendDetailsResponse querySendDetails(String phoneNumber, String bizId) throws ClientException {
  141. //可自助调整超时时间
  142. System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
  143. System.setProperty("sun.net.client.defaultReadTimeout", "10000");
  144. //初始化acsClient,暂不支持region化
  145. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
  146. DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
  147. IAcsClient acsClient = new DefaultAcsClient(profile);
  148. //组装请求对象
  149. QuerySendDetailsRequest request = new QuerySendDetailsRequest();
  150. //必填-号码
  151. request.setPhoneNumber(phoneNumber);
  152. //可选-流水号
  153. request.setBizId(bizId);
  154. //必填-发送日期 支持30天内记录查询,格式yyyyMMdd
  155. SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
  156. request.setSendDate(ft.format(new Date()));
  157. //必填-页大小
  158. request.setPageSize(10L);
  159. //必填-当前页码从1开始计数
  160. request.setCurrentPage(1L);
  161. //hint 此处可能会抛出异常,注意catch
  162. QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);
  163. return querySendDetailsResponse;
  164. }
  165. }

十一、controller类

       调用redis存入随机生成的验证码,调用短信平台接口发送验证码。

  1. package com.jp.zpzc.controller.sms;
  2. import com.jp.zpzc.service.ISmsService;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import javax.annotation.Resource;
  9. /**
  10. * 短信验证码接口
  11. *
  12. * @author yangfeng
  13. * @date 2018-06-09 16:02
  14. **/
  15. @RestController
  16. @RequestMapping("/smsVerifityCode")
  17. public class SmsVerifityCodeController {
  18. private Logger LOG = LoggerFactory.getLogger(this.getClass());
  19. @Resource
  20. private ISmsService smsService;
  21. /**
  22. * 发送短信验证码
  23. *
  24. * @param mobile
  25. * @return
  26. */
  27. @RequestMapping("/sendMessage")
  28. public Object sendMessage(@RequestParam String mobile) {
  29. return smsService.sendMessage(mobile);
  30. }
  31. /**
  32. * 判断验证码是否正确
  33. *
  34. * @param mobile
  35. * @param identifyCode
  36. * @return
  37. */
  38. @RequestMapping("/checkIsCorrectCode")
  39. public Object checkIsCorrectCode(@RequestParam String mobile, @RequestParam String identifyCode) {
  40. return smsService.checkIsCorrectCode(mobile, identifyCode);
  41. }
  42. }

十二、短信发送测试


短信发送成功。

附:

Constant类代码:

  1. package com.jp.tech.applet.common.constant;
  2. public class Constant {
  3. public static class ErrorCode {
  4. /**
  5. * 无效参数
  6. */
  7. public static Integer INVALID_PARAM_CODE = -101;
  8. public static String INVALID_PARAM_MSG = "无效参数";
  9. /**
  10. * 没有权限
  11. */
  12. public static Integer PERMISSION_DENIED_CODE = -102;
  13. public static String PERMISSION_DENIED_MSG = "没有权限";
  14. /**
  15. * 通用错误
  16. */
  17. public static Integer COMMON_ERROR_CODE = -103;
  18. public static String COMMON_ERROR_MSG = "服务器繁忙,请稍后再试";
  19. /**
  20. * 登录失效
  21. */
  22. public static Integer INVALID_LOGIN_CODE = -104;
  23. public static String INVALID_LOGIN_MSG = "登录失效";
  24. /**
  25. * 数据库操作失败
  26. */
  27. public static Integer DATABASE_OPERATION_ERROR_CODE = -105;
  28. public static String DATABASE_OPERATION_ERROR_MSG = "数据库操作失败";
  29. /**
  30. * token失效
  31. */
  32. public static Integer INVALID_TOKEN_CODE = -106;
  33. public static String INVALID_TOKEN_MSG = "用户未登录或登录信息已失效";
  34. /**
  35. * 服务器异常
  36. */
  37. public static Integer SERVER_ERROR_CODE = -200;
  38. public static String SERVER_ERROR_MSG = "服务器异常";
  39. }
  40. }

 ServiceResultHelper 类:

  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by Fernflower decompiler)
  4. //
  5. package com.jp.framework.common.model;
  6. import com.jp.framework.common.util.Constant;
  7. public class ServiceResultHelper {
  8. public ServiceResultHelper() {
  9. }
  10. public static <T> ServiceResult<T> genResult(boolean succeed, int retCode, String msg, T obj) {
  11. ServiceResult ret = new ServiceResult();
  12. ret.setData(obj);
  13. ret.setMsg(msg);
  14. ret.setCode(retCode);
  15. ret.setSucceed(succeed);
  16. return ret;
  17. }
  18. public static <T> boolean isSuccess(ServiceResult<T> result) {
  19. return result != null && result.isSucceed() && result.getCode() == 0;
  20. }
  21. public static <T> ServiceResult<T> genResultWithSuccess(T obj) {
  22. return genResult(true, Constant.SUCCESS, "成功", obj);
  23. }
  24. public static <T> ServiceResult<T> genResultWithSuccess() {
  25. return genResult(true, Constant.SUCCESS, "成功", (Object)null);
  26. }
  27. public static <T> ServiceResult<T> genResultWithFaild() {
  28. return genResult(false, Constant.FAILED, "失败", (Object)null);
  29. }
  30. public static <T> ServiceResult<T> genResultWithFaild(String msg, Integer code) {
  31. return genResult(false, code, msg, (Object)null);
  32. }
  33. public static <T> ServiceResult<T> genResultWithDataNull() {
  34. return genResult(false, Constant.SUCCESS, "数据为空", (Object)null);
  35. }
  36. }

ServiceResult 类:

  1. import com.fasterxml.jackson.annotation.JsonInclude;
  2. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  3. import com.backstage.core.constant.Constant;
  4. import org.apache.commons.lang3.builder.ToStringBuilder;
  5. import org.apache.commons.lang3.builder.ToStringStyle;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. @JsonInclude(Include.ALWAYS)
  9. public final class ServiceResult<T> {
  10. private static final long serialVersionUID = 6977558218691386450L;
  11. private boolean succeed = true;
  12. private int code;
  13. private int subCode;
  14. private String msg;
  15. private T data;
  16. private Map<String, Object> additionalProperties;
  17. public ServiceResult() {
  18. this.code = Constant.SUCCESS;
  19. this.subCode = Constant.SUCCESS;
  20. this.additionalProperties = new HashMap();
  21. }
  22. public static ServiceResult<Boolean> error() {
  23. return error(500, "未知异常,请联系管理员");
  24. }
  25. public static ServiceResult<Boolean> error(String msg) {
  26. return error(500, msg);
  27. }
  28. public static ServiceResult<Boolean> error(int subCode, String msg) {
  29. ServiceResult result = new ServiceResult();
  30. result.setCode(Constant.FAILED);
  31. result.setSubCode(subCode);
  32. result.setSucceed(false);
  33. result.setMsg(msg);
  34. return result;
  35. }
  36. public static ServiceResult ok() {
  37. return ok(Constant.SUCCESS, "成功");
  38. }
  39. public static ServiceResult ok(int code, String msg) {
  40. ServiceResult result = new ServiceResult();
  41. result.setCode(code);
  42. result.setSucceed(true);
  43. result.setMsg(msg);
  44. return result;
  45. }
  46. public static ServiceResult ok(Object data) {
  47. ServiceResult d = new ServiceResult();
  48. d.setSucceed(true);
  49. d.setData(data);
  50. d.setCode(Constant.SUCCESS);
  51. d.setMsg("成功");
  52. return d;
  53. }
  54. public static ServiceResult ok(Object data, Map<String, Object> additionalProperties) {
  55. ServiceResult d = new ServiceResult();
  56. d.setSucceed(true);
  57. d.setData(data);
  58. d.setCode(Constant.SUCCESS);
  59. d.setMsg("成功");
  60. d.additionalProperties.putAll(additionalProperties);
  61. return d;
  62. }
  63. public ServiceResult(T data) {
  64. this.code = Constant.SUCCESS;
  65. this.subCode = Constant.SUCCESS;
  66. this.additionalProperties = new HashMap();
  67. this.data = data;
  68. }
  69. public ServiceResult(boolean succeed, int code, String msg) {
  70. this.code = Constant.SUCCESS;
  71. this.subCode = Constant.SUCCESS;
  72. this.additionalProperties = new HashMap();
  73. this.succeed = succeed;
  74. this.code = code;
  75. this.msg = msg;
  76. }
  77. public ServiceResult(boolean succeed, T data, String msg) {
  78. this.code = Constant.SUCCESS;
  79. this.subCode = Constant.SUCCESS;
  80. this.additionalProperties = new HashMap();
  81. this.succeed = succeed;
  82. this.data = data;
  83. this.msg = msg;
  84. }
  85. public ServiceResult(boolean succeed, T data, int code, String msg) {
  86. this.code = Constant.SUCCESS;
  87. this.subCode = Constant.SUCCESS;
  88. this.additionalProperties = new HashMap();
  89. this.succeed = succeed;
  90. this.data = data;
  91. this.code = code;
  92. this.msg = msg;
  93. }
  94. public ServiceResult(boolean succeed, String msg) {
  95. this.code = Constant.SUCCESS;
  96. this.subCode = Constant.SUCCESS;
  97. this.additionalProperties = new HashMap();
  98. this.succeed = succeed;
  99. this.msg = msg;
  100. }
  101. public boolean isSucceed() {
  102. return this.succeed;
  103. }
  104. public void setSucceed(boolean succeed) {
  105. this.succeed = succeed;
  106. }
  107. public T getData() {
  108. return this.data;
  109. }
  110. public void setData(T data) {
  111. this.data = data;
  112. }
  113. public String toString() {
  114. return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
  115. }
  116. public String getMsg() {
  117. return this.msg;
  118. }
  119. public void setMsg(String msg) {
  120. this.msg = msg;
  121. }
  122. public int getCode() {
  123. return this.code;
  124. }
  125. public void setCode(int code) {
  126. this.code = code;
  127. }
  128. public int getSubCode() {
  129. return this.subCode;
  130. }
  131. public void setSubCode(int subCode) {
  132. this.subCode = subCode;
  133. }
  134. public Map<String, Object> getAdditionalProperties() {
  135. return this.additionalProperties;
  136. }
  137. public void setAdditionalProperties(String name, Object value) {
  138. this.additionalProperties.put(name, value);
  139. }
  140. public Object getAdditionalProperties(String name) {
  141. return this.additionalProperties.get(name);
  142. }
  143. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/511157
推荐阅读
相关标签
  

闽ICP备14008679号