JAXB手动类注释与生成 [英] JAXB manual class annotation vs generation

查看:86
本文介绍了JAXB手动类注释与生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

手动编写类和从方案生成JAXB类的优点/缺点是什么.我正在开始新项目,并想了解选择哪种方法来迷上陷阱.

What is props/cons of manual class writing and generation of JAXB classes from scheme. I am starting new project and would like to understand which way to choose to avid pitfalls.

我具有复杂的XML结构(基本上是 FreeSwitch配置定义),其中包含可重复的类型在不同的XML级别上.

I have complex structure of XML (basicly it is FreeSwitch configuration definition) with included repeatable types on different XML levels.

推荐答案

从POJO开始的优点

  1. 更易于创建可与其他映射一起使用的模型 技术(例如JPA或JSON绑定).
  2. 可以在集合属性上利用@XmlElementWrapper而不是为该层生成单独的类(请参阅: http://blog.bdoughan.com/2011/08/xml-schema-to-java-generating.html ).
  3. 更容易创建与您的确切命名约定相对应的模型.
  4. 易于利用JAXB提供程序扩展(例如MOXy的@XmlPath映射).
  1. Easier to create a model that can be used with another mapping technology (such as JPA or JSON-binding).
  2. Can leverage @XmlElementWrapper on collection properties instead of a separate class being generated for that layer (see: How generate XMLElementWrapper annotation with xjc and customized binding).
  3. Can add your own business logic on the domain model.
  4. Specifying an XmlAdapter is cleaner (compare with http://blog.bdoughan.com/2011/08/xml-schema-to-java-generating.html).
  5. Easier to create a model that corresponds to your exact naming conventions.
  6. Easy to leverage JAXB providers extensions (such as MOXy's @XmlPath mapping).

从XML模式生成模型的优点

  1. 非常容易将大型XML模式映射到Java类.
  2. 您可以放心,您的Java模型与XML模式完全对应.

混合两者

对于JAXB,它不必是一个或另一个类型的选择.您可以使用外部绑定文件,以便在clas生成期间,JAXB将使用为某些复杂类型编写的类,然后使其余生成的模型指向这些类.

Mixing the Two

With JAXB it does not need to be a one or the other type choice. You can use an external bindings file so that during clas generation JAXB will use classes that you wrote for certain complex types and then have the rest of the generated model point to these.

示例

更新

在JAXB中使用构造函数呢?据我所知,这是不允许的 没有特殊构造函数的类.就是说我不能做点什么 必填字段.有适当的方法来处理必填字段吗?

What about using constructors in JAXB ? As I know it is not allowed classes without puclic constructors. It is mean that I can't do some fields required. Is there proper way to deal with required fields ?

JAXB需要一个无参数的构造函数,该构造函数可以是私有的.如果您的类没有no-arg构造函数,则可以使用XmlAdapter.

JAXB requires a no-arg constructor which can be private. If your class doesn't have a no-arg constructor then you can use an XmlAdapter.

这篇关于JAXB手动类注释与生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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