diff --git a/pom.xml b/pom.xml
index e0dd7b0..03bfabc 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,65 +1,39 @@
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
-
guru.springframework
- blogposts
+ spring-boot-web
0.0.1-SNAPSHOT
jar
-
- Blog Posts
- Misc Blog Posts
-
+ Spring Boot Web Application
+ Spring Boot Web Application
org.springframework.boot
spring-boot-starter-parent
- 1.2.3.RELEASE
+ 1.4.2.RELEASE
-
UTF-8
- guru.springframework.blog.BlogPostsApplication
1.8
-
+
org.springframework.boot
- spring-boot-starter
-
-
- org.springframework.boot
- spring-boot-starter-logging
-
-
+ spring-boot-starter-web
-
- org.springframework.boot
- spring-boot-starter-test
+ com.jayway.jsonpath
+ json-path
test
+
- org.apache.logging.log4j
- log4j-api
- 2.5
-
-
- org.apache.logging.log4j
- log4j-core
- 2.5
-
-
- com.h2database
- h2
- runtime
-
-
- org.hibernate
- hibernate-core
- 4.3.10.Final
+ org.springframework.boot
+ spring-boot-starter-test
+ test
@@ -70,5 +44,4 @@
-
-
+
\ No newline at end of file
diff --git a/src/main/java/guru/springframework/blog/contextrefresh/ContextRefresehedApplication.java b/src/main/java/guru/springframework/blog/contextrefresh/ContextRefresehedApplication.java
deleted file mode 100644
index d0c30a2..0000000
--- a/src/main/java/guru/springframework/blog/contextrefresh/ContextRefresehedApplication.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package guru.springframework.blog.contextrefresh;
-
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.ConfigurableApplicationContext;
-
-@SpringBootApplication
-public class ContextRefresehedApplication {
- public static void main(String[] args) {
- ConfigurableApplicationContext ctx = SpringApplication.run(ContextRefresehedApplication.class, args);
- EventHolderBean bean = ctx.getBean(EventHolderBean.class);
- System.out.println("Event Processed?? - " + bean.getEventFired());
- }
-}
diff --git a/src/main/java/guru/springframework/blog/contextrefresh/ContextRefreshedListener.java b/src/main/java/guru/springframework/blog/contextrefresh/ContextRefreshedListener.java
deleted file mode 100644
index 448e0b5..0000000
--- a/src/main/java/guru/springframework/blog/contextrefresh/ContextRefreshedListener.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package guru.springframework.blog.contextrefresh;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.ApplicationListener;
-import org.springframework.context.event.ContextRefreshedEvent;
-import org.springframework.stereotype.Component;
-
-@Component
-public class ContextRefreshedListener implements ApplicationListener{
-
- private EventHolderBean eventHolderBean;
-
- @Autowired
- public void setEventHolderBean(EventHolderBean eventHolderBean) {
- this.eventHolderBean = eventHolderBean;
- }
-
- @Override
- public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
- System.out.println("Context Event Received");
- eventHolderBean.setEventFired(true);
- }
-}
\ No newline at end of file
diff --git a/src/main/java/guru/springframework/blog/contextrefresh/EventHolderBean.java b/src/main/java/guru/springframework/blog/contextrefresh/EventHolderBean.java
deleted file mode 100644
index dcea4cd..0000000
--- a/src/main/java/guru/springframework/blog/contextrefresh/EventHolderBean.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package guru.springframework.blog.contextrefresh;
-
-import org.springframework.stereotype.Component;
-
-@Component
-public class EventHolderBean {
- private Boolean eventFired = false;
-
- public Boolean getEventFired() {
- return eventFired;
- }
-
- public void setEventFired(Boolean eventFired) {
- this.eventFired = eventFired;
- }
-}
diff --git a/src/main/java/guru/springframework/blog/controllers/IndexController.java b/src/main/java/guru/springframework/blog/controllers/IndexController.java
new file mode 100644
index 0000000..221d6c3
--- /dev/null
+++ b/src/main/java/guru/springframework/blog/controllers/IndexController.java
@@ -0,0 +1,135 @@
+package guru.springframework.blog.controllers;
+
+import org.springframework.web.bind.annotation.*;
+import org.springframework.http.*;
+
+@RestController
+@RequestMapping("/home")
+public class IndexController {
+
+ /*@RequestMapping at method level*/
+ @RequestMapping("/index")
+ String index(){
+ return "Hello from index";
+ }
+ @RequestMapping(value={"", "/page", "page*","view/*"})
+ String indexMultipleMapping(){
+ return "Hello from index multiple mapping.";
+ }
+
+ /*@RequestMapping using HTTP request methods*/
+ @RequestMapping(method = RequestMethod.GET)
+ String get(){
+ return "Hello from get";
+ }
+ @RequestMapping(method = RequestMethod.DELETE)
+ String delete(){
+ return "Hello from delete";
+ }
+ @RequestMapping(method = RequestMethod.POST)
+ String post(){
+ return "Hello from post";
+ }
+ @RequestMapping(method = RequestMethod.PUT)
+ String put(){
+ return "Hello from put";
+ }
+ @RequestMapping(method = RequestMethod.PATCH)
+ String patch(){
+ return "Hello from patch";
+ }
+ /*@RequestMapping with headers*/
+ @RequestMapping(value = "/head", headers = {"content-type=text/plain", "content-type=text/html"})
+ String getWithHeaders(){
+ return "Mapping applied along with headers";
+ }
+
+ /*@RequestMapping with @RequestParam*/
+ @RequestMapping(value = "/personId")
+ String getId(@RequestParam String personId){
+ System.out.println("ID is "+personId);
+ return "Get ID from query string of URL without value element";
+ }
+ @RequestMapping(value = "/id")
+ String getIdByValue(@RequestParam("id") String personId){
+ System.out.println("ID is "+personId);
+ return "Get ID from query string of URL with value element";
+ }
+
+ @RequestMapping(value = "/name")
+ String getName(@RequestParam(value = "person", required = false) String personName ){
+ return "Required element of request param";
+ }
+
+ /*@RequestMapping with produces and consumes attributes*/
+ @RequestMapping(value = "/prod", produces = {"text/html", "application/JSON"})
+ @ResponseBody
+ String getProduces(){
+ return "Produces attribute";
+ }
+
+ @RequestMapping(value = "/cons", consumes = {"text/plain", "application/XML"})
+ String getConsumes(){
+ return "Consumes attribute";
+ }
+
+ /*@RequestMapping with @PathVariable for Dynamic URI */
+ @RequestMapping(value = "/fetch/{id}", method = RequestMethod.GET)
+ String getDynamicUriValue(@PathVariable String id){
+ System.out.println("ID is "+id);
+ return "Dynamic URI parameter fetched";
+ }
+ @RequestMapping(value = "/fetch/{id:[a-z]+}/{name}", method = RequestMethod.GET)
+ String getDynamicUriValueRegex(@PathVariable("name") String name){
+ System.out.println("Name is "+name);
+ return "Dynamic URI parameter fetched using regex";
+ }
+
+ /*@RequestMapping with params attribute*/
+ @RequestMapping(value = "/fetch", params = {"personId=10"})
+ String getParams(@RequestParam("personId") String id){
+ return "Fetched parameter using params attribute = "+id;
+ }
+
+ @RequestMapping(value = "/fetch", params = {"personId=20"})
+ String getParamsDifferent(@RequestParam("personId") String id){
+ return "Fetched parameter using params attribute = "+id;
+ }
+
+ /*@RequestMapping for default method*/
+ @RequestMapping()
+ String defaultMethod(){
+ return "This is a default method for the class";
+ }
+
+ /*@RequestMapping shortcuts*/
+ @GetMapping("/person")
+ public @ResponseBody ResponseEntity getPerson() {
+ return new ResponseEntity("Response from GET", HttpStatus.OK);
+ }
+
+ @GetMapping("/person/{id}")
+ public @ResponseBody ResponseEntity getPersonById(@PathVariable String id){
+ return new ResponseEntity("Response from GET with id " + id, HttpStatus.OK);
+ }
+
+ @PostMapping("/person/post")
+ public @ResponseBody ResponseEntity postPerson() {
+ return new ResponseEntity("Response from POST method", HttpStatus.OK);
+ }
+
+ @PutMapping("/person/put")
+ public @ResponseBody ResponseEntity putPerson() {
+ return new ResponseEntity("Response from PUT method", HttpStatus.OK);
+ }
+
+ @DeleteMapping("/person/delete")
+ public @ResponseBody ResponseEntity deletePerson() {
+ return new ResponseEntity("Response from DELETE method", HttpStatus.OK);
+ }
+
+ @PatchMapping("/person/patch")
+ public @ResponseBody ResponseEntity patchPerson() {
+ return new ResponseEntity("Response from PATCH method", HttpStatus.OK);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/guru/springframework/blog/currenttime/CurrentTimeDateCalendar.java b/src/main/java/guru/springframework/blog/currenttime/CurrentTimeDateCalendar.java
deleted file mode 100644
index d5daa5d..0000000
--- a/src/main/java/guru/springframework/blog/currenttime/CurrentTimeDateCalendar.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package guru.springframework.blog.currenttime;
-
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Date;
-
-public class CurrentTimeDateCalendar {
- public static void getCurrentTimeUsingDate() {
- Date date = new Date();
- String strDateFormat = "hh:mm:ss a";
- DateFormat dateFormat = new SimpleDateFormat(strDateFormat);
- String formattedDate= dateFormat.format(date);
- System.out.println("Current time of the day using Date - 12 hour format: " + formattedDate);
- }
- public static void getCurrentTimeUsingCalendar() {
- Calendar cal = Calendar.getInstance();
- Date date=cal.getTime();
- DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
- String formattedDate=dateFormat.format(date);
- System.out.println("Current time of the day using Calendar - 24 hour format: "+ formattedDate);
- }
-}
\ No newline at end of file
diff --git a/src/main/java/guru/springframework/blog/currenttime/CurrentTimeJava8.java b/src/main/java/guru/springframework/blog/currenttime/CurrentTimeJava8.java
deleted file mode 100644
index aed4a6b..0000000
--- a/src/main/java/guru/springframework/blog/currenttime/CurrentTimeJava8.java
+++ /dev/null
@@ -1,31 +0,0 @@
-package guru.springframework.blog.currenttime;
-
-import java.time.*;
-import java.time.format.DateTimeFormatter;
-public class CurrentTimeJava8 {
- public static void getCurrentTime(){
- System.out.println("-----Current time of your time zone-----");
- LocalTime time = LocalTime.now();
- System.out.println("Current time of the day: " + time);
- }
- public static void getCurrentTimeWithTimeZone(){
- System.out.println("-----Current time of a different time zone using LocalTime-----");
- ZoneId zoneId = ZoneId.of("America/Los_Angeles");
- LocalTime localTime=LocalTime.now(zoneId);
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
- String formattedTime=localTime.format(formatter);
- System.out.println("Current time of the day in Los Angeles: " + formattedTime);
-
- }
- public static void getCurrentTimeWithOffset(){
- System.out.println("-----Current time of different offset-----");
- ZoneOffset zoneOffset = ZoneOffset.of("-08:00");
- ZoneId zoneId=ZoneId.ofOffset("UTC", zoneOffset);
- LocalTime offsetTime = LocalTime.now(zoneId);
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");
- String formattedTime=offsetTime.format(formatter);
- System.out.println("Current time of the day with offset -08:00: " + formattedTime);
-
- }
-}
-
diff --git a/src/main/java/guru/springframework/blog/dependencyinversionprinciple/highlevel/ElectricPowerSwitch.java b/src/main/java/guru/springframework/blog/dependencyinversionprinciple/highlevel/ElectricPowerSwitch.java
deleted file mode 100644
index 6779ef5..0000000
--- a/src/main/java/guru/springframework/blog/dependencyinversionprinciple/highlevel/ElectricPowerSwitch.java
+++ /dev/null
@@ -1,25 +0,0 @@
-package guru.springframework.blog.dependencyinversionprinciple.highlevel;
-
-
-public class ElectricPowerSwitch implements Switch {
- public Switchable client;
- public boolean on;
- public ElectricPowerSwitch(Switchable client) {
- this.client = client;
- this.on = false;
- }
- public boolean isOn() {
- return this.on;
- }
- public void press(){
- boolean checkOn = isOn();
- if (checkOn) {
- client.turnOff();
- this.on = false;
- } else {
- client.turnOn();
- this.on = true;
- }
-
- }
-}
diff --git a/src/main/java/guru/springframework/blog/dependencyinversionprinciple/highlevel/Switch.java b/src/main/java/guru/springframework/blog/dependencyinversionprinciple/highlevel/Switch.java
deleted file mode 100644
index 46e7bc6..0000000
--- a/src/main/java/guru/springframework/blog/dependencyinversionprinciple/highlevel/Switch.java
+++ /dev/null
@@ -1,6 +0,0 @@
-package guru.springframework.blog.dependencyinversionprinciple.highlevel;
-
-public interface Switch {
- boolean isOn();
- void press();
-}
diff --git a/src/main/java/guru/springframework/blog/dependencyinversionprinciple/highlevel/Switchable.java b/src/main/java/guru/springframework/blog/dependencyinversionprinciple/highlevel/Switchable.java
deleted file mode 100644
index 84f1ea9..0000000
--- a/src/main/java/guru/springframework/blog/dependencyinversionprinciple/highlevel/Switchable.java
+++ /dev/null
@@ -1,6 +0,0 @@
-package guru.springframework.blog.dependencyinversionprinciple.highlevel;
-
-public interface Switchable {
- void turnOn();
- void turnOff();
-}
diff --git a/src/main/java/guru/springframework/blog/dependencyinversionprinciple/lowlevel/Fan.java b/src/main/java/guru/springframework/blog/dependencyinversionprinciple/lowlevel/Fan.java
deleted file mode 100644
index 40c0123..0000000
--- a/src/main/java/guru/springframework/blog/dependencyinversionprinciple/lowlevel/Fan.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package guru.springframework.blog.dependencyinversionprinciple.lowlevel;
-
-import guru.springframework.blog.dependencyinversionprinciple.highlevel.Switchable;
-
-public class Fan implements Switchable {
- @Override
- public void turnOn() {
- System.out.println("Fan: Fan turned on...");
- }
-
- @Override
- public void turnOff() {
- System.out.println("Fan: Fan turned off...");
- }
-}
diff --git a/src/main/java/guru/springframework/blog/dependencyinversionprinciple/lowlevel/LightBulb.java b/src/main/java/guru/springframework/blog/dependencyinversionprinciple/lowlevel/LightBulb.java
deleted file mode 100644
index 42a0154..0000000
--- a/src/main/java/guru/springframework/blog/dependencyinversionprinciple/lowlevel/LightBulb.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package guru.springframework.blog.dependencyinversionprinciple.lowlevel;
-
-import guru.springframework.blog.dependencyinversionprinciple.highlevel.Switchable;
-
-public class LightBulb implements Switchable {
- @Override
- public void turnOn() {
- System.out.println("LightBulb: Bulb turned on...");
- }
-
- @Override
- public void turnOff() {
- System.out.println("LightBulb: Bulb turned off...");
- }
-}
diff --git a/src/main/java/guru/springframework/blog/hibernatepagination/dao/ProductDao.java b/src/main/java/guru/springframework/blog/hibernatepagination/dao/ProductDao.java
deleted file mode 100644
index 3a7731a..0000000
--- a/src/main/java/guru/springframework/blog/hibernatepagination/dao/ProductDao.java
+++ /dev/null
@@ -1,143 +0,0 @@
-package guru.springframework.blog.hibernatepagination.dao;
-
-import guru.springframework.blog.hibernatepagination.domain.Product;
-import guru.springframework.blog.hibernatepagination.util.HibernateUtil;
-import org.hibernate.*;
-import org.hibernate.criterion.Order;
-import org.hibernate.criterion.Projections;
-import org.hibernate.criterion.Restrictions;
-
-import java.util.List;
-
-public class ProductDao {
- public void saveProducts() {
- Session session = HibernateUtil.getSessionFactory().openSession();
- Transaction trans = null;
- try {
- trans = session.beginTransaction();
- for (int i = 0; i < 30; i++) {
- Product product = new Product();
- product.setProductName("Product_" + i);
- session.save(product);
- }
- trans.commit();
- } catch (HibernateException e) {
- trans.rollback();
- e.printStackTrace();
- } finally {
- session.close();
- }
- System.out.println("Saved 30 products");
- }
-
- public int listPaginatedProductsUsingQuery(int firstResult, int maxResults) {
- int paginatedCount = 0;
- Session session = HibernateUtil.getSessionFactory().openSession();
- try {
- Query query = session.createQuery("From Product");
- query.setFirstResult(firstResult);
- query.setMaxResults(maxResults);
- List products = (List) query.list();
- if (products != null) {
- paginatedCount = products.size();
- System.out.println("Total Results: " + paginatedCount);
- for (Product product : products) {
- System.out.println("Retrieved Product using Query. Name: " + product.getProductName());
- }
- }
-
- } catch (HibernateException e) {
- e.printStackTrace();
-
- } finally {
- session.close();
- }
- return paginatedCount;
- }
-
-
- public int listPaginatedProductsUsingCriteria(int firstResult, int maxResults) {
- int paginatedCount = 0;
- Session session = HibernateUtil.getSessionFactory().openSession();
- try {
- Criteria criteria = session.createCriteria(Product.class);
- criteria.setFirstResult(firstResult);
- criteria.setMaxResults(maxResults);
- List products = (List) criteria.list();
- if (products != null) {
- paginatedCount = products.size();
- System.out.println("Total Results: " + paginatedCount);
- for (Product product : products) {
- System.out.println("Retrieved Product using Criteria. Name: " + product.getProductName());
- }
- }
-
- } catch (HibernateException e) {
- e.printStackTrace();
- } finally {
- session.close();
- }
- return paginatedCount;
- }
-
- public int listPaginatedProductsUsingScrollableResults(int firstResult, int maxResults ) {
- int totalRecords=0;
- final Session session = HibernateUtil.getSessionFactory().openSession();
- try {
-
- ScrollableResults scrollableResults = getCriteria(session).scroll();
- scrollableResults.last();
- totalRecords=scrollableResults.getRowNumber()+1;
- scrollableResults.close();
- Criteria criteria = getCriteria(session);
- criteria.setFirstResult(firstResult);
- criteria.setMaxResults(maxResults);
- List