无论如何将构造函数参数传递给JAXB适配器? [英] Anyway to pass a constructor parameter to a JAXB Adapter?

查看:199
本文介绍了无论如何将构造函数参数传递给JAXB适配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我编写的WebService中使用Spring 3 IOC和JAXB / JAX-WS。我现在有一个小问题,数据必须在返回消费者之前进行舍入,因为他们无法处理值的完整精度。



To最小化对WS设计和计算的影响,我选择使用Jaxb XmlAdapter在我的响应编组时对值进行舍入。一切正常。



我现在的问题是我想让它变得灵活。即:在某些情况下,我需要舍入到2个小数位,大约4个等等。现在,我必须创建一个TwoDecimalAdapter和一个FourDecimalAdapter,并在我的模型定义中使用必要的相应位置。这意味着代码重复。



无论如何都要创建一个通用的Rounding Adapter,并将参数传递给它?举例来说,而不是:

  @ javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(值= FourDecimalRoundingAdapter.class,类型= java.math.BigDecimal.class)

我希望能够做到这样的事情:

  @ javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value = new RoundingAdapter(4),type = java.math .BigDecimal.class)

显然这不起作用,因为JAXB实例化了适配器本身,但是在那里我可以使用任何技术将参数传递给适配器?我希望能够在Spring中声明舍入适配器并以这种方式使用它,但又一次,我无法设计可重用的解决方案。



谢谢,



Eric

解决方案

我不知道你怎么挂这个在Spring中,但下面是您可以利用的JAXB机制的描述。



如果您有以下内容:



'pre> @ javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(值= RoundingAdapter.class,类型= java.math.BigDecimal.class)

然后使用独立的JAXB API,您可以执行以下操作。以下代码表示只要遇到 RoundingAdapter ,就应该使用指定的实例。

  marshaller.setAdapter(new RoundingAdapter(4)); 
unmarshaller.setAdapter(new RoundingAdapter(4));

更多信息




I'm using Spring 3 IOC and JAXB/JAX-WS in a WebService that I wrote. I am having a slight issue right now with data that must be rounded prior to returning to the consumer as they are not capable of handling the complete precision of the values.

To minimize the impact on the WS design and calculations, I chose to use an Jaxb XmlAdapter to round the values upon marshalling of my response. Everything works fine.

My issue now is that I would like to make it flexible. Ie: in some cases, I need to round to 2 decimal places, in some 4, etc.. Right now, I have to create a TwoDecimalAdapter and a FourDecimalAdapter and use the appropriate one where necessary in my model definitions. This means code duplication.

Is there anyway to create a generic Rounding Adapter, and pass a parameter to it? For instance, instead of:

@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=FourDecimalRoundingAdapter.class,type=java.math.BigDecimal.class)

I'd like to be able to do something like:

@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=new RoundingAdapter(4),type=java.math.BigDecimal.class)

Obviously that doesn't work as JAXB instantiates the adapter itself, but is there any technique I can use to pass parameters to the adapter? I'd love to be able to declare the rounding adapter in Spring and use it that way, but there again, I am unable to devise a reusable solution.

Thanks,

Eric

解决方案

I'm not sure how you hook this in with Spring, but below is a description of the JAXB mechanism that you can leverage.

If you have the following:

@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=RoundingAdapter.class,type=java.math.BigDecimal.class)

Then using the standalone JAXB APIs you could do the following. The code below means whenever the RoundingAdapter is encountered, the specified instance should be used.

marshaller.setAdapter(new RoundingAdapter(4));
unmarshaller.setAdapter(new RoundingAdapter(4));

For More Information

这篇关于无论如何将构造函数参数传递给JAXB适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆