我想使用带有JSON Schema验证器选项的Spring-boot定义Mongo中的Collection(https://docs.mongodb.com/manual/core/schema-validation/#json-schema),我不想要JSR -303 Bean验证(这不是一个有效的答案Spring数据mongoDb不是像Spring数据Jpa那样的空注释),但是在创建Collection时,定义了一个使用CollectionInfos()在JSON中显示的选项.
例如,如果我定义一个Account模型类,则喜欢:
public class Account {
@Id
private String id;
private String name;
private String surname;
@NotNull
private String username;
}
Run Code Online (Sandbox Code Playgroud)
我希望该集合使用db.getCollectionInfos(),json喜欢:
[
{
"name" : "account",
"type" : "collection",
"options" : {
"validator" : {
"$jsonSchema" : {
"bsonType" : "object",
"required" : [
"username"
]
}
}
},
"info" : {
"readOnly" : false,
"uuid" : UUID("979cdc4b-d6f3-4aef-bc89-3eee812773a5")
},
"idIndex" : { …
Run Code Online (Sandbox Code Playgroud) 我开发了一个简单的注释界面
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
String foo() default "foo";
}
Run Code Online (Sandbox Code Playgroud)
然后我测试它注释一个类
@CustomAnnotation
public class AnnotatedClass {
}
Run Code Online (Sandbox Code Playgroud)
并使用方法调用它
public void foo() {
CustomAnnotation customAnnotation = AnnotatedClass.class.getAnnotation(CustomAnnotation.class);
logger.info(customAnnotation.foo());
}
Run Code Online (Sandbox Code Playgroud)
并且一切正常,因为它记录了foo.我也尝试将注释类更改为@CustomAnnotation(foo = "123")
并且一切正常,因为它记录了123.
现在我希望传递给注释的值由the检索application.properties
,因此我将注释类更改为
@CustomAnnotation(foo = "${my.value}")
public class AnnotatedClass {
}
Run Code Online (Sandbox Code Playgroud)
但现在日志返回String ${my.vlaue}
而不是值application.properties
.
我知道可以${}
在注释中使用指令,因为我总是使用@RestController
这样的@GetMapping(path = "${path.value:/}")
,一切正常.
我在Github存储库上的解决方案:https://github.com/federicogatti/annotatedexample
可以在注解声明中或更普遍地在Interfance中使用application.properties值吗?例如,我有:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EndLogging {
String BusinessOperationName() default "NOME_BUSINESSOPERATIONNAME_UNDEFINED";
String ProcessName() default "NOME_MICROSERVZIO_UNDEFINED";
}
Run Code Online (Sandbox Code Playgroud)
但我希望该方法返回的默认值是application.properties值,类似于:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EndLogging {
String BusinessOperationName() default @Value("${business.value}");
String ProcessName() default @Value("${process.value}");
}
Run Code Online (Sandbox Code Playgroud)