12 changed files with 188 additions and 25 deletions
@ -0,0 +1,32 @@ |
|||
package org.dromara.platform.domain; |
|||
|
|||
public class Attachment { |
|||
private String url; |
|||
private String name; |
|||
|
|||
// 默认构造函数
|
|||
public Attachment() {} |
|||
|
|||
// 带参数的构造函数
|
|||
public Attachment(String url, String name) { |
|||
this.url = url; |
|||
this.name = name; |
|||
} |
|||
|
|||
// Getter 和 Setter 方法
|
|||
public String getUrl() { |
|||
return url; |
|||
} |
|||
|
|||
public void setUrl(String url) { |
|||
this.url = url; |
|||
} |
|||
|
|||
public String getName() { |
|||
return name; |
|||
} |
|||
|
|||
public void setName(String name) { |
|||
this.name = name; |
|||
} |
|||
} |
@ -0,0 +1,75 @@ |
|||
package org.dromara.platform.domain; |
|||
|
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.core.type.TypeReference; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
|
|||
import java.util.List; |
|||
|
|||
public class AttachmentSerializer { |
|||
|
|||
private final ObjectMapper objectMapper = new ObjectMapper(); |
|||
|
|||
/** |
|||
* 将List<Attachment>序列化为JSON字符串。 |
|||
* |
|||
* @param attachment 包含附件信息的列表 |
|||
* @return 序列化后的JSON字符串 |
|||
*/ |
|||
public String serializeAttachments(List<Attachment> attachment) { |
|||
try { |
|||
// 使用ObjectMapper将List<Attachment>转换为JSON字符串
|
|||
return objectMapper.writeValueAsString(attachment); |
|||
} catch (JsonProcessingException e) { |
|||
// 处理可能发生的异常
|
|||
e.printStackTrace(); |
|||
// 根据实际情况决定如何处理错误,这里简单地返回null
|
|||
return null; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 将JSON字符串反序列化为List<Attachment>。 |
|||
* |
|||
* @param attachmentJson JSON格式的字符串 |
|||
* @return 反序列化后的List<Attachment> |
|||
*/ |
|||
public List<Attachment> deserializeAttachments(String attachmentJson) { |
|||
try { |
|||
// 使用ObjectMapper将JSON字符串转换回List<Attachment>
|
|||
return objectMapper.readValue(attachmentJson, new TypeReference<List<Attachment>>() {}); |
|||
} catch (JsonProcessingException e) { |
|||
// 处理可能发生的异常
|
|||
e.printStackTrace(); |
|||
// 根据实际情况决定如何处理错误,这里简单地返回null
|
|||
return null; |
|||
} |
|||
} |
|||
|
|||
// 测试方法
|
|||
public static void main(String[] args) { |
|||
AttachmentSerializer serializer = new AttachmentSerializer(); |
|||
|
|||
// 创建一些模拟数据
|
|||
List<Attachment> attachmentList = List.of( |
|||
new Attachment("http://example.com/file1.pdf", "File One"), |
|||
new Attachment("http://example.com/file2.docx", "File Two") |
|||
); |
|||
|
|||
// 序列化
|
|||
String serialized = serializer.serializeAttachments(attachmentList); |
|||
System.out.println(serialized); // 输出序列化后的JSON字符串
|
|||
|
|||
// 反序列化
|
|||
List<Attachment> deserializedAttachments = serializer.deserializeAttachments(serialized); |
|||
System.out.println("Deserialized:"); |
|||
if (deserializedAttachments != null) { |
|||
for (Attachment attachment : deserializedAttachments) { |
|||
System.out.println("URL: " + attachment.getUrl() + ", Name: " + attachment.getName()); |
|||
} |
|||
} else { |
|||
System.out.println("Failed to deserialize the JSON string."); |
|||
} |
|||
|
|||
} |
|||
} |
Loading…
Reference in new issue