0

Trying to get the POJO property name in Micronaut application. I have the below POJO class

@Introspected
public class Product {
    @BsonProperty("_id")
    @BsonId
    private ObjectId id;
    private String name;
    private float price;
    private String description;

    // Getter and setter
}

I know we can get the property using Introspected

The below code gives me all the property in an array form.

 final BeanIntrospection<Product> introspection = BeanIntrospection.getIntrospection(Product.class);
 var product = introspection.getPropertyNames();

Now the product holds string of property names,

[0] name
[1] price
[2] description

I need to get the individual property as below instead of foreach on the product

Instead of getting through the array, is there any way to access it directly similar to Lombok as below

var desc = Product.Fields.description
var name = Product.Fields.name
var price = Product.Fields.price

Is there any way to achieve this?

San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • https://docs.micronaut.io/latest/guide/index.html#introspection with explanation how yo use BeanIntrospection API – Marcin Rzepecki Nov 02 '20 at 16:54
  • From the document I didn't get any reference for the individual property. I can see the method for to retrieve all the properties ' introspection.getPropertyNames()' – San Jaisy Nov 02 '20 at 17:16
  • The title is currently `"Getting the POJO property name in micronaut"` - Are you really trying to get the property names, or do you want the property values? – Jeff Scott Brown Nov 02 '20 at 17:20
  • I am trying to get the property names, similar to this Product.Fields.name – San Jaisy Nov 02 '20 at 17:22
  • 1
    It isn't really clear what you need to do. Do you just need a list of all fields that are declared in the class? All the properties (not the same as a field) declared in the class? Something else? – Jeff Scott Brown Nov 02 '20 at 19:54
  • I have updated the question please have a look – San Jaisy Nov 03 '20 at 02:23

1 Answers1

1

Instead of getting through the array, is there any way to access it directly similar to Lombok as below:

var desc = Product.Fields.description 
var name = Product.Fields.name
var price = Product.Fields.price

No. There is no such API to support a syntax like that.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • FYI... In a comment that was attached to an answer I deleted, the OP said "I am trying to get the name of the property not the value". – Jeff Scott Brown Nov 03 '20 at 20:09
  • Thank you for that, do you have any example for this question https://stackoverflow.com/questions/64247414/reactive-and-non-blocking-method-micronaut-with-apache-kafka, I am still not able to solve what I am missing – San Jaisy Nov 04 '20 at 02:39