这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
mvn "io.quarkus:quarkus-maven-plugin:create" \
-DprojectGroupId="com.bolingcavalry" \
-DprojectArtifactId="hello-quarkus" \
-DprojectVersion="1.0-SNAPSHOT" \
-DclassName="HobbyResource" \
-Dpath="actions"
greeting.message = hello from application.properties
@ConfigProperty(name = "greeting.message")
String message;
@Path("/actions")
public class HobbyResource {
// 配置文件中不存在名为not.exists.config的配置项
@ConfigProperty(name = "not.exists.config")
String notExistsConfig;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello RESTEasy, " + LocalDateTime.now() + ", [" + notExistsConfig + "]";
}
}
@Path("/actions")
public class HobbyResource {
// 配置文件中不存在名为not.exists.config的配置项
@ConfigProperty(name = "not.exists.config", defaultValue = "112233")
String notExistsConfig;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello RESTEasy, " + LocalDateTime.now() + ", [" + notExistsConfig + "]";
}
}
// 配置文件中不存在名为not.exists.config的配置项
@ConfigProperty(name = "not.exists.config", defaultValue = "123")
int notExistsConfig;
@ConfigProperty(name = "server.address", defaultValue = "192.168.1.1")
InetAddress serverAddress;
@Path("/actions")
public class HobbyResource {
// 配置文件中存在名为greeting.message的配置项
@ConfigProperty(name = "greeting.message")
String message;
// 配置文件中,不论是否存在名为optional.message的配置项,应用都不会抛出异常
@ConfigProperty(name = "optional.message")
Optional<String> optionalMessage;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
List<String> list = new ArrayList<>();
list.add(message);
// 只有配置项optional.message存在的时候,才会执行list.add方法
optionalMessage.ifPresent(list::add);
return "Hello RESTEasy, " + LocalDateTime.now() + ", " + list;
}
}
@Path("/actions")
public class HobbyResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
List<String> list = new ArrayList<>();
// 可以用静态方法取得Config实例
Config config = ConfigProvider.getConfig();
// getValue可取得指定配置项的指定类型值
String greet = config.getValue("greeting.message", String.class);
list.add(greet);
// getOptionalValue可以将配置项的值包状为Optional对象,如果配置项不存在,也不会报错
Optional<String> optional = config.getOptionalValue("not.exists.config", String.class);
// 函数式编程:只用optional中有对象时,才会执行list.add方法
optional.ifPresent(list::add);
return "Hello RESTEasy, " + LocalDateTime.now() + ", " + list;
}
}
student.name=Tom
student.age=11
student.description=He is a good boy
package com.bolingcavalry;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithName;
@ConfigMapping(prefix = "student")
public interface StudentConfiguration {
/**
* 名字与配置项一致
* @return
*/
String name();
/**
* 名字与配置项一致,自动转为int型
* @return
*/
int age();
/**
* 名字与配置项不一致时,用WithName注解指定配置项
* @return
*/
@WithName("description")
String desc();
/**
* 用WithDefault注解设置默认值,如果配置项"student.favorite"不存在,则默认值生效
* @return
*/
@WithDefault("default from code")
String favorite();
}
package com.bolingcavalry;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.time.LocalDateTime;
@Path("/actions")
public class HobbyResource {
@Inject
StudentConfiguration student;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello RESTEasy, "
+ LocalDateTime.now()
+ " [" + student.name() + "], "
+ " [" + student.age() + "], "
+ " [" + student.desc() + "], "
+ " [" + student.favorite() + "]";
}
}
@ConfigMapping(prefix = "student", namingStrategy = ConfigMapping.NamingStrategy.SNAKE_CASE)
public interface StudentConfiguration {
/**
* 名字与配置项一致
* @return
*/
String name();
...
student.name=Tom
student.age=11
student.description=He is a good boy
student.address.province=guangdong
student.address.city=shenzhen
package com.bolingcavalry;
public interface Address {
String province();
String city();
}
student.address.province=guangdong
student.address.city=shenzhen
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://xinchen.blog.csdn.net/article/details/123307704
内容来源于网络,如有侵权,请联系作者删除!