Annotation Interface NameProperty


Identifies a setter method or field for setting the name of a POJO as it's known by its parent object.

This annotation is used by parsers to automatically set the name/key of an object when parsing structured data (e.g., JSON maps, XML elements). A common use case is when parsing a map where the map key should be stored as a property on the bean.

Requirements:
  • Must be an instance method or field (not static)
  • For methods: Must accept exactly one parameter (any type)
  • For fields: Can be any type
  • The method or field does not need to be public (will be made accessible automatically)

Can be used in the following locations:

  • Bean setter methods or fields
  • @Rest-annotated classes and @RestOp-annotated methods when an on() value is specified
Example:

// JSON being parsed: // { // "id1": {name: "John Smith", sex: "M"}, // "id2": {name: "Jane Doe", sex: "F"} // } public class Person { @NameProperty public String id; // Gets set to "id1" or "id2" from map key public String name; public char sex; } // Or using a setter method: public class Person { private String id; @NameProperty protected void setName(String name) { this.id = name; } public String name; public char sex; }

When It's Called:
  • During parsing when an object is created as a value in a map/collection
  • The parser automatically calls the setter or sets the field with the key/name from the parent structure
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Optional description for the exposed API.
    Dynamically apply this annotation to the specified methods/fields.
  • Element Details

    • description

      Optional description for the exposed API.
      Returns:
      The annotation value.
      Since:
      9.2.0
      Default:
      {}
    • on

      Dynamically apply this annotation to the specified methods/fields.

      Used in conjunction with BeanContext.Builder.applyAnnotations(Class...) to dynamically apply an annotation to an existing method/field. It is ignored when the annotation is applied directly to methods/fields.

      Valid patterns:
      • Methods:
        • Fully qualified with args:
          • "com.foo.MyClass.myMethod(String,int)"
          • "com.foo.MyClass.myMethod(java.lang.String,int)"
          • "com.foo.MyClass.myMethod()"
        • Fully qualified:
          • "com.foo.MyClass.myMethod"
        • Simple with args:
          • "MyClass.myMethod(String,int)"
          • "MyClass.myMethod(java.lang.String,int)"
          • "MyClass.myMethod()"
        • Simple:
          • "MyClass.myMethod"
        • Simple inner class:
          • "MyClass$Inner1$Inner2.myMethod"
          • "Inner1$Inner2.myMethod"
          • "Inner2.myMethod"
      • Fields:
        • Fully qualified:
          • "com.foo.MyClass.myField"
        • Simple:
          • "MyClass.myField"
        • Simple inner class:
          • "MyClass$Inner1$Inner2.myField"
          • "Inner1$Inner2.myField"
          • "Inner2.myField"
      • A comma-delimited list of anything on this list.
      See Also:
      Returns:
      The annotation value.
      Default:
      {}