修复XmlUtil.xmlToBean空节点转换失败问题

This commit is contained in:
Looly 2023-06-16 19:23:19 +08:00
parent d53ac71616
commit 7351aec47d
3 changed files with 72 additions and 0 deletions

View File

@ -19,6 +19,7 @@
* 【extra 】 修复Sftp中exists方法父目录不存在时报错issue#I7CSQ9@Gitee
* 【extra 】 修复xml转json再转bean失败问题issue#3139@Github
* 【poi 】 修复RowUtil传入参数错误问题issue#3139@Github
* 【poi 】 修复XmlUtil.xmlToBean空节点转换失败问题issue#3136@Github
-------------------------------------------------------------------------------------------------------------
# 5.8.19(2023-05-27)

View File

@ -9,6 +9,7 @@ import cn.hutool.core.convert.ConvertException;
import cn.hutool.core.map.MapProxy;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import java.lang.reflect.Type;
@ -79,6 +80,9 @@ public class BeanConverter<T> extends AbstractConverter<T> {
} else if(value instanceof byte[]){
// 尝试反序列化
return ObjectUtil.deserialize((byte[])value);
} else if(StrUtil.isEmptyIfStr(value)){
// issue#3136
return null;
}
throw new ConvertException("Unsupported source type: {}", value.getClass());

View File

@ -0,0 +1,67 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package cn.hutool.core.util;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class Issue3136Test {
@Test
public void xmlToBeanTest() {
final String xmlStr = "<?xml version=\"1.0\" encoding=\"gbk\" ?><response><code>02</code><message></message></response>";
final SmsRes smsRes = XmlUtil.xmlToBean(XmlUtil.parseXml(xmlStr).getDocumentElement(), SmsRes.class);
Assert.assertEquals("02", smsRes.getCode());
Assert.assertNull(smsRes.getMessage());
}
@Data
static class SmsRes {
/**
* 状态码.
*/
private String code;
/**
* 消息.
*/
private Message message;
}
@Data
static class Message {
/**
* 消息项.
*/
private List<MessageItem> item = new ArrayList<>();
}
@Data
static class MessageItem {
/**
* 手机号.
*/
private String desmobile;
/**
* 消息id.
*/
private String msgid;
}
}