简体   繁体   English

Springboot 获取如果字段在运行时可以为空

[英]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.然后通过 API 调用,我得到了这些字段,我想检查一个字段是否可以为空,以将其标记为必需。

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.如果我理解正确,您可以使用 Java Reflections 来检查注释是否设置为可为空。

For example:例如:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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