add method

This commit is contained in:
Looly 2021-08-22 19:06:25 +08:00
parent 4acfe47bbe
commit f06a04a965
5 changed files with 59 additions and 5 deletions

View File

@ -17,6 +17,8 @@
* 【poi 】 ExcelBase增加renameSheet、cloneSheetissue#I466ZZ@Gitee
* 【core 】 ListUtil增加splitAvg方法pr#397@Gitee
* 【poi 】 Excel07SaxReader支持数字类型sheet名称、支持sheetName:名称前缀issue#I46OMA@Gitee
* 【extra 】 Mail增加build方法issue#I46LGE@Gitee
* 【core 】 XmlUtil增加beanToXml重载支持忽略null
### 🐞Bug修复
* 【core 】 修复MapUtil.sort比较器不一致返回原map的问题issue#I46AQJ@Gitee

View File

@ -1270,10 +1270,25 @@ public class XmlUtil {
* @since 5.2.4
*/
public static Document beanToXml(Object bean, String namespace) {
return beanToXml(bean, namespace, false);
}
/**
* 将Bean转换为XML
*
* @param bean Bean对象
* @param namespace 命名空间可以为null
* @param ignoreNull 时候忽略值为{@code null}的属性
* @return XML
* @see JAXBUtil#beanToXml(Object)
* @since 5.7.10
*/
public static Document beanToXml(Object bean, String namespace, boolean ignoreNull) {
if (null == bean) {
return null;
}
return mapToXml(BeanUtil.beanToMap(bean), bean.getClass().getSimpleName(), namespace);
return mapToXml(BeanUtil.beanToMap(bean, false, ignoreNull),
bean.getClass().getSimpleName(), namespace);
}
/**

View File

@ -195,6 +195,33 @@ public class XmlUtilTest {
Assert.assertEquals("2020/04/15 21:01:21", value);
}
@Test
public void beanToXmlIgnoreNullTest() {
@Data
class TestBean {
private String ReqCode;
private String AccountName;
private String Operator;
private String ProjectCode;
private String BankCode;
}
final TestBean testBean = new TestBean();
testBean.setReqCode("1111");
testBean.setAccountName("账户名称");
testBean.setOperator("cz");
testBean.setProjectCode(null);
testBean.setBankCode("00001");
// 不忽略空字段情况下保留自闭标签
Document doc = XmlUtil.beanToXml(testBean, null, false);
Assert.assertNotNull(XmlUtil.getElement(doc.getDocumentElement(), "ProjectCode"));
// 忽略空字段情况下无自闭标签
doc = XmlUtil.beanToXml(testBean, null, true);
Assert.assertNull(XmlUtil.getElement(doc.getDocumentElement(), "ProjectCode"));
}
@Test
public void xmlToBeanTest() {
@Data

View File

@ -1,5 +1,6 @@
package cn.hutool.extra.mail;
import cn.hutool.core.builder.Builder;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
@ -34,7 +35,7 @@ import java.util.Date;
* @author looly
* @since 3.2.0
*/
public class Mail {
public class Mail implements Builder<MimeMessage> {
/**
* 邮箱帐户信息以及一些客户端配置信息
@ -364,6 +365,15 @@ public class Mail {
}
// --------------------------------------------------------------- Getters and Setters end
@Override
public MimeMessage build() {
try {
return buildMsg();
} catch (MessagingException e) {
throw new MailException(e);
}
}
/**
* 发送
*

View File

@ -345,7 +345,7 @@ public class MailUtil {
* @since 4.6.3
*/
public static String send(MailAccount mailAccount, Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content, Map<String, InputStream> imageMap,
boolean isHtml, File... files) {
boolean isHtml, File... files) {
return send(mailAccount, false, tos, ccs, bccs, subject, content, imageMap, isHtml, files);
}
@ -357,7 +357,7 @@ public class MailUtil {
* @return {@link Session}
* @since 5.5.7
*/
public static Session getSession(MailAccount mailAccount, boolean isSingleton){
public static Session getSession(MailAccount mailAccount, boolean isSingleton) {
Authenticator authenticator = null;
if (mailAccount.isAuth()) {
authenticator = new UserPassAuthenticator(mailAccount.getUser(), mailAccount.getPass());
@ -386,7 +386,7 @@ public class MailUtil {
* @since 4.6.3
*/
private static String send(MailAccount mailAccount, boolean useGlobalSession, Collection<String> tos, Collection<String> ccs, Collection<String> bccs, String subject, String content,
Map<String, InputStream> imageMap, boolean isHtml, File... files) {
Map<String, InputStream> imageMap, boolean isHtml, File... files) {
final Mail mail = Mail.create(mailAccount).setUseGlobalSession(useGlobalSession);
// 可选抄送人