简体   繁体   中英

Springboot Get if field is nullable on runtime

I'm making an application that presents different filters depending on the data you want to query. I'm trying to set if a filter is required or not based on its entities "nullable" property.

@Entity
@Table(name = "PLAYER")
public class Player {
    ...
    @Column(name = "NAME", nullable = false)
    private String name;
    ...
}

Then with an API call I get the fields and I want to check if a field is nullable or no, to mark it as required.

I have looked the documentation, but it just says what are the fields, it's options, how to use them, but nothing about checking the properties on runtime.

Can it be done?

If I understood it correctly, you can make use of Java Reflections to check if annotation is set to nullable or not.

For example:

Field declaredField = Player.class.getDeclaredField("name");
        
Column annotation = declaredField.getAnnotation(Column.class);

System.out.println(annotation.nullable());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM