在Web方法中使用JAXB忽略JodaTime方法参数

时间:2014-02-15 21:46:16

标签: java xml soap jaxb jodatime

使用SOAP客户端我可以在另一端获得正确的编组,但不能解组。因此,为此目的,我一直在测试SOAP UI,使用参数“2013-05”调用此方法。

web方法中的断点findByYearMonth显示当前日期值,我还可以看到YearMonth的基本构造函数是由JAXB绑定调用的(这就是我得到2014-02的原因)。

//The class with the web method
@Component
@Qualifier("myFacade")
@XmlJavaTypeAdapter(YearMonthAdapter.class)
@Service
public class myFacade implements IMyFacade {

    /*the web method */ 
    findByYearMonth(YearMonth yearMonth) throws MyException {
        // Break point here shows yearMonth = "2014-02"
    }
}

适配器:

/**
 * The YearMonth adapter.
 *
 * XmlAdapter for YearMonth
 */
public class YearMonthAdapter extends XmlAdapter<String, YearMonth> {

    @Override
    public YearMonth unmarshal(String v) throws Exception {
        return YearMonth.parse(v);
   }

    @Override
    public String marshal(YearMonth v) throws Exception {
        return v.toString();
    }

}

如果YearMonth在DTO中,并且我将其作为参数传递它可以正常工作 - 只要我在正确的路径中有package-info.java文件。我尝试了很多组合和不同的注释,并且在包含web方法的类所在的包中也有以下package-info.java文件,但是我没有运气获取正确的值。 / p>

//package-info.java
@XmlJavaTypeAdapter(type = YearMonth.class, value = YearMonthAdapter.class)
package uk.co.imperatives.billing.core.summary;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.joda.time.YearMonth;

import uk.co.imperatives.billing.api.util.YearMonthAdapter;

1 个答案:

答案 0 :(得分:1)

您必须直接注释参数,如下所示:

//The class with the web method
@Component
@Qualifier("myFacade")
@Service
public class MyFacade implements IMyFacade {

    /* the web method */ 
    findByYearMonth(@XmlJavaTypeAdapter(type = YearMonth.class, value = YearMonthAdapter.class) YearMonth yearMonth) throws MyException {
        // Break point should now show yearMonth = "2013-05"
    }
}