mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复XmlUtil.xmlToBean空节点转换失败问题
This commit is contained in:
parent
d53ac71616
commit
7351aec47d
@ -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)
|
||||
|
@ -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());
|
||||
|
67
hutool-core/src/test/java/cn/hutool/core/util/Issue3136Test.java
Executable file
67
hutool-core/src/test/java/cn/hutool/core/util/Issue3136Test.java
Executable 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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user