From 4a3c6f550d4d0104485ce129f5b64c053a448a27 Mon Sep 17 00:00:00 2001 From: Ranga Rao Karanam Date: Tue, 17 Jan 2017 11:28:00 +0530 Subject: [PATCH] 10 steps updated --- Step06.md | 541 ++++++++++++++++++++++++----------------------- Step07.md | 550 ++++++++++++++++++++++++------------------------ Step08.md | 554 +++++++++++++++++++++++++----------------------- Step09.md | 361 -------------------------------- Step10.md | 613 ++++++++++++++++++++++++++++-------------------------- 5 files changed, 1169 insertions(+), 1450 deletions(-) diff --git a/Step06.md b/Step06.md index 9e6589e..04bd517 100644 --- a/Step06.md +++ b/Step06.md @@ -6,224 +6,205 @@ - We use hard-coded data to get started ## Files List -### /pom.xml +### pom.xml ``` - 4.0.0 - com.in28minutes - springboot-for-beginners-example - 0.0.1-SNAPSHOT - Your First Spring Boot Example - jar - - - org.springframework.boot - spring-boot-starter-parent - 1.4.0.RELEASE - - - - - org.springframework.boot - spring-boot-starter-web - - - - - 1.8 - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.in28minutes.springboot + first-springboot-project + 0.0.1-SNAPSHOT + + org.springframework.boot + spring-boot-starter-parent + 1.4.0.RELEASE + + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + ``` -### /src/main/java/com/in28minutes/springboot/Application.java +### src/main/java/com/in28minutes/springboot/Application.java ``` package com.in28minutes.springboot; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Component; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class Application { - public static void main(String[] args) { - ApplicationContext ctx = SpringApplication.run(Application.class, args); - - } - - @RestController - class SomeBean { - - @Autowired - private SomeDependency someDependency; - - @RequestMapping("/") - public String index() { - return someDependency.getSomething(); - } + public static void main(String[] args) { + ApplicationContext ctx = SpringApplication.run(Application.class, args); - } - - @Component - class SomeDependency { - - public String getSomething() { - return "Hello! Welcome!"; - } - - } + } } ``` -### /src/main/java/com/in28minutes/springboot/model/Question.java +### src/main/java/com/in28minutes/springboot/model/Question.java ``` package com.in28minutes.springboot.model; import java.util.List; public class Question { - private String id; - private String description; - private String correctAnswer; - private List options; - - // Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException: - // Can not construct instance of com.in28minutes.springboot.model.Question: - // no suitable constructor found, can not deserialize from Object value - // (missing default constructor or creator, or perhaps need to add/enable - // type information?) - public Question() { - - } - - public Question(String id, String description, String correctAnswer, - List options) { - super(); - this.id = id; - this.description = description; - this.correctAnswer = correctAnswer; - this.options = options; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public String getCorrectAnswer() { - return correctAnswer; - } - - public List getOptions() { - return options; - } - - @Override - public String toString() { - return String - .format("Question [id=%s, description=%s, correctAnswer=%s, options=%s]", - id, description, correctAnswer, options); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (id == null ? 0 : id.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - Question other = (Question) obj; - if (id == null) { - if (other.id != null) { - return false; - } - } else if (!id.equals(other.id)) { - return false; - } - return true; - } + private String id; + private String description; + private String correctAnswer; + private List options; + + // Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException: + // Can not construct instance of com.in28minutes.springboot.model.Question: + // no suitable constructor found, can not deserialize from Object value + // (missing default constructor or creator, or perhaps need to add/enable + // type information?) + public Question() { + + } + + public Question(String id, String description, String correctAnswer, + List options) { + super(); + this.id = id; + this.description = description; + this.correctAnswer = correctAnswer; + this.options = options; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public String getCorrectAnswer() { + return correctAnswer; + } + + public List getOptions() { + return options; + } + + @Override + public String toString() { + return String + .format("Question [id=%s, description=%s, correctAnswer=%s, options=%s]", + id, description, correctAnswer, options); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Question other = (Question) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } } ``` -### /src/main/java/com/in28minutes/springboot/model/Survey.java +### src/main/java/com/in28minutes/springboot/model/Survey.java ``` package com.in28minutes.springboot.model; import java.util.List; public class Survey { - private String id; - private String title; - private String description; - private List questions; - - public Survey(String id, String title, String description, - List questions) { - super(); - this.id = id; - this.title = title; - this.description = description; - this.questions = questions; - } - - public String getId() { - return id; - } - - public String getTitle() { - return title; - } - - public String getDescription() { - return description; - } - - public List getQuestions() { - return questions; - } - - @Override - public String toString() { - return String.format( - "Survey [id=%s, title=%s, description=%s, questions=%s]", id, - title, description, questions); - } + private String id; + private String title; + private String description; + private List questions; + + public Survey(String id, String title, String description, + List questions) { + super(); + this.id = id; + this.title = title; + this.description = description; + this.questions = questions; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public List getQuestions() { + return questions; + } + + public void setQuestions(List questions) { + this.questions = questions; + } + + @Override + public String toString() { + return "Survey [id=" + id + ", title=" + title + ", description=" + + description + ", questions=" + questions + "]"; + } } ``` -### /src/main/java/com/in28minutes/springboot/service/SurveyService.java +### src/main/java/com/in28minutes/springboot/service/SurveyService.java ``` package com.in28minutes.springboot.service; @@ -240,88 +221,124 @@ import com.in28minutes.springboot.model.Survey; @Component public class SurveyService { - private static List surveys = new ArrayList<>(); - static { - Question question1 = new Question("Question1", - "Largest Country in the World", "Russia", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question2 = new Question("Question2", - "Most Populus Country in the World", "China", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question3 = new Question("Question3", - "Highest GDP in the World", "United States", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question4 = new Question("Question4", - "Second largest english speaking country", "India", - Arrays.asList("India", "Russia", "United States", "China")); - - List questions = new ArrayList<>(Arrays.asList(question1, - question2, question3, question4)); - - Survey survey = new Survey("Survey1", "My Favorite Survey", - "Description of the Survey", questions); - - surveys.add(survey); - } - - public List retrieveAllSurveys() { - return surveys; - } - - public Survey retrieveSurvey(String surveyId) { - for (Survey survey : surveys) { - if (survey.getId().equals(surveyId)) { - return survey; - } - } - return null; - } - - public List retrieveQuestions(String surveyId) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - return survey.getQuestions(); - } - - public Question retrieveQuestion(String surveyId, String questionId) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - for (Question question : survey.getQuestions()) { - if (question.getId().equals(questionId)) { - return question; - } - } - - return null; - } - - private SecureRandom random = new SecureRandom(); - - public Question addQuestion(String surveyId, Question question) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - String randomId = new BigInteger(130, random).toString(32); - question.setId(randomId); - - survey.getQuestions().add(question); - - return question; - } + private static List surveys = new ArrayList<>(); + static { + Question question1 = new Question("Question1", + "Largest Country in the World", "Russia", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question2 = new Question("Question2", + "Most Populus Country in the World", "China", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question3 = new Question("Question3", + "Highest GDP in the World", "United States", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question4 = new Question("Question4", + "Second largest english speaking country", "India", Arrays + .asList("India", "Russia", "United States", "China")); + + List questions = new ArrayList<>(Arrays.asList(question1, + question2, question3, question4)); + + Survey survey = new Survey("Survey1", "My Favorite Survey", + "Description of the Survey", questions); + + surveys.add(survey); + } + + public List retrieveAllSurveys() { + return surveys; + } + + public Survey retrieveSurvey(String surveyId) { + for (Survey survey : surveys) { + if (survey.getId().equals(surveyId)) { + return survey; + } + } + return null; + } + + public List retrieveQuestions(String surveyId) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + return survey.getQuestions(); + } + + public Question retrieveQuestion(String surveyId, String questionId) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + for (Question question : survey.getQuestions()) { + if (question.getId().equals(questionId)) { + return question; + } + } + + return null; + } + + private SecureRandom random = new SecureRandom(); + + public Question addQuestion(String surveyId, Question question) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + String randomId = new BigInteger(130, random).toString(32); + question.setId(randomId); + + survey.getQuestions().add(question); + + return question; + } +} +``` +### src/main/java/com/in28minutes/springboot/WelcomeController.java +``` +package com.in28minutes.springboot; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class WelcomeController { + + //Auto wiring + @Autowired + private WelcomeService service; + + @RequestMapping("/welcome") + public String welcome() { + return service.retrieveWelcomeMessage(); + } +} +``` +### src/main/java/com/in28minutes/springboot/WelcomeService.java +``` +package com.in28minutes.springboot; + +import org.springframework.stereotype.Component; + +@Component +public class WelcomeService { + + public String retrieveWelcomeMessage() { + //Complex Method + return "Good Morning updated"; + } } ``` -### /src/main/resources/application.properties +### src/main/resources/application.properties ``` logging.level.org.springframework: DEBUG ``` diff --git a/Step07.md b/Step07.md index 39a4cff..920fa83 100644 --- a/Step07.md +++ b/Step07.md @@ -38,89 +38,66 @@ class SurveyController { - Try to think about how the URI for retrieving the details of a specific question should be! ## Files List -### /pom.xml +### pom.xml ``` - 4.0.0 - com.in28minutes - springboot-for-beginners-example - 0.0.1-SNAPSHOT - Your First Spring Boot Example - jar - - - org.springframework.boot - spring-boot-starter-parent - 1.4.0.RELEASE - - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.in28minutes.springboot + first-springboot-project + 0.0.1-SNAPSHOT + + org.springframework.boot + spring-boot-starter-parent + 1.4.0.RELEASE + + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot - spring-boot-starter-web + spring-boot-devtools + true - - - - 1.8 - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + ``` -### /src/main/java/com/in28minutes/springboot/Application.java +### src/main/java/com/in28minutes/springboot/Application.java ``` package com.in28minutes.springboot; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Component; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class Application { - public static void main(String[] args) { - ApplicationContext ctx = SpringApplication.run(Application.class, args); - - } - - @RestController - class SomeBean { - - @Autowired - private SomeDependency someDependency; - - @RequestMapping("/") - public String index() { - return someDependency.getSomething(); - } - - } - - @Component - class SomeDependency { + public static void main(String[] args) { + ApplicationContext ctx = SpringApplication.run(Application.class, args); - public String getSomething() { - return "Hello! Welcome!"; - } - - } + } } ``` -### /src/main/java/com/in28minutes/springboot/controller/SurveyController.java +### src/main/java/com/in28minutes/springboot/controller/SurveyController.java ``` package com.in28minutes.springboot.controller; @@ -136,158 +113,161 @@ import com.in28minutes.springboot.service.SurveyService; @RestController class SurveyController { - @Autowired - private SurveyService surveyService; - - @GetMapping("/surveys/{surveyId}/questions") - public List retrieveQuestions(@PathVariable String surveyId) { - List retrieveQuestions = surveyService - .retrieveQuestions(surveyId); - - if (retrieveQuestions == null) { - throw new RuntimeException("Survey not found"); - } + @Autowired + private SurveyService surveyService; - return retrieveQuestions; - } + @GetMapping("/surveys/{surveyId}/questions") + public List retrieveQuestions(@PathVariable String surveyId) { + return surveyService.retrieveQuestions(surveyId); + } } ``` -### /src/main/java/com/in28minutes/springboot/model/Question.java +### src/main/java/com/in28minutes/springboot/model/Question.java ``` package com.in28minutes.springboot.model; import java.util.List; public class Question { - private String id; - private String description; - private String correctAnswer; - private List options; - - // Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException: - // Can not construct instance of com.in28minutes.springboot.model.Question: - // no suitable constructor found, can not deserialize from Object value - // (missing default constructor or creator, or perhaps need to add/enable - // type information?) - public Question() { - - } - - public Question(String id, String description, String correctAnswer, - List options) { - super(); - this.id = id; - this.description = description; - this.correctAnswer = correctAnswer; - this.options = options; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public String getCorrectAnswer() { - return correctAnswer; - } - - public List getOptions() { - return options; - } - - @Override - public String toString() { - return String - .format("Question [id=%s, description=%s, correctAnswer=%s, options=%s]", - id, description, correctAnswer, options); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (id == null ? 0 : id.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - Question other = (Question) obj; - if (id == null) { - if (other.id != null) { - return false; - } - } else if (!id.equals(other.id)) { - return false; - } - return true; - } + private String id; + private String description; + private String correctAnswer; + private List options; + + // Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException: + // Can not construct instance of com.in28minutes.springboot.model.Question: + // no suitable constructor found, can not deserialize from Object value + // (missing default constructor or creator, or perhaps need to add/enable + // type information?) + public Question() { + + } + + public Question(String id, String description, String correctAnswer, + List options) { + super(); + this.id = id; + this.description = description; + this.correctAnswer = correctAnswer; + this.options = options; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public String getCorrectAnswer() { + return correctAnswer; + } + + public List getOptions() { + return options; + } + + @Override + public String toString() { + return String + .format("Question [id=%s, description=%s, correctAnswer=%s, options=%s]", + id, description, correctAnswer, options); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Question other = (Question) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } } ``` -### /src/main/java/com/in28minutes/springboot/model/Survey.java +### src/main/java/com/in28minutes/springboot/model/Survey.java ``` package com.in28minutes.springboot.model; import java.util.List; public class Survey { - private String id; - private String title; - private String description; - private List questions; - - public Survey(String id, String title, String description, - List questions) { - super(); - this.id = id; - this.title = title; - this.description = description; - this.questions = questions; - } - - public String getId() { - return id; - } - - public String getTitle() { - return title; - } - - public String getDescription() { - return description; - } - - public List getQuestions() { - return questions; - } - - @Override - public String toString() { - return String.format( - "Survey [id=%s, title=%s, description=%s, questions=%s]", id, - title, description, questions); - } + private String id; + private String title; + private String description; + private List questions; + + public Survey(String id, String title, String description, + List questions) { + super(); + this.id = id; + this.title = title; + this.description = description; + this.questions = questions; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public List getQuestions() { + return questions; + } + + public void setQuestions(List questions) { + this.questions = questions; + } + + @Override + public String toString() { + return "Survey [id=" + id + ", title=" + title + ", description=" + + description + ", questions=" + questions + "]"; + } } ``` -### /src/main/java/com/in28minutes/springboot/service/SurveyService.java +### src/main/java/com/in28minutes/springboot/service/SurveyService.java ``` package com.in28minutes.springboot.service; @@ -304,88 +284,124 @@ import com.in28minutes.springboot.model.Survey; @Component public class SurveyService { - private static List surveys = new ArrayList<>(); - static { - Question question1 = new Question("Question1", - "Largest Country in the World", "Russia", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question2 = new Question("Question2", - "Most Populus Country in the World", "China", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question3 = new Question("Question3", - "Highest GDP in the World", "United States", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question4 = new Question("Question4", - "Second largest english speaking country", "India", - Arrays.asList("India", "Russia", "United States", "China")); - - List questions = new ArrayList<>(Arrays.asList(question1, - question2, question3, question4)); - - Survey survey = new Survey("Survey1", "My Favorite Survey", - "Description of the Survey", questions); - - surveys.add(survey); - } - - public List retrieveAllSurveys() { - return surveys; - } - - public Survey retrieveSurvey(String surveyId) { - for (Survey survey : surveys) { - if (survey.getId().equals(surveyId)) { - return survey; - } - } - return null; - } - - public List retrieveQuestions(String surveyId) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - return survey.getQuestions(); - } - - public Question retrieveQuestion(String surveyId, String questionId) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - for (Question question : survey.getQuestions()) { - if (question.getId().equals(questionId)) { - return question; - } - } + private static List surveys = new ArrayList<>(); + static { + Question question1 = new Question("Question1", + "Largest Country in the World", "Russia", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question2 = new Question("Question2", + "Most Populus Country in the World", "China", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question3 = new Question("Question3", + "Highest GDP in the World", "United States", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question4 = new Question("Question4", + "Second largest english speaking country", "India", Arrays + .asList("India", "Russia", "United States", "China")); + + List questions = new ArrayList<>(Arrays.asList(question1, + question2, question3, question4)); + + Survey survey = new Survey("Survey1", "My Favorite Survey", + "Description of the Survey", questions); + + surveys.add(survey); + } + + public List retrieveAllSurveys() { + return surveys; + } + + public Survey retrieveSurvey(String surveyId) { + for (Survey survey : surveys) { + if (survey.getId().equals(surveyId)) { + return survey; + } + } + return null; + } + + public List retrieveQuestions(String surveyId) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + return survey.getQuestions(); + } + + public Question retrieveQuestion(String surveyId, String questionId) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + for (Question question : survey.getQuestions()) { + if (question.getId().equals(questionId)) { + return question; + } + } + + return null; + } + + private SecureRandom random = new SecureRandom(); + + public Question addQuestion(String surveyId, Question question) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + String randomId = new BigInteger(130, random).toString(32); + question.setId(randomId); + + survey.getQuestions().add(question); + + return question; + } +} +``` +### src/main/java/com/in28minutes/springboot/WelcomeController.java +``` +package com.in28minutes.springboot; - return null; - } +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; - private SecureRandom random = new SecureRandom(); +@RestController +public class WelcomeController { - public Question addQuestion(String surveyId, Question question) { - Survey survey = retrieveSurvey(surveyId); + //Auto wiring + @Autowired + private WelcomeService service; - if (survey == null) { - return null; - } + @RequestMapping("/welcome") + public String welcome() { + return service.retrieveWelcomeMessage(); + } +} +``` +### src/main/java/com/in28minutes/springboot/WelcomeService.java +``` +package com.in28minutes.springboot; - String randomId = new BigInteger(130, random).toString(32); - question.setId(randomId); +import org.springframework.stereotype.Component; - survey.getQuestions().add(question); +@Component +public class WelcomeService { - return question; - } + public String retrieveWelcomeMessage() { + //Complex Method + return "Good Morning updated"; + } } ``` -### /src/main/resources/application.properties +### src/main/resources/application.properties ``` logging.level.org.springframework: DEBUG ``` diff --git a/Step08.md b/Step08.md index cf12491..a829409 100644 --- a/Step08.md +++ b/Step08.md @@ -24,89 +24,66 @@ First Snippet - Write the method to retrieve all surveys! ## Files List -### /pom.xml +### pom.xml ``` - 4.0.0 - com.in28minutes - springboot-for-beginners-example - 0.0.1-SNAPSHOT - Your First Spring Boot Example - jar - - - org.springframework.boot - spring-boot-starter-parent - 1.4.0.RELEASE - - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.in28minutes.springboot + first-springboot-project + 0.0.1-SNAPSHOT + + org.springframework.boot + spring-boot-starter-parent + 1.4.0.RELEASE + + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot - spring-boot-starter-web + spring-boot-devtools + true - - - - 1.8 - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + ``` -### /src/main/java/com/in28minutes/springboot/Application.java +### src/main/java/com/in28minutes/springboot/Application.java ``` package com.in28minutes.springboot; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Component; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class Application { - public static void main(String[] args) { - ApplicationContext ctx = SpringApplication.run(Application.class, args); - - } - - @RestController - class SomeBean { - - @Autowired - private SomeDependency someDependency; - - @RequestMapping("/") - public String index() { - return someDependency.getSomething(); - } - - } - - @Component - class SomeDependency { + public static void main(String[] args) { + ApplicationContext ctx = SpringApplication.run(Application.class, args); - public String getSomething() { - return "Hello! Welcome!"; - } - - } + } } ``` -### /src/main/java/com/in28minutes/springboot/controller/SurveyController.java +### src/main/java/com/in28minutes/springboot/controller/SurveyController.java ``` package com.in28minutes.springboot.controller; @@ -122,158 +99,169 @@ import com.in28minutes.springboot.service.SurveyService; @RestController class SurveyController { - @Autowired - private SurveyService surveyService; + @Autowired + private SurveyService surveyService; - @GetMapping("/surveys/{surveyId}/questions") - public List retrieveQuestions(@PathVariable String surveyId) { - return surveyService.retrieveQuestions(surveyId); - } + @GetMapping("/surveys/{surveyId}/questions") + public List retrieveQuestions(@PathVariable String surveyId) { + return surveyService.retrieveQuestions(surveyId); + } - @GetMapping(path = "/surveys/{surveyId}/questions/{questionId}") - public Question retrieveQuestion(@PathVariable String surveyId, - @PathVariable String questionId) { - return surveyService.retrieveQuestion(surveyId, questionId); - } + // GET "/surveys/{surveyId}/questions/{questionId}" + @GetMapping("/surveys/{surveyId}/questions/{questionId}") + public Question retrieveDetailsForQuestion(@PathVariable String surveyId, + @PathVariable String questionId) { + return surveyService.retrieveQuestion(surveyId, questionId); + } } ``` -### /src/main/java/com/in28minutes/springboot/model/Question.java +### src/main/java/com/in28minutes/springboot/model/Question.java ``` package com.in28minutes.springboot.model; import java.util.List; public class Question { - private String id; - private String description; - private String correctAnswer; - private List options; - - // Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException: - // Can not construct instance of com.in28minutes.springboot.model.Question: - // no suitable constructor found, can not deserialize from Object value - // (missing default constructor or creator, or perhaps need to add/enable - // type information?) - public Question() { - - } - - public Question(String id, String description, String correctAnswer, - List options) { - super(); - this.id = id; - this.description = description; - this.correctAnswer = correctAnswer; - this.options = options; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public String getCorrectAnswer() { - return correctAnswer; - } - - public List getOptions() { - return options; - } - - @Override - public String toString() { - return String - .format("Question [id=%s, description=%s, correctAnswer=%s, options=%s]", - id, description, correctAnswer, options); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (id == null ? 0 : id.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - Question other = (Question) obj; - if (id == null) { - if (other.id != null) { - return false; - } - } else if (!id.equals(other.id)) { - return false; - } - return true; - } + private String id; + private String description; + private String correctAnswer; + private List options; + + // Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException: + // Can not construct instance of com.in28minutes.springboot.model.Question: + // no suitable constructor found, can not deserialize from Object value + // (missing default constructor or creator, or perhaps need to add/enable + // type information?) + public Question() { + + } + + public Question(String id, String description, String correctAnswer, + List options) { + super(); + this.id = id; + this.description = description; + this.correctAnswer = correctAnswer; + this.options = options; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public String getCorrectAnswer() { + return correctAnswer; + } + + public List getOptions() { + return options; + } + + @Override + public String toString() { + return String + .format("Question [id=%s, description=%s, correctAnswer=%s, options=%s]", + id, description, correctAnswer, options); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Question other = (Question) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } } ``` -### /src/main/java/com/in28minutes/springboot/model/Survey.java +### src/main/java/com/in28minutes/springboot/model/Survey.java ``` package com.in28minutes.springboot.model; import java.util.List; public class Survey { - private String id; - private String title; - private String description; - private List questions; - - public Survey(String id, String title, String description, - List questions) { - super(); - this.id = id; - this.title = title; - this.description = description; - this.questions = questions; - } - - public String getId() { - return id; - } - - public String getTitle() { - return title; - } - - public String getDescription() { - return description; - } - - public List getQuestions() { - return questions; - } - - @Override - public String toString() { - return String.format( - "Survey [id=%s, title=%s, description=%s, questions=%s]", id, - title, description, questions); - } + private String id; + private String title; + private String description; + private List questions; + + public Survey(String id, String title, String description, + List questions) { + super(); + this.id = id; + this.title = title; + this.description = description; + this.questions = questions; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public List getQuestions() { + return questions; + } + + public void setQuestions(List questions) { + this.questions = questions; + } + + @Override + public String toString() { + return "Survey [id=" + id + ", title=" + title + ", description=" + + description + ", questions=" + questions + "]"; + } } ``` -### /src/main/java/com/in28minutes/springboot/service/SurveyService.java +### src/main/java/com/in28minutes/springboot/service/SurveyService.java ``` package com.in28minutes.springboot.service; @@ -290,88 +278,124 @@ import com.in28minutes.springboot.model.Survey; @Component public class SurveyService { - private static List surveys = new ArrayList<>(); - static { - Question question1 = new Question("Question1", - "Largest Country in the World", "Russia", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question2 = new Question("Question2", - "Most Populus Country in the World", "China", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question3 = new Question("Question3", - "Highest GDP in the World", "United States", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question4 = new Question("Question4", - "Second largest english speaking country", "India", - Arrays.asList("India", "Russia", "United States", "China")); - - List questions = new ArrayList<>(Arrays.asList(question1, - question2, question3, question4)); - - Survey survey = new Survey("Survey1", "My Favorite Survey", - "Description of the Survey", questions); - - surveys.add(survey); - } - - public List retrieveAllSurveys() { - return surveys; - } - - public Survey retrieveSurvey(String surveyId) { - for (Survey survey : surveys) { - if (survey.getId().equals(surveyId)) { - return survey; - } - } - return null; - } - - public List retrieveQuestions(String surveyId) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - return survey.getQuestions(); - } - - public Question retrieveQuestion(String surveyId, String questionId) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - for (Question question : survey.getQuestions()) { - if (question.getId().equals(questionId)) { - return question; - } - } + private static List surveys = new ArrayList<>(); + static { + Question question1 = new Question("Question1", + "Largest Country in the World", "Russia", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question2 = new Question("Question2", + "Most Populus Country in the World", "China", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question3 = new Question("Question3", + "Highest GDP in the World", "United States", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question4 = new Question("Question4", + "Second largest english speaking country", "India", Arrays + .asList("India", "Russia", "United States", "China")); + + List questions = new ArrayList<>(Arrays.asList(question1, + question2, question3, question4)); + + Survey survey = new Survey("Survey1", "My Favorite Survey", + "Description of the Survey", questions); + + surveys.add(survey); + } + + public List retrieveAllSurveys() { + return surveys; + } + + public Survey retrieveSurvey(String surveyId) { + for (Survey survey : surveys) { + if (survey.getId().equals(surveyId)) { + return survey; + } + } + return null; + } + + public List retrieveQuestions(String surveyId) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + return survey.getQuestions(); + } + + public Question retrieveQuestion(String surveyId, String questionId) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + for (Question question : survey.getQuestions()) { + if (question.getId().equals(questionId)) { + return question; + } + } + + return null; + } + + private SecureRandom random = new SecureRandom(); + + public Question addQuestion(String surveyId, Question question) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + String randomId = new BigInteger(130, random).toString(32); + question.setId(randomId); + + survey.getQuestions().add(question); + + return question; + } +} +``` +### src/main/java/com/in28minutes/springboot/WelcomeController.java +``` +package com.in28minutes.springboot; - return null; - } +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; - private SecureRandom random = new SecureRandom(); +@RestController +public class WelcomeController { - public Question addQuestion(String surveyId, Question question) { - Survey survey = retrieveSurvey(surveyId); + //Auto wiring + @Autowired + private WelcomeService service; - if (survey == null) { - return null; - } + @RequestMapping("/welcome") + public String welcome() { + return service.retrieveWelcomeMessage(); + } +} +``` +### src/main/java/com/in28minutes/springboot/WelcomeService.java +``` +package com.in28minutes.springboot; - String randomId = new BigInteger(130, random).toString(32); - question.setId(randomId); +import org.springframework.stereotype.Component; - survey.getQuestions().add(question); +@Component +public class WelcomeService { - return question; - } + public String retrieveWelcomeMessage() { + //Complex Method + return "Good Morning updated"; + } } ``` -### /src/main/resources/application.properties +### src/main/resources/application.properties ``` logging.level.org.springframework: DEBUG ``` diff --git a/Step09.md b/Step09.md index 62e4823..0276b92 100644 --- a/Step09.md +++ b/Step09.md @@ -22,364 +22,3 @@ First Snippet ## Exercises - Make changes and see if they reflect immediately - -## Files List -### /pom.xml -``` - - 4.0.0 - com.in28minutes - springboot-for-beginners-example - 0.0.1-SNAPSHOT - Your First Spring Boot Example - jar - - - org.springframework.boot - spring-boot-starter-parent - 1.4.0.RELEASE - - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-devtools - true - - - - - - - 1.8 - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - -``` -### /src/main/java/com/in28minutes/springboot/Application.java -``` -package com.in28minutes.springboot; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Component; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@SpringBootApplication -public class Application { - - public static void main(String[] args) { - ApplicationContext ctx = SpringApplication.run(Application.class, args); - - } - - @RestController - class SomeBean { - - @Autowired - private SomeDependency someDependency; - - @RequestMapping("/") - public String index() { - return someDependency.getSomething(); - } - - } - - @Component - class SomeDependency { - - public String getSomething() { - return "Hello! Welcome!"; - } - - } - -} -``` -### /src/main/java/com/in28minutes/springboot/controller/SurveyController.java -``` -package com.in28minutes.springboot.controller; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; - -import com.in28minutes.springboot.model.Question; -import com.in28minutes.springboot.service.SurveyService; - -@RestController -class SurveyController { - @Autowired - private SurveyService surveyService; - - @GetMapping("/surveys/{surveyId}/questions") - public List retrieveQuestions(@PathVariable String surveyId) { - return surveyService.retrieveQuestions(surveyId); - } - - @GetMapping(path = "/surveys/{surveyId}/questions/{questionId}") - public Question retrieveQuestion(@PathVariable String surveyId, - @PathVariable String questionId) { - return surveyService.retrieveQuestion(surveyId, questionId); - } - -} -``` -### /src/main/java/com/in28minutes/springboot/model/Question.java -``` -package com.in28minutes.springboot.model; - -import java.util.List; - -public class Question { - private String id; - private String description; - private String correctAnswer; - private List options; - - // Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException: - // Can not construct instance of com.in28minutes.springboot.model.Question: - // no suitable constructor found, can not deserialize from Object value - // (missing default constructor or creator, or perhaps need to add/enable - // type information?) - public Question() { - - } - - public Question(String id, String description, String correctAnswer, - List options) { - super(); - this.id = id; - this.description = description; - this.correctAnswer = correctAnswer; - this.options = options; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public String getCorrectAnswer() { - return correctAnswer; - } - - public List getOptions() { - return options; - } - - @Override - public String toString() { - return String - .format("Question [id=%s, description=%s, correctAnswer=%s, options=%s]", - id, description, correctAnswer, options); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (id == null ? 0 : id.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - Question other = (Question) obj; - if (id == null) { - if (other.id != null) { - return false; - } - } else if (!id.equals(other.id)) { - return false; - } - return true; - } - -} -``` -### /src/main/java/com/in28minutes/springboot/model/Survey.java -``` -package com.in28minutes.springboot.model; - -import java.util.List; - -public class Survey { - private String id; - private String title; - private String description; - private List questions; - - public Survey(String id, String title, String description, - List questions) { - super(); - this.id = id; - this.title = title; - this.description = description; - this.questions = questions; - } - - public String getId() { - return id; - } - - public String getTitle() { - return title; - } - - public String getDescription() { - return description; - } - - public List getQuestions() { - return questions; - } - - @Override - public String toString() { - return String.format( - "Survey [id=%s, title=%s, description=%s, questions=%s]", id, - title, description, questions); - } - -} -``` -### /src/main/java/com/in28minutes/springboot/service/SurveyService.java -``` -package com.in28minutes.springboot.service; - -import java.math.BigInteger; -import java.security.SecureRandom; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.springframework.stereotype.Component; - -import com.in28minutes.springboot.model.Question; -import com.in28minutes.springboot.model.Survey; - -@Component -public class SurveyService { - private static List surveys = new ArrayList<>(); - static { - Question question1 = new Question("Question1", - "Largest Country in the World", "Russia", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question2 = new Question("Question2", - "Most Populus Country in the World", "China", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question3 = new Question("Question3", - "Highest GDP in the World", "United States", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question4 = new Question("Question4", - "Second largest english speaking country", "India", - Arrays.asList("India", "Russia", "United States", "China")); - - List questions = new ArrayList<>(Arrays.asList(question1, - question2, question3, question4)); - - Survey survey = new Survey("Survey1", "My Favorite Survey", - "Description of the Survey", questions); - - surveys.add(survey); - } - - public List retrieveAllSurveys() { - return surveys; - } - - public Survey retrieveSurvey(String surveyId) { - for (Survey survey : surveys) { - if (survey.getId().equals(surveyId)) { - return survey; - } - } - return null; - } - - public List retrieveQuestions(String surveyId) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - return survey.getQuestions(); - } - - public Question retrieveQuestion(String surveyId, String questionId) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - for (Question question : survey.getQuestions()) { - if (question.getId().equals(questionId)) { - return question; - } - } - - return null; - } - - private SecureRandom random = new SecureRandom(); - - public Question addQuestion(String surveyId, Question question) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - String randomId = new BigInteger(130, random).toString(32); - question.setId(randomId); - - survey.getQuestions().add(question); - - return question; - } -} -``` -### /src/main/resources/application.properties -``` -logging.level.org.springframework: DEBUG -``` diff --git a/Step10.md b/Step10.md index deb6a59..d1655ba 100644 --- a/Step10.md +++ b/Step10.md @@ -6,8 +6,15 @@ - ResponseEntity.created(location).build() - ResponseEntity.noContent().build() - Using Postman : https://www.getpostman.com - + - URL to POST to - http://localhost:8080/surveys/Survey1/questions + ## Useful Snippets and References + +Sample Body for POST Request +``` +{"description":"Second Most Populous Country in the World","correctAnswer":"India","options":["India","Russia","United States","China"]} +``` + First Snippet ``` @PostMapping("/surveys/{surveyId}/questions") @@ -27,106 +34,71 @@ First Snippet } ``` -Second Snippet -``` -{"description":"Second Most Populous Country in the World","correctAnswer":"India","options":["India","Russia","United States","China"]} -``` ## Exercises - Create more REST services of your choice ## Files List -### /pom.xml +### pom.xml ``` - 4.0.0 - com.in28minutes - springboot-for-beginners-example - 0.0.1-SNAPSHOT - Your First Spring Boot Example - jar - - - org.springframework.boot - spring-boot-starter-parent - 1.4.0.RELEASE - - - - - org.springframework.boot - spring-boot-starter-web - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.in28minutes.springboot + first-springboot-project + 0.0.1-SNAPSHOT + + org.springframework.boot + spring-boot-starter-parent + 1.4.0.RELEASE + + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot spring-boot-devtools true - - - - - - 1.8 - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + ``` -### /src/main/java/com/in28minutes/springboot/Application.java +### src/main/java/com/in28minutes/springboot/Application.java ``` package com.in28minutes.springboot; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Component; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class Application { - public static void main(String[] args) { - ApplicationContext ctx = SpringApplication.run(Application.class, args); + public static void main(String[] args) { + ApplicationContext ctx = SpringApplication.run(Application.class, args); - } - - @RestController - class SomeBean { - - @Autowired - private SomeDependency someDependency; - - @RequestMapping("/") - public String index() { - return someDependency.getSomething(); - } - - } - - @Component - class SomeDependency { - - public String getSomething() { - return "Hello! Welcome!"; - } - - } + } } ``` -### /src/main/java/com/in28minutes/springboot/controller/SurveyController.java +### src/main/java/com/in28minutes/springboot/controller/SurveyController.java ``` package com.in28minutes.springboot.controller; @@ -147,175 +119,190 @@ import com.in28minutes.springboot.service.SurveyService; @RestController class SurveyController { - @Autowired - private SurveyService surveyService; - - @GetMapping("/surveys/{surveyId}/questions") - public List retrieveQuestions(@PathVariable String surveyId) { - return surveyService.retrieveQuestions(surveyId); - } - - @GetMapping(path = "/surveys/{surveyId}/questions/{questionId}") - public Question retrieveQuestion(@PathVariable String surveyId, - @PathVariable String questionId) { - return surveyService.retrieveQuestion(surveyId, questionId); - } - - @PostMapping("/surveys/{surveyId}/questions") - ResponseEntity add(@PathVariable String surveyId, - @RequestBody Question question) { - - Question createdTodo = surveyService.addQuestion(surveyId, question); - - if (createdTodo == null) { - return ResponseEntity.noContent().build(); - } - - URI location = ServletUriComponentsBuilder.fromCurrentRequest() - .path("/{id}").buildAndExpand(createdTodo.getId()).toUri(); - - return ResponseEntity.created(location).build(); - - } + @Autowired + private SurveyService surveyService; + + @GetMapping("/surveys/{surveyId}/questions") + public List retrieveQuestions(@PathVariable String surveyId) { + return surveyService.retrieveQuestions(surveyId); + } + + // GET "/surveys/{surveyId}/questions/{questionId}" + @GetMapping("/surveys/{surveyId}/questions/{questionId}") + public Question retrieveDetailsForQuestion(@PathVariable String surveyId, + @PathVariable String questionId) { + return surveyService.retrieveQuestion(surveyId, questionId); + } + + // /surveys/{surveyId}/questions + @PostMapping("/surveys/{surveyId}/questions") + public ResponseEntity addQuestionToSurvey( + @PathVariable String surveyId, @RequestBody Question newQuestion) { + + Question question = surveyService.addQuestion(surveyId, newQuestion); + + if (question == null) + return ResponseEntity.noContent().build(); + + // Success - URI of the new resource in Response Header + // Status - created + // URI -> /surveys/{surveyId}/questions/{questionId} + // question.getQuestionId() + URI location = ServletUriComponentsBuilder.fromCurrentRequest().path( + "/{id}").buildAndExpand(question.getId()).toUri(); + + // Status + return ResponseEntity.created(location).build(); + } } ``` -### /src/main/java/com/in28minutes/springboot/model/Question.java +### src/main/java/com/in28minutes/springboot/model/Question.java ``` package com.in28minutes.springboot.model; import java.util.List; public class Question { - private String id; - private String description; - private String correctAnswer; - private List options; - - // Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException: - // Can not construct instance of com.in28minutes.springboot.model.Question: - // no suitable constructor found, can not deserialize from Object value - // (missing default constructor or creator, or perhaps need to add/enable - // type information?) - public Question() { - - } - - public Question(String id, String description, String correctAnswer, - List options) { - super(); - this.id = id; - this.description = description; - this.correctAnswer = correctAnswer; - this.options = options; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public String getCorrectAnswer() { - return correctAnswer; - } - - public List getOptions() { - return options; - } - - @Override - public String toString() { - return String - .format("Question [id=%s, description=%s, correctAnswer=%s, options=%s]", - id, description, correctAnswer, options); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + (id == null ? 0 : id.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - Question other = (Question) obj; - if (id == null) { - if (other.id != null) { - return false; - } - } else if (!id.equals(other.id)) { - return false; - } - return true; - } + private String id; + private String description; + private String correctAnswer; + private List options; + + // Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException: + // Can not construct instance of com.in28minutes.springboot.model.Question: + // no suitable constructor found, can not deserialize from Object value + // (missing default constructor or creator, or perhaps need to add/enable + // type information?) + public Question() { + + } + + public Question(String id, String description, String correctAnswer, + List options) { + super(); + this.id = id; + this.description = description; + this.correctAnswer = correctAnswer; + this.options = options; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public String getCorrectAnswer() { + return correctAnswer; + } + + public List getOptions() { + return options; + } + + @Override + public String toString() { + return String + .format("Question [id=%s, description=%s, correctAnswer=%s, options=%s]", + id, description, correctAnswer, options); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Question other = (Question) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } } ``` -### /src/main/java/com/in28minutes/springboot/model/Survey.java +### src/main/java/com/in28minutes/springboot/model/Survey.java ``` package com.in28minutes.springboot.model; import java.util.List; public class Survey { - private String id; - private String title; - private String description; - private List questions; - - public Survey(String id, String title, String description, - List questions) { - super(); - this.id = id; - this.title = title; - this.description = description; - this.questions = questions; - } - - public String getId() { - return id; - } - - public String getTitle() { - return title; - } - - public String getDescription() { - return description; - } - - public List getQuestions() { - return questions; - } - - @Override - public String toString() { - return String.format( - "Survey [id=%s, title=%s, description=%s, questions=%s]", id, - title, description, questions); - } + private String id; + private String title; + private String description; + private List questions; + + public Survey(String id, String title, String description, + List questions) { + super(); + this.id = id; + this.title = title; + this.description = description; + this.questions = questions; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public List getQuestions() { + return questions; + } + + public void setQuestions(List questions) { + this.questions = questions; + } + + @Override + public String toString() { + return "Survey [id=" + id + ", title=" + title + ", description=" + + description + ", questions=" + questions + "]"; + } } ``` -### /src/main/java/com/in28minutes/springboot/service/SurveyService.java +### src/main/java/com/in28minutes/springboot/service/SurveyService.java ``` package com.in28minutes.springboot.service; @@ -332,88 +319,124 @@ import com.in28minutes.springboot.model.Survey; @Component public class SurveyService { - private static List surveys = new ArrayList<>(); - static { - Question question1 = new Question("Question1", - "Largest Country in the World", "Russia", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question2 = new Question("Question2", - "Most Populus Country in the World", "China", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question3 = new Question("Question3", - "Highest GDP in the World", "United States", Arrays.asList( - "India", "Russia", "United States", "China")); - Question question4 = new Question("Question4", - "Second largest english speaking country", "India", - Arrays.asList("India", "Russia", "United States", "China")); - - List questions = new ArrayList<>(Arrays.asList(question1, - question2, question3, question4)); - - Survey survey = new Survey("Survey1", "My Favorite Survey", - "Description of the Survey", questions); - - surveys.add(survey); - } - - public List retrieveAllSurveys() { - return surveys; - } - - public Survey retrieveSurvey(String surveyId) { - for (Survey survey : surveys) { - if (survey.getId().equals(surveyId)) { - return survey; - } - } - return null; - } - - public List retrieveQuestions(String surveyId) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - return survey.getQuestions(); - } - - public Question retrieveQuestion(String surveyId, String questionId) { - Survey survey = retrieveSurvey(surveyId); - - if (survey == null) { - return null; - } - - for (Question question : survey.getQuestions()) { - if (question.getId().equals(questionId)) { - return question; - } - } + private static List surveys = new ArrayList<>(); + static { + Question question1 = new Question("Question1", + "Largest Country in the World", "Russia", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question2 = new Question("Question2", + "Most Populus Country in the World", "China", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question3 = new Question("Question3", + "Highest GDP in the World", "United States", Arrays.asList( + "India", "Russia", "United States", "China")); + Question question4 = new Question("Question4", + "Second largest english speaking country", "India", Arrays + .asList("India", "Russia", "United States", "China")); + + List questions = new ArrayList<>(Arrays.asList(question1, + question2, question3, question4)); + + Survey survey = new Survey("Survey1", "My Favorite Survey", + "Description of the Survey", questions); + + surveys.add(survey); + } + + public List retrieveAllSurveys() { + return surveys; + } + + public Survey retrieveSurvey(String surveyId) { + for (Survey survey : surveys) { + if (survey.getId().equals(surveyId)) { + return survey; + } + } + return null; + } + + public List retrieveQuestions(String surveyId) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + return survey.getQuestions(); + } + + public Question retrieveQuestion(String surveyId, String questionId) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + for (Question question : survey.getQuestions()) { + if (question.getId().equals(questionId)) { + return question; + } + } + + return null; + } + + private SecureRandom random = new SecureRandom(); + + public Question addQuestion(String surveyId, Question question) { + Survey survey = retrieveSurvey(surveyId); + + if (survey == null) { + return null; + } + + String randomId = new BigInteger(130, random).toString(32); + question.setId(randomId); + + survey.getQuestions().add(question); + + return question; + } +} +``` +### src/main/java/com/in28minutes/springboot/WelcomeController.java +``` +package com.in28minutes.springboot; - return null; - } +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; - private SecureRandom random = new SecureRandom(); +@RestController +public class WelcomeController { - public Question addQuestion(String surveyId, Question question) { - Survey survey = retrieveSurvey(surveyId); + //Auto wiring + @Autowired + private WelcomeService service; - if (survey == null) { - return null; - } + @RequestMapping("/welcome") + public String welcome() { + return service.retrieveWelcomeMessage(); + } +} +``` +### src/main/java/com/in28minutes/springboot/WelcomeService.java +``` +package com.in28minutes.springboot; - String randomId = new BigInteger(130, random).toString(32); - question.setId(randomId); +import org.springframework.stereotype.Component; - survey.getQuestions().add(question); +@Component +public class WelcomeService { - return question; - } + public String retrieveWelcomeMessage() { + //Complex Method + return "Good Morning updated"; + } } ``` -### /src/main/resources/application.properties +### src/main/resources/application.properties ``` logging.level.org.springframework: DEBUG ```