处理 ResourceMap.java json序列化异常

This commit is contained in:
天爱有情
2023-08-23 11:12:10 +08:00
parent 2f416e67e0
commit 866db7c43b
3 changed files with 34 additions and 11 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>cloud.tianai.captcha</groupId> <groupId>cloud.tianai.captcha</groupId>
<artifactId>tianai-captcha</artifactId> <artifactId>tianai-captcha</artifactId>
<version>1.4.1</version> <version>1.4.2</version>
<name>tianai-captcha</name> <name>tianai-captcha</name>
<description>行为验证码</description> <description>行为验证码</description>
@@ -122,7 +122,7 @@ public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGener
protected ResourceMap requiredRandomGetTemplate(String type, String tag) { protected ResourceMap requiredRandomGetTemplate(String type, String tag) {
ResourceMap templateMap = imageCaptchaResourceManager.randomGetTemplate(type, tag); ResourceMap templateMap = imageCaptchaResourceManager.randomGetTemplate(type, tag);
if (CollectionUtils.isEmpty(templateMap)) { if (templateMap == null || CollectionUtils.isEmpty(templateMap.getResourceMap())) {
throw new ImageCaptchaException("随机获取模板资源失败, 获取到的资源为空, type=" + type + ",tag=" + tag); throw new ImageCaptchaException("随机获取模板资源失败, 获取到的资源为空, type=" + type + ",tag=" + tag);
} }
return templateMap; return templateMap;
@@ -137,7 +137,7 @@ public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGener
} }
protected InputStream getTemplateFile(Map<String, Resource> templateImages, String imageName) { protected InputStream getTemplateFile(ResourceMap templateImages, String imageName) {
Resource resource = templateImages.get(imageName); Resource resource = templateImages.get(imageName);
if (resource == null) { if (resource == null) {
throw new IllegalArgumentException("查找模板异常, 该模板下未找到 ".concat(imageName)); throw new IllegalArgumentException("查找模板异常, 该模板下未找到 ".concat(imageName));
@@ -145,7 +145,7 @@ public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGener
return getResourceInputStream(resource, null); return getResourceInputStream(resource, null);
} }
protected BufferedImage getTemplateImage(Map<String, Resource> templateImages, String imageName) { protected BufferedImage getTemplateImage(ResourceMap templateImages, String imageName) {
InputStream stream = getTemplateFile(templateImages, imageName); InputStream stream = getTemplateFile(templateImages, imageName);
BufferedImage bufferedImage = CaptchaImageUtils.wrapFile2BufferedImage(stream); BufferedImage bufferedImage = CaptchaImageUtils.wrapFile2BufferedImage(stream);
closeStream(stream); closeStream(stream);
@@ -190,7 +190,7 @@ public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGener
return stream; return stream;
} }
protected Optional<BufferedImage> getTemplateImageOfOptional(Map<String, Resource> templateImages, String imageName) { protected Optional<BufferedImage> getTemplateImageOfOptional(ResourceMap templateImages, String imageName) {
Optional<InputStream> optional = getTemplateFileOfOptional(templateImages, imageName); Optional<InputStream> optional = getTemplateFileOfOptional(templateImages, imageName);
if (optional.isPresent()) { if (optional.isPresent()) {
InputStream inputStream = optional.get(); InputStream inputStream = optional.get();
@@ -201,7 +201,7 @@ public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGener
return Optional.empty(); return Optional.empty();
} }
protected Optional<InputStream> getTemplateFileOfOptional(Map<String, Resource> templateImages, String imageName) { protected Optional<InputStream> getTemplateFileOfOptional(ResourceMap templateImages, String imageName) {
Resource resource = templateImages.get(imageName); Resource resource = templateImages.get(imageName);
if (resource == null) { if (resource == null) {
return Optional.empty(); return Optional.empty();
@@ -1,10 +1,10 @@
package cloud.tianai.captcha.resource.common.model.dto; package cloud.tianai.captcha.resource.common.model.dto;
import cloud.tianai.captcha.resource.common.model.dto.Resource;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
/** /**
* @Author: 天爱有情 * @Author: 天爱有情
@@ -12,24 +12,47 @@ import java.util.HashMap;
* @Description 存储一组Resource的Map, 增加tag标记 * @Description 存储一组Resource的Map, 增加tag标记
*/ */
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode
public class ResourceMap extends HashMap<String, Resource> { public class ResourceMap {
private Map<String, Resource> resourceMap;
private String tag; private String tag;
public ResourceMap(String tag) { public ResourceMap(String tag) {
this.tag = tag; this.tag = tag;
this.resourceMap = new HashMap<>();
} }
public ResourceMap(String tag, int initialCapacity) { public ResourceMap(String tag, int initialCapacity) {
super(initialCapacity);
this.tag = tag; this.tag = tag;
this.resourceMap = new HashMap<>(initialCapacity);
} }
public ResourceMap(int initialCapacity) { public ResourceMap(int initialCapacity) {
super(initialCapacity); this.resourceMap = new HashMap<>(initialCapacity);
} }
public ResourceMap() { public ResourceMap() {
} }
private Map<String, Resource> getResourceMapOfCreate() {
if (resourceMap == null) {
resourceMap = new HashMap<>(2);
}
return resourceMap;
}
// ================== Map ==================
public Resource put(String key, Resource value) {
return getResourceMapOfCreate().put(key, value);
}
public Resource get(Object key) {
return getResourceMapOfCreate().get(key);
}
public Resource remove(Object key) {
return getResourceMapOfCreate().remove(key);
}
} }