使用保留下划线的xjb覆盖JAXB属性名称 [英] Override the JAXB property name using a xjb preserving the underscores

查看:101
本文介绍了使用保留下划线的xjb覆盖JAXB属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义xjb非常适合覆盖名称,但是我们会丢失名称中的下划线。

The custom xjb works great for overriding the names as desired however we lose the underscores in the names.

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
  version="2.1">
  <jxb:globalBindings underscoreBinding="asCharInWord"/>
   <jxb:bindings schemaLocation="foo.xsd">
        <jxb:bindings node="//xs:complexType[@name='fooType']">
            <jxb:property name="value" />
        </jxb:bindings>
  </jxb:bindings>
</jxb:bindings>

正如您在上面看到的xjb所见,生成的java代码是

As you can see for the above xjb the java code generated is

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {
"value"
})
public class FooType {

@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> value;
......
 public List<Object> getValue() {
    if (value == null) {
        value = new ArrayList<Object>();
    }
    return this.value;
}

现在,我将上面的xjb中的一行更改为:

Now, once I change one line in the xjb above to:

 <jxb:property name="_value" />

java代码中的所有更改都是:

All that changes in the java code is :

public List<Object> get_Value() {
    if (value == null) {
        value = new ArrayList<Object>();
    }
    return this.value;
}

观察到:value

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {
"value"
})

所需:_ value

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {
"_value"
})


推荐答案

以下 propOrder 没问题,因为指定了 @XmlAccessorType(XmlAccessType.FIELD),并且该字段的名称是即使该属性名为_Value(请参阅: http://blog.bdoughan.com/2012/02/jaxbs-xmltype-and-proporder.html

The following propOrder is fine since @XmlAccessorType(XmlAccessType.FIELD) is specified and the name of the field is value even though the property is called _Value (see: http://blog.bdoughan.com/2012/02/jaxbs-xmltype-and-proporder.html).

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "textType", propOrder = {
"value"
})








目标是让值显示为_valuein我的json

The goal is to have the "value" appear as "_value" in my json

你的特定 _Value 属性似乎能够持有任何东西。你想如何将内容呈现为JSON?

Your particular _Value property appears to be to be able to hold anything. How do you want the contents to be rendered to JSON?

@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> value;

这篇关于使用保留下划线的xjb覆盖JAXB属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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