我有一個外部(即不可修改的)com.external.Money
類,它有一個帶有getter和setter的java.util.Currency
字段。在我的CXF JAXWS Web服務,我有類似下面的請求對象:XmlAdapter沒有在CXF中使用
@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
?
EDIT
作爲布萊斯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>
SomeService:
@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
}
}
UPDATE
創建一個JIRA ticket,看起來像它已經被解決CXF團隊(哇)!
''ExampleRequest'在同一個'com.internal'包中嗎?還要確保你用'extends XmlAdapter'聲明你的適配器。還要確保你的'package-info'類正在被編譯? –
嗨布萊斯,謝謝你的迴應。是 - ExampleRequest位於com.internal中,並且適配器確實擴展了XmlAdapter。我非常確定package-info正在被編譯,因爲它對執行有影響(給出不同的錯誤信息),但是我會仔細檢查。 –
andy