一个javabean可以有方法和非基元类型作为它的属性吗?

时间:2021-07-24 13:27:45

Sorry this is probably super basic. But in all my javabean examples, I've not seen anything beyond an "int" and "String" as javabean properties.

抱歉,这可能是非常基本的。但是在我所有的javabean示例中,除了“int”和“String”之外,我没有看到任何东西是javabean属性。

What if I had a Person bean, and I wanted a "nicknames" property? That would be an array of "String" objects. Is that possible? Could I use a HashMap as a javabean property as well?

如果我有一个Person bean,我想要一个“昵称”属性呢?这将是一个“字符串”对象数组。这有可能吗?我可以使用HashMap作为javabean属性吗?

I'm also not clear if javabeans are allowed to have methods beyond getters and setters. If I had a Calculator bean, could I have a method like add() or subtract()?

我也不清楚javabeans是否被允许拥有除getter和setter之外的方法。如果我有一个计算器bean,我能有一个方法像add()或subtract()吗?

(The reason I'm asking is related to marshalling and unmarshalling JSON to java objects using the JAX-RS specification. I think that in standard tutorials the beans are all very simple and can use the automatic marshalling and unmarshalling without writing any adapters. I'm trying to figure out at what point the automatic conversion stops working.)

(我问这个问题的原因与使用JAX-RS规范将JSON编组和反编组到java对象有关。我认为在标准教程中,bean都非常简单,可以使用自动编组和反编组,而不需要编写任何适配器。我想知道自动转换在什么时候停止工作)

Thanks!

谢谢!

1 个解决方案

#1


1  

  1. Properties of non-primitive or simple (String) types - no problem as long as JAXB provider would be able to serialize/deserialize them.

    非基元或简单(字符串)类型的属性——只要JAXB提供程序能够序列化/反序列化它们,就没有问题。

  2. You can add methods to your beans, no problem. If you want to be sure that fields are used for serialization you may annotate your bean with @XmlAccessorType(XmlAccessType.FIELD)

    您可以向bean添加方法,没问题。如果您希望确保将字段用于序列化,可以使用@XmlAccessorType(XmlAccessType.FIELD)注释您的bean

#1


1  

  1. Properties of non-primitive or simple (String) types - no problem as long as JAXB provider would be able to serialize/deserialize them.

    非基元或简单(字符串)类型的属性——只要JAXB提供程序能够序列化/反序列化它们,就没有问题。

  2. You can add methods to your beans, no problem. If you want to be sure that fields are used for serialization you may annotate your bean with @XmlAccessorType(XmlAccessType.FIELD)

    您可以向bean添加方法,没问题。如果您希望确保将字段用于序列化,可以使用@XmlAccessorType(XmlAccessType.FIELD)注释您的bean