added properties reader, and a few other bits

This commit is contained in:
Greg Gauthier 2020-11-18 23:30:34 +00:00
parent b59f534e85
commit 840a4bad54
5 changed files with 92 additions and 1 deletions

View File

@ -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'

View File

@ -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<Integer> 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<Integer> getResourcePort() {
return resourcePort;
}
public void setResourcePort(List<Integer> resourcePort) {
this.resourcePort = resourcePort;
}
public Integer getResourceCount() {
return resourceCount;
}
public void setResourceCount(Integer resourceCount) {
this.resourceCount = resourceCount;
}
}

View File

@ -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 "<h1>Hello World!</h1> \n <h2>Spring boot is so simple.</h2>";
}
@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 "<p><b>The Current Date is:" + strDate + "</b></p>";
}
}

View File

@ -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());
}
}

View File

@ -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