cxf wsdl2java生成'protected'属性,如何生成私有属性?

时间:2013-11-21 10:03:14

标签: java cxf wsdl2java

我正在使用带有wsdl2java目标的cxf-codegen-plugin(maven)从我的wsdl生成java代码。

问题是,当我需要私有属性时,它会生成受保护的属性:

我的wsdl元素:

<element name="productCode" type="string"/>

预期:

@XmlElement(required = true)
private String productCode;

结果:

@XmlElement(required = true)
protected String productCode;

我的wsdl或插件配置中有什么要添加来生成私有字段吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

我认为这是因为jaxb比cxf。你需要开发自己的xjc插件才能做到。

https://jaxb.java.net/nonav/2.0.2/docs/developPlugins.html

还谷歌开发和插入自己的xjc 创建插件是正确的方法。使用此论坛中的代码

public class PrivateMemberPlugin
    extends Plugin
{

    @Override
    public String getOptionName()
    {
        return "Xpm";
    }

    @Override
    public String getUsage()
    {
        return "  -Xpm    : Change members visibility to private";
    }

    @Override
    public boolean run(Outline model, Options opt, ErrorHandler errorHandler)
        throws SAXException
    {
        for (ClassOutline co : model.getClasses())
        {

            JDefinedClass jdc = co.implClass;
            // avoid concurrent modification by copying the fields in a new list
            List<JFieldVar> fields = new ArrayList<JFieldVar>(jdc.fields().values());
            for (JFieldVar field : fields)
            {
                // never do something with serialVersionUID if it exists.
                if (!field.name().equalsIgnoreCase("serialVersionuid"))
                {
                    // only try to change members that are not private
                    if (field.mods().getValue() != JMod.PRIVATE)
                    {
                        // since there is no way to change the visibilty, remove the field an recreate it
                        jdc.removeField(field);
                        jdc.field(JMod.PRIVATE, field.type(), field.name());

                    }
                }
            }

        }
        return true;
    }

}