From 840a4bad5457556af639c937ab9f5f2569a31383 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Wed, 18 Nov 2020 23:30:34 +0000 Subject: [PATCH] added properties reader, and a few other bits --- build.gradle | 1 + .../gmgauthier/springboot/DemoProperties.java | 52 +++++++++++++++++++ .../springboot/HelloWorldController.java | 14 ++++- .../springboot/SpringBootWebApplication.java | 16 ++++++ src/main/resources/application.properties | 10 ++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/gmgauthier/springboot/DemoProperties.java create mode 100644 src/main/resources/application.properties diff --git a/build.gradle b/build.gradle index 4614e65..29cb6b7 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,7 @@ test { } dependencies { + implementation group: 'org.hibernate', name: 'hibernate-validator', version: '6.1.6.Final' implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.4.0' implementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.4.0' implementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.7.0' diff --git a/src/main/java/com/gmgauthier/springboot/DemoProperties.java b/src/main/java/com/gmgauthier/springboot/DemoProperties.java new file mode 100644 index 0000000..4c3d3cb --- /dev/null +++ b/src/main/java/com/gmgauthier/springboot/DemoProperties.java @@ -0,0 +1,52 @@ +package com.gmgauthier.springboot; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotEmpty; +import java.util.List; + +@Configuration +@ConfigurationProperties("demo") +public class DemoProperties { + + @NotEmpty + private String resourceUrl; + + private List resourcePort; + + @Max(5) + @Min(0) + private Integer resourceCount; + + @Override + public String toString() { + + return "\n" + "resourceUrl: "+ this.resourceUrl+"\n" + + "resourcePort: "+this.resourcePort+"\n" + + "resourceCount: "+this.resourceCount+"\n"; + } + + public String getResourceUrl() { + return resourceUrl; + } + public void setResourceUrl(String resourceUrl) { + this.resourceUrl = resourceUrl; + } + public List getResourcePort() { + return resourcePort; + } + public void setResourcePort(List resourcePort) { + this.resourcePort = resourcePort; + } + + public Integer getResourceCount() { + return resourceCount; + } + + public void setResourceCount(Integer resourceCount) { + this.resourceCount = resourceCount; + } +} \ No newline at end of file diff --git a/src/main/java/com/gmgauthier/springboot/HelloWorldController.java b/src/main/java/com/gmgauthier/springboot/HelloWorldController.java index e4c55b1..bbae104 100644 --- a/src/main/java/com/gmgauthier/springboot/HelloWorldController.java +++ b/src/main/java/com/gmgauthier/springboot/HelloWorldController.java @@ -5,6 +5,9 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; +import java.text.SimpleDateFormat; +import java.util.Date; + @RestController @EnableAutoConfiguration public class HelloWorldController { @@ -12,6 +15,15 @@ public class HelloWorldController { @RequestMapping("/") @ResponseBody String hello() { - return "Hello World! Spring boot is so simple."; + return "

Hello World!

\n

Spring boot is so simple.

"; + } + + @RequestMapping("/date") + @ResponseBody + String time() { + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z"); + Date date = new Date(System.currentTimeMillis()); + String strDate = formatter.format(date); + return "

The Current Date is:" + strDate + "

"; } } \ No newline at end of file diff --git a/src/main/java/com/gmgauthier/springboot/SpringBootWebApplication.java b/src/main/java/com/gmgauthier/springboot/SpringBootWebApplication.java index 36b8587..61eeade 100644 --- a/src/main/java/com/gmgauthier/springboot/SpringBootWebApplication.java +++ b/src/main/java/com/gmgauthier/springboot/SpringBootWebApplication.java @@ -1,12 +1,28 @@ package com.gmgauthier.springboot; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import javax.annotation.PostConstruct; + @SpringBootApplication public class SpringBootWebApplication { + private static Logger logger = LoggerFactory.getLogger(SpringBootWebApplication.class); + + @Autowired + private DemoProperties demoProperties; + public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } + + @PostConstruct + public void init() { + + logger.info(demoProperties.toString()); + } } \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..fa39a50 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,10 @@ +spring.main.banner-mode=off + +server.port=800 +logging.level.org.springframework.web=DEBUG +logging.level.org.hibernate=ERROR + +demo.resource-url=http://duckduckgo.com +demo.resourcePort[0]=80 +demo.resourcePort[1]=443 +demo.resourceCount=10 \ No newline at end of file