gpt4 book ai didi

java - CXF 中未使用 XmlAdapter

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:32:04 35 4
gpt4 key购买 nike

我有一个外部(即不可修改的)com.external.Money 类,它有一个带有 getter 和 setter 的 java.util.Currency 字段。在我的 CXF jaxws 网络服务中,我有一个如下所示的请求对象:

@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 类,其中货币存储为字符串。理想情况下,我会创建一个 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 ticket看起来它已经被 CXF 团队解决了(哇)!

最佳答案

以下内容在 CXF 环境之外对我有用。您可以将此与您正在做的事情进行比较。如果您所做的匹配,那么您可能遇到了 CXF 问题。

金钱

下面是根据您的问题描述实现的 Money 类。

I have an external (i.e. unmodifiable) com.external.Money class that has a java.util.Currency field with getters and setters.

package com.external;

import java.util.Currency;

public class Money {

private Currency currency;

public Currency getCurrency() {
return currency;
}

public void setCurrency(Currency currency) {
this.currency = currency;
}

}

MoneyAdapter

下面是根据您问题中的描述实现的 MoneyAdapter:

So, I created a MoneyAdapter, which converts the Money into something usable by JAXB, namely a TransportableMoney class with currency stored as a String

package com.internal;

import java.util.Currency;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import com.external.Money;

public class MoneyAdapter extends XmlAdapter<TransportableMoney, Money> {

@Override
public Money unmarshal(TransportableMoney v) throws Exception {
Money money = new Money();
Currency currency = Currency.getInstance(v.currency);
money.setCurrency(currency);
return money;
}

@Override
public TransportableMoney marshal(Money v) throws Exception {
TransportableMoney transportableMoney = new TransportableMoney();
transportableMoney.currency = v.getCurrency().getCurrencyCode();
return transportableMoney;
}

}

TransportableMoney

package com.internal;

public class TransportableMoney {

public String currency;

}

包信息

这是您的 package-info 类,直接取自您的问题。

@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;

示例请求

这是您的 ExampleRequest 类,直接取自您的问题。

package com.internal;

import javax.xml.bind.annotation.XmlRootElement;

import com.external.Money;

@XmlRootElement
public class ExampleRequest {
private Money money;
public Money getMoney() { return money; }
public void setMoney(Money money) { this.money = money; }
}

演示

下面是一些示例代码,将 XML 读入对象并将其写回以证明一切正常。

package com.internal;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(ExampleRequest.class);

File xml = new File("src/com/internal/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
ExampleRequest er = (ExampleRequest) unmarshaller.unmarshal(xml);

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(er, System.out);
}

}

input.xml/输出

这是示例输入和输出 XML。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<exampleRequest>
<money>
<currency>USD</currency>
</money>
</exampleRequest>

关于java - CXF 中未使用 XmlAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12684103/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com