15 changed files with 1138 additions and 0 deletions
@ -0,0 +1,32 @@ |
|||
package com.yxt.supervise.cyf.api.appversion; |
|||
|
|||
import com.baomidou.mybatisplus.annotation.IdType; |
|||
import com.baomidou.mybatisplus.annotation.TableId; |
|||
import com.baomidou.mybatisplus.annotation.TableName; |
|||
import com.yxt.common.core.domain.BaseEntity; |
|||
import lombok.Data; |
|||
|
|||
import java.io.Serializable; |
|||
|
|||
/** |
|||
* Created with IntelliJ IDEA. |
|||
* |
|||
* @Auther: dimengzhe |
|||
* @Date: 2020/06/15/11:08 |
|||
* @Description: |
|||
*/ |
|||
@Data |
|||
@TableName("app_version") |
|||
public class AppVersion extends BaseEntity { |
|||
private static final long serialVersionUID = -1024009882612883351L; |
|||
|
|||
|
|||
private String appName;//应用名称
|
|||
private Integer versionCode;//版本编码
|
|||
private String versionName;//版本号
|
|||
private String downloadUrl;//下载地址
|
|||
private String versionInfo;//版本描述
|
|||
private Integer isAlpha;//是否内测:0否,1是
|
|||
private String fileName; |
|||
private boolean forceUpdate; |
|||
} |
@ -0,0 +1,21 @@ |
|||
package com.yxt.supervise.cyf.api.appversion; |
|||
|
|||
import com.yxt.common.core.vo.Vo; |
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @author wangpengfei |
|||
* @date 2023/5/19 9:55 |
|||
*/ |
|||
@Data |
|||
public class AppVersionDetailsVo implements Vo { |
|||
private String id; |
|||
private String appName;//应用名称
|
|||
private Integer versionCode;//版本编码
|
|||
private String versionName;//版本号
|
|||
private String downloadUrl;//下载地址
|
|||
private String versionInfo;//版本描述
|
|||
private Integer isAlpha;//是否内测:0否,1是
|
|||
private String fileName; |
|||
private boolean forceUpdate; |
|||
} |
@ -0,0 +1,21 @@ |
|||
package com.yxt.supervise.cyf.api.appversion; |
|||
|
|||
import com.yxt.common.core.dto.Dto; |
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @author wangpengfei |
|||
* @date 2023/5/19 9:55 |
|||
*/ |
|||
@Data |
|||
public class AppVersionDto implements Dto { |
|||
private String id; |
|||
private String appName;//应用名称
|
|||
private Integer versionCode;//版本编码
|
|||
private String versionName;//版本号
|
|||
private String downloadUrl;//下载地址
|
|||
private String versionInfo;//版本描述
|
|||
private Integer isAlpha;//是否内测:0否,1是
|
|||
private String fileName; |
|||
private boolean forceUpdate; |
|||
} |
@ -0,0 +1,21 @@ |
|||
package com.yxt.supervise.cyf.api.appversion; |
|||
|
|||
import com.yxt.common.core.vo.Vo; |
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @author wangpengfei |
|||
* @date 2023/5/19 9:55 |
|||
*/ |
|||
@Data |
|||
public class AppVersionVo implements Vo { |
|||
private String id; |
|||
private String appName;//应用名称
|
|||
private Integer versionCode;//版本编码
|
|||
private String versionName;//版本号
|
|||
private String downloadUrl;//下载地址
|
|||
private String versionInfo;//版本描述
|
|||
private Integer isAlpha;//是否内测:0否,1是
|
|||
private String fileName; |
|||
private boolean forceUpdate; |
|||
} |
@ -0,0 +1,20 @@ |
|||
package com.yxt.supervise.cyf.app.appversion; |
|||
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
|||
import com.yxt.supervise.cyf.api.appversion.AppVersion; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
|
|||
/** |
|||
* Created with IntelliJ IDEA. |
|||
* |
|||
* @Auther: dimengzhe |
|||
* @Date: 2020/06/15/11:24 |
|||
* @Description: |
|||
*/ |
|||
@Mapper |
|||
public interface AppVersionMapper extends BaseMapper<AppVersion> { |
|||
|
|||
AppVersion getAll(); |
|||
|
|||
AppVersion selectAppVersion(); |
|||
} |
@ -0,0 +1,19 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
|||
<mapper namespace="com.yxt.supervise.cyf.app.appversion.AppVersionMapper"> |
|||
|
|||
|
|||
<select id="getAll" resultType="com.yxt.supervise.cyf.api.appversion.AppVersionVo"> |
|||
select * from app_version where isAlpha=0 order by versionCode desc limit 1 |
|||
</select> |
|||
|
|||
|
|||
<select id="selectAppVersion" resultType="com.yxt.supervise.cyf.api.appversion.AppVersionVo"> |
|||
select * from app_version where isAlpha=1 order by versionCode desc limit 1 |
|||
|
|||
</select> |
|||
|
|||
|
|||
|
|||
|
|||
</mapper> |
@ -0,0 +1,248 @@ |
|||
package com.yxt.supervise.cyf.app.appversion; |
|||
|
|||
|
|||
import com.yxt.common.core.result.ResultBean; |
|||
import com.yxt.supervise.cyf.api.appversion.AppVersion; |
|||
import com.yxt.supervise.cyf.api.appversion.AppVersionDto; |
|||
import com.yxt.supervise.cyf.util.JsonUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Controller; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.ResponseBody; |
|||
|
|||
import javax.servlet.http.HttpServletRequest; |
|||
import javax.servlet.http.HttpServletResponse; |
|||
import javax.servlet.http.HttpSession; |
|||
import java.io.*; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* Created with IntelliJ IDEA. |
|||
* |
|||
* @Auther: dimengzhe |
|||
* @Date: 2020/06/15/10:43 |
|||
* @Description: 更新apk |
|||
*/ |
|||
@Controller |
|||
@RequestMapping("cyf/appVersion") |
|||
public class AppVersionRest { |
|||
|
|||
private String uploadUrl = "http://www.yyundong.com:8083/api/appVersion/download"; |
|||
// private String uploadUrl = "http://26077a35f5.wicp.vip:43609/api/appVersion/download";
|
|||
|
|||
@Autowired |
|||
private AppVersionService appVersionService; |
|||
|
|||
public ResultBean save(AppVersionDto dto){ |
|||
//dto.setDownloadUrl();
|
|||
return appVersionService.save(dto); |
|||
} |
|||
|
|||
// 文件下载相关代码
|
|||
@RequestMapping("/download") |
|||
public Object downloadFile(HttpServletRequest request, HttpServletResponse response) |
|||
throws UnsupportedEncodingException { |
|||
String fileName = request.getParameter("fileName"); |
|||
if (fileName != null) { |
|||
// 设置文件路径
|
|||
String realPath = "D:/mallplus_project/upload/apks"; |
|||
File file = new File(realPath, fileName); |
|||
if (file.exists()) { |
|||
response.setContentType("application/octet-stream"); |
|||
response.addHeader("Content-Disposition", |
|||
"attachment;fileName=" + java.net.URLEncoder.encode(fileName, "UTF-8"));// 设置文件名
|
|||
byte[] buffer = new byte[1024];// 创建数据缓冲区
|
|||
FileInputStream fis = null;// 文件输入流
|
|||
BufferedInputStream bis = null; |
|||
OutputStream os = null; // 输出流
|
|||
byte[] bytes = null; |
|||
ByteArrayOutputStream baos = null; |
|||
try { |
|||
os = response.getOutputStream(); |
|||
fis = new FileInputStream(file); |
|||
bis = new BufferedInputStream(fis); |
|||
baos = new ByteArrayOutputStream(); |
|||
int len; |
|||
while ((len = fis.read(buffer)) != -1) { |
|||
baos.write(buffer, 0, len); |
|||
} |
|||
bytes = baos.toByteArray(); |
|||
response.setContentLength(bytes.length); |
|||
os.write(bytes, 0, bytes.length); |
|||
os.flush(); |
|||
System.out.println("success"); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} finally { |
|||
if (bis != null) { |
|||
try { |
|||
bis.close(); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
if (fis != null) { |
|||
try { |
|||
fis.close(); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
// @RequestMapping(value = "/versioninfo1")
|
|||
// @ResponseBody
|
|||
// public Object appVersion1(HttpServletRequest request) {
|
|||
// Map<String, Object> map = new HashMap<String, Object>();
|
|||
// int versionCode = 1;// 版本编码
|
|||
// String defUrl = "";
|
|||
// String downloadUrl = "";// apk下载地址
|
|||
// String versionName = "";// 版本号
|
|||
// String versionInfo = "";// 版本的更新描述
|
|||
// String fileName = "";// apk名称
|
|||
// int isAlpha = 0;//是否内测,0否,1是
|
|||
// String userName = request.getParameter("userName");//会员账号
|
|||
// // 根据会员账号查询账号信息
|
|||
// UmsMember umsMember = iUmsMemberService.getByUsername(userName);
|
|||
// HttpSession session = request.getSession();
|
|||
// if (umsMember == null) {
|
|||
// // 查询版本记录未删除及为正式版的版本记录
|
|||
// AppVersion av = appVersionService.getAll();
|
|||
// if (null == av) {// 如果没有正式版的版本记录
|
|||
// defUrl = uploadUrl;
|
|||
// av = new AppVersion();
|
|||
// av.setVersionCode(1);
|
|||
// av.setDownloadUrl(defUrl);
|
|||
// versionCode = av.getVersionCode();
|
|||
// downloadUrl = defUrl;
|
|||
// isAlpha = 0;
|
|||
// } else {
|
|||
// versionCode = av.getVersionCode();// 版本编码
|
|||
// versionName = av.getVersionName();// 版本名称即版本号
|
|||
// fileName = av.getFileName();
|
|||
// session.setAttribute("fileName", fileName);
|
|||
// versionInfo = av.getVersionInfo();// 版本的更新描述
|
|||
// defUrl = av.getDownloadUrl();// apk下载地址
|
|||
// downloadUrl = uploadUrl+"?fileName=" + fileName;
|
|||
// isAlpha = av.getIsAlpha();// 是否为内测版本
|
|||
// }
|
|||
// } else {// 若member不为null,则版本可以分为内测人员和非内测人员
|
|||
// int isAlphaUser = umsMember.getIsAlphaUser();
|
|||
// if (isAlphaUser != 0) {//若人员是内测人员,则查询是否有内测版本
|
|||
// AppVersion av = appVersionService.selectAppVersion();// 内测版本
|
|||
// AppVersion av2 = appVersionService.getAll();// 正式版本
|
|||
// if (null == av) {// 若内测版本为null
|
|||
// defUrl = uploadUrl;
|
|||
// // defUrl默认地址需要改config配置文件
|
|||
// av = new AppVersion();
|
|||
// av.setVersionCode(2);
|
|||
// av.setDownloadUrl(defUrl);
|
|||
// versionCode = av.getVersionCode();
|
|||
// downloadUrl = defUrl;
|
|||
// isAlpha = 0;
|
|||
// } else {// 若有内测版本
|
|||
// if (av2 != null) {// 若有正式版本
|
|||
// if (av2.getVersionCode() > av.getVersionCode()) {// 若正式版本编码>内测版本的版本编码
|
|||
// versionCode = av2.getVersionCode();// 正式版本的编码
|
|||
// versionName = av2.getVersionName();
|
|||
// versionInfo = av2.getVersionInfo();
|
|||
// defUrl = av2.getDownloadUrl();
|
|||
// fileName = av.getFileName();
|
|||
// session.setAttribute("fileName", fileName);
|
|||
// downloadUrl = uploadUrl+"?fileName=" + fileName;
|
|||
// isAlpha = av2.getIsAlpha();
|
|||
// } else {
|
|||
// versionCode = av.getVersionCode();// 内测版本编码
|
|||
// versionName = av.getVersionName();// 版本号
|
|||
// versionInfo = av.getVersionInfo();// 版本的更新描述
|
|||
// defUrl = av.getDownloadUrl();// apk下载地址
|
|||
// fileName = av.getFileName();
|
|||
// session.setAttribute("fileName", fileName);
|
|||
// downloadUrl = uploadUrl+"?fileName=" + fileName;
|
|||
// isAlpha = av.getIsAlpha();
|
|||
// }
|
|||
// } else {
|
|||
// versionCode = av.getVersionCode();// 内测版本编码
|
|||
// versionName = av.getVersionName();// 版本号
|
|||
// versionInfo = av.getVersionInfo();// 版本的更新描述
|
|||
// defUrl = av.getDownloadUrl();// apk下载地址
|
|||
// fileName = av.getFileName();
|
|||
// session.setAttribute("fileName", fileName);
|
|||
// downloadUrl = uploadUrl+"?fileName=" + fileName;
|
|||
// isAlpha = av.getIsAlpha();
|
|||
// }
|
|||
// }
|
|||
// } else {// 如果不是内测人员
|
|||
// AppVersion av = appVersionService.getAll();// 正式版本倒序第一个
|
|||
// if (null == av) {//正式版本为null
|
|||
// defUrl = uploadUrl;
|
|||
// av = new AppVersion();
|
|||
// av.setVersionCode(1);// 将版本编码设置为2
|
|||
// av.setDownloadUrl(defUrl);// 将下载地址设置为默认地址
|
|||
// versionCode = av.getVersionCode();
|
|||
// downloadUrl = defUrl;
|
|||
// isAlpha = 0;
|
|||
// } else {
|
|||
// versionCode = av.getVersionCode();
|
|||
// versionName = av.getVersionName();
|
|||
// versionInfo = av.getVersionInfo();
|
|||
// defUrl = av.getDownloadUrl();
|
|||
// fileName = av.getFileName();
|
|||
// session.setAttribute("fileName", fileName);
|
|||
// downloadUrl = uploadUrl+"?fileName=" + fileName;
|
|||
// isAlpha = av.getIsAlpha();
|
|||
// }
|
|||
//
|
|||
// }
|
|||
// }
|
|||
//
|
|||
// AppVersion appVersion = new AppVersion();
|
|||
// appVersion.setDownloadUrl(downloadUrl); //apk下载地址新
|
|||
// appVersion.setVersionCode(versionCode);//版本编码
|
|||
// appVersion.setVersionInfo(versionInfo);//版本的更新的描述
|
|||
// appVersion.setVersionName(versionName);//版本号
|
|||
// appVersion.setFileName(fileName);//应用名称
|
|||
// appVersion.setForceUpdate(false);
|
|||
// String appVersionNew = JsonUtils.objectToJson(appVersion);
|
|||
// return new ResultBean().success().setData(appVersionNew);
|
|||
// }
|
|||
|
|||
@RequestMapping(value = "/versioninfo") |
|||
@ResponseBody |
|||
public Object appVersion(HttpServletRequest request) { |
|||
Map<String, Object> map = new HashMap<String, Object>(); |
|||
int versionCode = 101;// 版本编码
|
|||
String versionName = "101";// 版本号
|
|||
String downloadUrl = "";// apk下载地址
|
|||
String versionInfo = "";// 版本的更新描述
|
|||
String fileName = "";// apk名称
|
|||
int isAlpha = 0;//是否内测,0否,1是
|
|||
|
|||
AppVersion appVersion = new AppVersion(); |
|||
appVersion.setVersionCode(versionCode);//版本编码
|
|||
appVersion.setVersionName(versionName);//版本号
|
|||
appVersion.setDownloadUrl(downloadUrl); //apk下载地址新
|
|||
appVersion.setVersionInfo(versionInfo);//版本的更新的描述
|
|||
appVersion.setFileName(fileName);//应用名称
|
|||
appVersion.setForceUpdate(false); |
|||
|
|||
// 查询版本记录未删除及为正式版的版本记录
|
|||
AppVersion av = appVersionService.getAll(); |
|||
if (null != av) {// 如果有正式版的版本记录
|
|||
appVersion.setVersionCode(av.getVersionCode());//版本编码
|
|||
appVersion.setVersionName(av.getVersionName());//版本号
|
|||
appVersion.setDownloadUrl(av.getDownloadUrl()); //apk下载地址新
|
|||
appVersion.setVersionInfo(av.getVersionInfo());//版本的更新的描述
|
|||
appVersion.setFileName(av.getFileName());//应用名称
|
|||
} |
|||
|
|||
String appVersionNew = JsonUtils.objectToJson(appVersion); |
|||
|
|||
return new ResultBean().success().setData(appVersionNew); |
|||
} |
|||
} |
@ -0,0 +1,38 @@ |
|||
package com.yxt.supervise.cyf.app.appversion; |
|||
|
|||
import cn.hutool.core.bean.BeanUtil; |
|||
import com.baomidou.mybatisplus.extension.service.IService; |
|||
import com.yxt.common.base.service.MybatisBaseService; |
|||
import com.yxt.common.core.domain.BaseEntity; |
|||
import com.yxt.common.core.result.ResultBean; |
|||
import com.yxt.supervise.cyf.api.appversion.AppVersion; |
|||
import com.yxt.supervise.cyf.api.appversion.AppVersionDto; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
/** |
|||
* Created with IntelliJ IDEA. |
|||
* |
|||
* @Auther: dimengzhe |
|||
* @Date: 2020/06/15/11:17 |
|||
* @Description: 版本更新 |
|||
*/ |
|||
@Service |
|||
public class AppVersionService extends MybatisBaseService<AppVersionMapper,AppVersion> { |
|||
|
|||
public ResultBean save(AppVersionDto dto){ |
|||
AppVersion entity=new AppVersion(); |
|||
BeanUtil.copyProperties(dto, entity); |
|||
return new ResultBean().success().setMsg("新增成功"); |
|||
}; |
|||
/** |
|||
* 查询apk版本中正式版本按版本编码倒叙排序 |
|||
* @return |
|||
*/ |
|||
public AppVersion getAll(){ |
|||
return baseMapper.getAll(); |
|||
}; |
|||
|
|||
public AppVersion selectAppVersion(){ |
|||
return baseMapper.selectAppVersion(); |
|||
} |
|||
} |
@ -0,0 +1,418 @@ |
|||
package com.yxt.supervise.cyf.util; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonFilter; |
|||
import com.fasterxml.jackson.core.JsonParseException; |
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.core.type.TypeReference; |
|||
import com.fasterxml.jackson.databind.*; |
|||
import com.fasterxml.jackson.databind.ser.FilterProvider; |
|||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; |
|||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; |
|||
import com.google.common.collect.Lists; |
|||
import com.google.common.collect.Maps; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import net.sf.json.JsonConfig; |
|||
import net.sf.json.util.PropertyFilter; |
|||
import org.springframework.core.annotation.AnnotationUtils; |
|||
import org.springframework.util.StringUtils; |
|||
|
|||
import java.io.IOException; |
|||
import java.io.OutputStream; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.*; |
|||
|
|||
/** |
|||
* json对象映射工具类之jackson封装 |
|||
*/ |
|||
@Slf4j |
|||
public class JsonUtils { |
|||
|
|||
private static ObjectMapper objectMapper = null; |
|||
|
|||
static { |
|||
objectMapper = new ObjectMapper(); |
|||
// 设置默认日期格式
|
|||
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); |
|||
// 提供其它默认设置
|
|||
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); |
|||
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); |
|||
objectMapper.setFilters(new SimpleFilterProvider() |
|||
.setFailOnUnknownId(false)); |
|||
objectMapper.registerModule(new MyModule()); |
|||
} |
|||
|
|||
/** |
|||
* 将对象转换成json字符串格式(默认将转换所有的属性) |
|||
* |
|||
* @param value |
|||
* @return |
|||
*/ |
|||
public static String toJsonStr(Object value) { |
|||
try { |
|||
return objectMapper.writeValueAsString(value); |
|||
} catch (JsonProcessingException e) { |
|||
log.error("Json转换失败", e); |
|||
throw new RuntimeException(e); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 将对象转换成json字符串格式 |
|||
* |
|||
* @param value 需要转换的对象 |
|||
* @param properties 需要转换的属性 |
|||
*/ |
|||
public static String toJsonStr(Object value, String[] properties) { |
|||
try { |
|||
SimpleBeanPropertyFilter sbp = SimpleBeanPropertyFilter |
|||
.filterOutAllExcept(properties); |
|||
FilterProvider filterProvider = new SimpleFilterProvider() |
|||
.addFilter("propertyFilterMixIn", sbp); |
|||
return objectMapper.writer(filterProvider) |
|||
.writeValueAsString(value); |
|||
} catch (Exception e) { |
|||
log.error("Json转换失败", e); |
|||
throw new RuntimeException(e); |
|||
} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 将对象转换成json字符串格式 |
|||
* |
|||
* @param value 需要转换的对象 |
|||
* @param properties2Exclude 需要排除的属性 |
|||
*/ |
|||
public static String toJsonStrWithExcludeProperties(Object value, |
|||
String[] properties2Exclude) { |
|||
try { |
|||
SimpleBeanPropertyFilter sbp = SimpleBeanPropertyFilter |
|||
.serializeAllExcept(properties2Exclude); |
|||
FilterProvider filterProvider = new SimpleFilterProvider() |
|||
.addFilter("propertyFilterMixIn", sbp); |
|||
return objectMapper.writer(filterProvider) |
|||
.writeValueAsString(value); |
|||
} catch (Exception e) { |
|||
log.error("Json转换失败", e); |
|||
throw new RuntimeException(e); |
|||
} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 将对象json格式直接写出到流对象中(默认将转换所有的属性) |
|||
* |
|||
* @param out |
|||
* @return |
|||
*/ |
|||
public static void writeJsonStr(OutputStream out, Object value) { |
|||
try { |
|||
objectMapper.writeValue(out, value); |
|||
} catch (Exception e) { |
|||
log.error("Json转换失败", e); |
|||
throw new RuntimeException(e); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 将对象json格式直接写出到流对象中 |
|||
* |
|||
* @param value 需要转换的对象(注意,需要在要转换的对象中定义JsonFilter注解) |
|||
* @param properties 需要转换的属性 |
|||
*/ |
|||
public static void writeJsonStr(OutputStream out, Object value, |
|||
String[] properties) { |
|||
|
|||
try { |
|||
objectMapper.writer( |
|||
new SimpleFilterProvider().addFilter( |
|||
AnnotationUtils |
|||
.getValue( |
|||
AnnotationUtils.findAnnotation( |
|||
value.getClass(), |
|||
JsonFilter.class)) |
|||
.toString(), SimpleBeanPropertyFilter |
|||
.filterOutAllExcept(properties))) |
|||
.writeValue(out, value); |
|||
} catch (Exception e) { |
|||
log.error("Json转换失败", e); |
|||
throw new RuntimeException(e); |
|||
} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 将对象转换成json字符串格式 |
|||
* |
|||
* @param value 需要转换的对象 |
|||
* @param properties2Exclude 需要排除的属性(注意,需要在要转换的对象中定义JsonFilter注解) |
|||
*/ |
|||
public static void writeJsonStrWithExcludeProperties(OutputStream out, |
|||
Object value, String[] properties2Exclude) { |
|||
try { |
|||
objectMapper.writer( |
|||
new SimpleFilterProvider().addFilter( |
|||
AnnotationUtils |
|||
.getValue( |
|||
AnnotationUtils.findAnnotation( |
|||
value.getClass(), |
|||
JsonFilter.class)) |
|||
.toString(), SimpleBeanPropertyFilter |
|||
.serializeAllExcept(properties2Exclude))) |
|||
.writeValue(out, value); |
|||
} catch (Exception e) { |
|||
log.error("Json转换失败", e); |
|||
throw new RuntimeException(e); |
|||
} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 反序列化POJO或简单Collection如List<String>. |
|||
* <p> |
|||
* 如果JSON字符串为Null或"null"字符串, 返回Null. 如果JSON字符串为"[]", 返回空集合. |
|||
* <p> |
|||
* 如需反序列化复杂Collection如List<MyBean>, 请使用fromJson(String, JavaType) |
|||
*/ |
|||
public static <T> T fromJson(String jsonString, Class<T> clazz) { |
|||
if (StringUtils.isEmpty(jsonString)) { |
|||
return null; |
|||
} |
|||
|
|||
try { |
|||
return objectMapper.readValue(jsonString, clazz); |
|||
} catch (IOException e) { |
|||
log.warn("parse json string error:" + jsonString, e); |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
@SuppressWarnings({"unchecked", "unused"}) |
|||
public static List<Object> readJsonList(String jsondata, Object object) { |
|||
try { |
|||
List<LinkedHashMap<String, Object>> list = objectMapper.readValue( |
|||
jsondata, List.class); |
|||
|
|||
List<Object> objects = Lists.newArrayList(); |
|||
System.out.println(list.size()); |
|||
for (int i = 0; i < list.size(); i++) { |
|||
Map<String, Object> map = list.get(i); |
|||
Set<String> set = map.keySet(); |
|||
for (Iterator<String> it = set.iterator(); it.hasNext(); ) { |
|||
String key = it.next(); |
|||
System.out.println(key + ":" + map.get(key)); |
|||
} |
|||
} |
|||
} catch (JsonParseException e) { |
|||
e.printStackTrace(); |
|||
} catch (JsonMappingException e) { |
|||
e.printStackTrace(); |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 单独解析某一个json的key值 |
|||
* |
|||
* @param @param jsonText |
|||
* @param @param key |
|||
* @param @return 设定文件 |
|||
* @return JsonNode 返回类型 |
|||
* @throws |
|||
* @Title: getjsonvalue |
|||
* @Description: TODO(这里用一句话描述这个方法的作用) |
|||
*/ |
|||
public static JsonNode getjsonvalue(String jsonText, String key) { |
|||
|
|||
try { |
|||
ObjectMapper mapper = new ObjectMapper(); |
|||
JsonNode rootNode = mapper.readTree(jsonText); // 读取Json
|
|||
|
|||
return rootNode.path(key); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
|
|||
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) { |
|||
ObjectMapper mapper = new ObjectMapper(); |
|||
return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); |
|||
} |
|||
|
|||
public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz) throws Exception { |
|||
List<Map<String, Object>> list = (List) objectMapper.readValue(jsonArrayStr, new TypeReference<List<T>>() { |
|||
}); |
|||
List<T> result = new ArrayList(); |
|||
Iterator var4 = list.iterator(); |
|||
|
|||
while (var4.hasNext()) { |
|||
Map<String, Object> map = (Map) var4.next(); |
|||
result.add(map2pojo(map, clazz)); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
public static <T> T map2pojo(Map map, Class<T> clazz) { |
|||
return objectMapper.convertValue(map, clazz); |
|||
} |
|||
|
|||
/** |
|||
* 解析json属性,放到实体里面去 |
|||
* |
|||
* @param @param jsondata |
|||
* @param @param collectionClass |
|||
* @param @return 设定文件 |
|||
* @return List<SpecVO> 返回类型 |
|||
* @throws |
|||
* @Title: readJsonList |
|||
* @Description: TODO(这里用一句话描述这个方法的作用) |
|||
*/ |
|||
@SuppressWarnings("unchecked") |
|||
public static List<Object> readJsonList(String jsondata, Class<?> collectionClass) { |
|||
try { |
|||
ObjectMapper mapper = new ObjectMapper(); |
|||
JavaType javaType = getCollectionType(ArrayList.class, collectionClass); |
|||
List<Object> lst = (List<Object>) mapper.readValue(jsondata, javaType); |
|||
|
|||
return lst; |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* json 转map |
|||
* |
|||
* @param @param jsondata |
|||
* @param @return 设定文件 |
|||
* @return Map<String, Map < String, Object>> 返回类型 |
|||
* @throws |
|||
* @Title: readJsonMap |
|||
* @Description: TODO(这里用一句话描述这个方法的作用) |
|||
*/ |
|||
@SuppressWarnings("unchecked") |
|||
public static Map<String, Object> readJsonToMap(String jsondata) { |
|||
try { |
|||
Map<String, Object> maps = objectMapper.readValue(jsondata, Map.class); |
|||
//System.out.println(maps);
|
|||
return maps; |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
@SuppressWarnings("unchecked") |
|||
public static Map<String, Object> readJsonToMap1(String jsondata) { |
|||
try { |
|||
Map<String, Object> maps = objectMapper.readValue(jsondata, Map.class); |
|||
//System.out.println(maps);
|
|||
return maps; |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 设置过滤值为空的属性,使得生成的 json 字符串只包含非空的值 |
|||
* |
|||
* @return |
|||
*/ |
|||
public static JsonConfig getJsonConfig() { |
|||
JsonConfig jsonConfig = new JsonConfig(); |
|||
jsonConfig.registerJsonValueProcessor(java.sql.Timestamp.class, new JsonValueProcessorImpl()); |
|||
jsonConfig.setJsonPropertyFilter(new PropertyFilter() { |
|||
@Override |
|||
public boolean apply(Object source, String name, Object value) { |
|||
return value == null; |
|||
} |
|||
}); |
|||
|
|||
jsonConfig.setIgnoreDefaultExcludes(false); // 设置默认忽略
|
|||
jsonConfig.setExcludes(new String[]{"dbName", "isDel"}); // 此处是亮点,只要将所需忽略字段加到数组中即可,在实际测试中,我发现在所返回数组中,存在大量无用属性,
|
|||
|
|||
return jsonConfig; |
|||
} |
|||
|
|||
/** |
|||
* 将对象转换成json字符串。 |
|||
*/ |
|||
public static String objectToJson(Object data) { |
|||
try { |
|||
String string = objectMapper.writeValueAsString(data); |
|||
return string; |
|||
} catch (JsonProcessingException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 将json结果集转化为对象 |
|||
* |
|||
* @param jsonData json数据 |
|||
* @param beanType 对象中的object类型 |
|||
*/ |
|||
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) { |
|||
try { |
|||
if (ValidatorUtils.notEmpty(jsonData)) { |
|||
T t = objectMapper.readValue(jsonData, beanType); |
|||
return t; |
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 将json数据转换成pojo对象list |
|||
*/ |
|||
public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) { |
|||
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, beanType); |
|||
try { |
|||
List<T> list = objectMapper.readValue(jsonData, javaType); |
|||
return list; |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
public static void main(String[] args) { |
|||
Map<String, Object> userData = Maps.newHashMap(); |
|||
Map<String, Object> nameStruct = Maps.newHashMap(); |
|||
nameStruct.put("firstName", "张三"); |
|||
nameStruct.put("lastName", "你大爷"); |
|||
|
|||
System.out.println(JsonUtils.toJsonStr(nameStruct)); |
|||
userData.put("name", nameStruct); |
|||
userData.put("age", 20); |
|||
List<String> stringList = Lists.newArrayList("A", "B", "C"); |
|||
System.out.println(JsonUtils.toJsonStr(userData)); |
|||
System.out.println(JsonUtils.toJsonStr(stringList)); |
|||
// String[] arr = {"37","38","41","42","43","44","45","1693","1694","1695","1696"};
|
|||
// System.out.println(toJsonStr(arr));
|
|||
|
|||
String ss = "{\"address\": \"address2\",\"name\":\"haha2\"}"; |
|||
|
|||
Map<String, Object> map = readJsonToMap(ss); |
|||
|
|||
if (map != null) { |
|||
System.out.println(map.get("address")); |
|||
} |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,49 @@ |
|||
/** |
|||
* |
|||
*/ |
|||
package com.yxt.supervise.cyf.util; |
|||
|
|||
|
|||
import net.sf.json.JsonConfig; |
|||
import net.sf.json.processors.JsonValueProcessor; |
|||
|
|||
import java.text.SimpleDateFormat; |
|||
import java.util.Date; |
|||
|
|||
/** |
|||
* <p>Title: JsonValueProcessorImpl.java</p> |
|||
* <p>Description: net.js.json 特殊值处理</p> |
|||
* <p>Copyright: Copyright (c) 2014-2018</p> |
|||
* <p>Company: leimingtech.com</p> |
|||
* |
|||
* @author linjm |
|||
* @version 1.0 |
|||
* @date 2015年7月17日 |
|||
*/ |
|||
public class JsonValueProcessorImpl implements JsonValueProcessor { |
|||
|
|||
private String format = "yyyy-MM-dd HH:mm:ss"; |
|||
|
|||
@Override |
|||
public Object processArrayValue(Object value, JsonConfig jsonConfig) { |
|||
String[] obj = {}; |
|||
if (value instanceof Date[]) { |
|||
SimpleDateFormat sdf = new SimpleDateFormat(format); |
|||
Date[] date = (Date[]) value; |
|||
for (int i = 0; i < date.length; i++) { |
|||
obj[i] = sdf.format(date[i]); |
|||
} |
|||
} |
|||
return obj; |
|||
} |
|||
|
|||
@Override |
|||
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { |
|||
if (value instanceof Date) { |
|||
String str = new SimpleDateFormat(format).format(value); |
|||
return str; |
|||
} |
|||
return value.toString(); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,10 @@ |
|||
package com.yxt.supervise.cyf.util; |
|||
|
|||
import com.fasterxml.jackson.databind.module.SimpleModule; |
|||
|
|||
public class MyModule extends SimpleModule { |
|||
@Override |
|||
public void setupModule(SetupContext context) { |
|||
context.setMixInAnnotations(Object.class, PropertyFilterMixIn.class); |
|||
} |
|||
} |
@ -0,0 +1,8 @@ |
|||
package com.yxt.supervise.cyf.util; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonFilter; |
|||
|
|||
@JsonFilter("propertyFilterMixIn") |
|||
public class PropertyFilterMixIn { |
|||
|
|||
} |
@ -0,0 +1,23 @@ |
|||
package com.yxt.supervise.cyf.util; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import java.io.Serializable; |
|||
|
|||
@Data |
|||
public class TimeSecound implements Serializable { |
|||
long days;//天
|
|||
long hours; //小时
|
|||
long mins; //分钟
|
|||
long sc; //秒
|
|||
|
|||
public TimeSecound() { |
|||
} |
|||
|
|||
public TimeSecound(long days, long hours, long mins, long sc) { |
|||
this.days = days; |
|||
this.hours = hours; |
|||
this.mins = mins; |
|||
this.sc = sc; |
|||
} |
|||
} |
@ -0,0 +1,200 @@ |
|||
package com.yxt.supervise.cyf.util; |
|||
|
|||
|
|||
import java.math.BigDecimal; |
|||
import java.util.Collection; |
|||
import java.util.Date; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.regex.Matcher; |
|||
import java.util.regex.Pattern; |
|||
|
|||
public class ValidatorUtils { |
|||
|
|||
/** |
|||
* 判断内容不为空 |
|||
* |
|||
* @param str |
|||
* @return |
|||
*/ |
|||
public static boolean notEmpty(Object str) { |
|||
if (str != null && str.toString().trim().length() > 0) { |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 判断内容不为空(包含数组) |
|||
* |
|||
* @param str |
|||
* @return |
|||
*/ |
|||
@SuppressWarnings("unchecked") |
|||
public static boolean notEmptyIncludeArray(Object str) { |
|||
if (str != null && String.valueOf(str).trim().length() > 0) { |
|||
if (str instanceof Object[]) {// 增加了数组长度判断
|
|||
Object[] array = (Object[]) str; |
|||
if (array.length > 0) |
|||
return true; |
|||
|
|||
return false; |
|||
} else if (str instanceof List<?>) {// 增加了数组长度判断
|
|||
List<Object> list = (List<Object>) str; |
|||
if (list.size() > 0) |
|||
return true; |
|||
|
|||
return false; |
|||
} |
|||
|
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 判断内容是空 |
|||
* |
|||
* @param str |
|||
* @return |
|||
*/ |
|||
public static boolean isEmpty(Object str) { |
|||
if (str == null || str.toString().trim().length() == 0) { |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 检查对象是否为空 |
|||
* |
|||
* @param obj 要检查的数据(数据类型: String、Number、Boolean、Collection、Map、Object[]) |
|||
* @return true: 为空; false: 不为空 <li>String:值为 null、""、"0" 时返回 true <li> |
|||
* Number:值为 null、0 时返回 true <li>Boolean:值为 null、false 时返回 true <li> |
|||
* Collection:值为 null、size=0 时返回 true <li>Map:值为 null、size=0 时返回 |
|||
* true <li>Object[]:值为 null、length=0 时返回 true |
|||
*/ |
|||
@SuppressWarnings("unchecked") |
|||
public static boolean empty(Object obj) { |
|||
if (obj == null) { |
|||
return true; |
|||
} else if (obj instanceof String && (obj.equals(""))) { |
|||
return true; |
|||
} else if (obj instanceof Number && ((Number) obj).doubleValue() == 0) { |
|||
return true; |
|||
} else if (obj instanceof Boolean && !((Boolean) obj)) { |
|||
return true; |
|||
} else if (obj instanceof Collection && ((Collection) obj).isEmpty()) { |
|||
return true; |
|||
} else if (obj instanceof Map && ((Map) obj).isEmpty()) { |
|||
return true; |
|||
} else if (obj instanceof Object[] && ((Object[]) obj).length == 0) { |
|||
return true; |
|||
} |
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* 判断是否是合法邮箱地址 |
|||
* |
|||
* @param email |
|||
* @return |
|||
*/ |
|||
public static boolean isEmail(String email) { |
|||
Pattern p = Pattern |
|||
.compile("^\\w+([\\-+.]\\w+)*@\\w+([-.]\\w+)*\\.[a-z]{2,3}"); |
|||
Matcher m = p.matcher(email); |
|||
return m.matches(); |
|||
} |
|||
|
|||
/** |
|||
* 只包含英文字母和数字、下划线 |
|||
* |
|||
* @param str |
|||
* @return |
|||
*/ |
|||
public static boolean onlyNumAndChar(String str) { |
|||
String regex = "^[a-zA-Z0-9_]+$"; |
|||
Pattern pattern = Pattern.compile(regex); |
|||
return pattern.matcher(str).matches(); |
|||
} |
|||
|
|||
/** |
|||
* 必须包含字母 |
|||
* |
|||
* @param str |
|||
* @return |
|||
*/ |
|||
public static boolean hasLetterAndNum(String str) { |
|||
Pattern pattern = Pattern.compile("^(?=.*[a-zA-Z].*).{6,}$"); |
|||
return pattern.matcher(str).matches(); |
|||
} |
|||
|
|||
/** |
|||
* 是否长度符合 |
|||
* |
|||
* @param str |
|||
* @param min 最小 |
|||
* @param max 最大 |
|||
* @return |
|||
*/ |
|||
public static boolean lengthBetween(String str, int min, int max) { |
|||
return str.length() >= min && str.length() <= max; |
|||
} |
|||
|
|||
/** |
|||
* 判断字符串是否是数字 |
|||
* |
|||
* @param str |
|||
* @return |
|||
*/ |
|||
public static boolean isNumeric(String str) { |
|||
Pattern pattern = Pattern.compile("[0-9]*"); |
|||
Matcher isNum = pattern.matcher(str); |
|||
if (!isNum.matches()) { |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
public static Double rountTwo(Double num) { |
|||
if (num != null) { |
|||
BigDecimal b = new BigDecimal(num); |
|||
Double d = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); |
|||
return d; |
|||
} else { |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static String doubleTrans(double d) { |
|||
if (Math.round(d) - d == 0) { |
|||
return String.valueOf((long) d); |
|||
} |
|||
return String.valueOf(d); |
|||
} |
|||
|
|||
/** |
|||
* 获取结束时间与当前的时间差 |
|||
* |
|||
* @param endTime |
|||
* @return |
|||
*/ |
|||
public static TimeSecound getTimeSecound(Date endTime) { |
|||
|
|||
long diff = endTime.getTime() - System.currentTimeMillis();// 这样得到的差值是微秒级别
|
|||
|
|||
long days = diff / (1000 * 60 * 60 * 24);//天
|
|||
|
|||
long hours = (diff - days * (1000 * 60 * 60 * 24)) |
|||
/ (1000 * 60 * 60); //小时
|
|||
long mins = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60); //小时
|
|||
long sc = (diff - days * (1000 * 60 * 60 * 24) - hours |
|||
* (1000 * 60 * 60) - mins * (1000 * 60)) / (1000); // 秒
|
|||
|
|||
return new TimeSecound(days, hours, mins, sc); |
|||
} |
|||
} |
Loading…
Reference in new issue