java中类的创建与赋值带注释,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.

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

解决方案

Advantages of Starting from POJOs

Easier to create a model that can be used with another mapping

technology (such as JPA or JSON-binding).

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).

Can add your own business logic on the domain model.

Easier to create a model that corresponds to your exact naming conventions.

Easy to leverage JAXB providers extensions (such as MOXy's @XmlPath mapping).

Advantages of Generating Model from XML Schema

Very easy to map a large XML schema to Java classes.

You can greater confidence that your Java model corresponds exactly to your XML schema.

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.

Example

UPDATE

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 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值