小编мυѕ*_*ւмo的帖子

JAXB Marshaller没有值为null的元素

当我使用JAXB Marshaller编写java对象时,marshaller不会为java对象中的空文件创建空元素.例如,我有一个以下java对象:

public class PersonTraining {

    @XmlElement(name = "Val1", required = true)
    protected BigDecimal val1;
    @XmlElement(name = "Val2", required = true, nillable = true)
    protected BigDecimal val2;
    @XmlElement(name = "Val3", required = true, nillable = true)
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar val3;
}
Run Code Online (Sandbox Code Playgroud)

当我获取此对象的实例并编组为XML时,我得到以下内容(这是因为我没有为Val2设置值):

<PersonTraining>
      <Val1>1</Val1>
       <Val3>2010-01-01T00:00:00.0-05:00</Val3>
 </PersonTraining>
Run Code Online (Sandbox Code Playgroud)

但是,我原本预计会有来自编组操作的结果(事实上,我特别需要元素,以便可以针对XSD验证XML)

<PersonTraining>
      <Val1>1</Val1>
      <Val2></Val2>
       <Val3>2010-01-01T00:00:00.0-05:00</Val3>
 </PersonTraining>
Run Code Online (Sandbox Code Playgroud)

请让我知道我需要设置哪个选项,以便对象属性中的空值也可以编组,并作为空/ null元素返回.

这是编组代码:

StringWriter sw = new StringWriter();
JAXBContext jc = JAXBContext.newInstance("person_training");   
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(ptl, sw);
Run Code Online (Sandbox Code Playgroud)

java xml jaxb

9
推荐指数
1
解决办法
3万
查看次数

消费者模拟方法

我想repository.actionOnFile(String path, Consumer<InputStream> action)在此源中进行模拟:

@Autowired
private FileRepositoryService repository;

public Document getDocument(URL url) {
    MutableObject<Document> obj = new MutableObject<>();
    Consumer<InputStream> actionOnFile = inputStream -> obj.setValue(getDocument(inputStream));
    try {
        repository.actionOnFile(url.toExternalForm(), actionOnFile);
    } catch (S3FileRepositoryException e) {
        throw e.getCause();
    }
    return obj.getValue();
}
Run Code Online (Sandbox Code Playgroud)

问题在于第二个参数是lambda表达式。

如何使用模仿来模拟它,我需要accept将输入流传递给方法以对其进行测试?

java lambda mocking

2
推荐指数
1
解决办法
2119
查看次数

标签 统计

java ×2

jaxb ×1

lambda ×1

mocking ×1

xml ×1