我有一个外部(即不可修改)com.external.Money
类,它有一个java.util.Currency
带有 getter 和 setter 的字段。在我的 CXF jaxws Web 服务中,我有一个如下所示的请求对象:
@XmlRootElement
public ExampleRequest {
private Money money;
public Money getMoney() { return money; }
public void setMoney(Money money) { this.money = money; }
}
当我尝试启动服务时,出现以下错误:
Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.util.Currency does not have a no-arg default constructor.
this problem is related to the following location:
at java.util.Currency
at public java.util.Currency com.external.Money.getCurrency()
at com.external.Money
at public com.external.Money com.internal.ExampleRequest.getMoney()
at com.internal.ExampleRequest
因此,我创建了一个MoneyAdapter
,它将 转换Money
为 JAXB 可用的东西,即一个TransportableMoney
将货币存储为String
. 理想情况下,我会创建一个CurrencyAdapter
,但由于货币字段是由外部类封装的,我无法将其连接起来(或者我不知道如何连接)。
我正在尝试使用以下方式连接适配器package-info.java
:
@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(value=MoneyAdapter.class, type=Money.class)
})
package com.internal;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
import com.external.Money;
问题是,这行不通。而不是上面的错误,我现在得到:
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
java.util.Currency does not have a no-arg default constructor.
this problem is related to the following location:
at java.util.Currency
at public java.util.Currency com.external.Money.getCurrency()
at com.external.Money
我认为这是因为com.external.Money
有一个无参数构造函数。当它没有无参数构造函数时,此设置似乎有效。
我在这里错过了什么吗?有谁知道如何强制 CXF 使用XmlAdapter
?
编辑
正如 Blaise Doughan 指出的那样,上面的配置只使用 JAXB 编组器就可以工作。它只是不适用于 CXF 2.6.0。这是我的主要方法:
SomeService ss = new SomeService();
JaxWsServerFactoryBean jwsfb = new JaxWsServerFactoryBean();
jwsfb.setServiceClass(SomeService.class);
jwsfb.setServiceBean(ss);
jwsfb.setAddress("http://localhost:9020/hello");
jwsfb.create();
maven依赖:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>2.6.0</version>
<scope>runtime</scope>
</dependency>
一些服务:
@WebService
@SOAPBinding(use = SOAPBinding.Use.LITERAL, style = SOAPBinding.Style.DOCUMENT)
public class SomeService {
public ExampleRequest getRequest() {
ExampleRequest request = new ExampleRequest();
request.setMoney(new Money(Currency.getInstance("USD"), BigDecimal.ONE));
return request;
}
public void setRequest(ExampleRequest req) {
// do nothing
}
}
更新
创建了一张JIRA 票,看起来它已经被 CXF 团队解决了(哇)!