简体   繁体   English

如何将XmlAdapter全局应用于JAXB程序?

[英]How do I globally apply an XmlAdapter to a JAXB program?

I am using JAXB (through a mess of JaxWS and CXF) and attempting to marshal the BigDecimal type into a string (number) and int (exponent) because we now have a front end which can't interpret BigDecimal thanks to locale issues. 我正在使用JAXB(通过一堆JaxWS和CXF),并试图将BigDecimal类型编组为字符串(数字)和int(指数),因为现在有了前端,由于语言环境问题,它无法解释BigDecimal。

What I'm trying is to use an XmlAdapter to convert BigDecimal into my own type, BigDecimalUnScaled. 我正在尝试使用XmlAdapter将BigDecimal转换为我自己的类型BigDecimalUnScaled。 This works fine as long as I put my @XmlJavaTypeAdapter annotation directly on the field. 只要我将@XmlJavaTypeAdapter批注直接放在字段上,此方法就可以正常工作。 However, what I really would like to do is put it on my Web Service implementation and have it globally apply to all BigDecimals without having to individual marshal each return object. 但是,我真正想做的是将其放在我的Web Service实现中,并将其全局应用于所有BigDecimal,而不必分别编组每个返回对象。 Is there any way I can do this? 有什么办法可以做到吗?

Eg 例如

Interface DummyWebService
-- Get Return Object (Return object is a POJO with say an ID and a BigDecimal value)

Implementation default
-- No type annotation, uses default BigDecimal marshaller

Marshalled Implementation
-- XmlTypeAdapters implementation, should globally use BigDecimal

I've tried just putting the adapter on the implementation, but it doesn't work. 我已经尝试过仅将适配器放在实现中,但是它不起作用。

Any ideas? 有任何想法吗?

You can register your XmlAdapter at the package level and have it apply to all fields/properties of that type within that package: 您可以在包级别注册XmlAdapter并将其应用于该包内该类型的所有字段/属性:

com/example/package-info.java com / example / package-info.java

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdapter(value=BigDecimalAdapter.class, type=BigDecimal.class)
})
package com.example;

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

For More Information 想要查询更多的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM