@@ -67,391 +67,3 @@ Second Snippet
6767
6868## Exercises
6969- Execute other services using xml and see what happens!
70-
71- ## Files List
72- ### /pom.xml
73- ```
74- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
75- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
76- <modelVersion>4.0.0</modelVersion>
77- <groupId>com.in28minutes</groupId>
78- <artifactId>springboot-for-beginners-example</artifactId>
79- <version>0.0.1-SNAPSHOT</version>
80- <name>Your First Spring Boot Example</name>
81- <packaging>jar</packaging>
82-
83- <parent>
84- <groupId>org.springframework.boot</groupId>
85- <artifactId>spring-boot-starter-parent</artifactId>
86- <version>1.4.0.RELEASE</version>
87- </parent>
88-
89- <dependencies>
90- <dependency>
91- <groupId>org.springframework.boot</groupId>
92- <artifactId>spring-boot-starter-web</artifactId>
93- </dependency>
94-
95- <dependency>
96- <groupId>com.fasterxml.jackson.dataformat</groupId>
97- <artifactId>jackson-dataformat-xml</artifactId>
98- </dependency>
99-
100- <dependency>
101- <groupId>org.springframework.boot</groupId>
102- <artifactId>spring-boot-devtools</artifactId>
103- <optional>true</optional>
104- </dependency>
105-
106- </dependencies>
107-
108-
109- <properties>
110- <java.version>1.8</java.version>
111- </properties>
112-
113- <build>
114- <plugins>
115- <plugin>
116- <groupId>org.springframework.boot</groupId>
117- <artifactId>spring-boot-maven-plugin</artifactId>
118- </plugin>
119- </plugins>
120- </build>
121- </project>
122- ```
123- ### /src/main/java/com/in28minutes/springboot/Application.java
124- ```
125- package com.in28minutes.springboot;
126-
127- import org.springframework.beans.factory.annotation.Autowired;
128- import org.springframework.boot.SpringApplication;
129- import org.springframework.boot.autoconfigure.SpringBootApplication;
130- import org.springframework.context.ApplicationContext;
131- import org.springframework.stereotype.Component;
132- import org.springframework.web.bind.annotation.RequestMapping;
133- import org.springframework.web.bind.annotation.RestController;
134-
135- @SpringBootApplication
136- public class Application {
137-
138- public static void main(String[] args) {
139- ApplicationContext ctx = SpringApplication.run(Application.class, args);
140-
141- }
142-
143- @RestController
144- class SomeBean {
145-
146- @Autowired
147- private SomeDependency someDependency;
148-
149- @RequestMapping("/")
150- public String index() {
151- return someDependency.getSomething();
152- }
153-
154- }
155-
156- @Component
157- class SomeDependency {
158-
159- public String getSomething() {
160- return "Hello! Welcome!";
161- }
162-
163- }
164-
165- }
166- ```
167- ### /src/main/java/com/in28minutes/springboot/controller/SurveyController.java
168- ```
169- package com.in28minutes.springboot.controller;
170-
171- import java.net.URI;
172- import java.util.List;
173-
174- import org.springframework.beans.factory.annotation.Autowired;
175- import org.springframework.http.ResponseEntity;
176- import org.springframework.web.bind.annotation.GetMapping;
177- import org.springframework.web.bind.annotation.PathVariable;
178- import org.springframework.web.bind.annotation.PostMapping;
179- import org.springframework.web.bind.annotation.RequestBody;
180- import org.springframework.web.bind.annotation.RestController;
181- import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
182-
183- import com.in28minutes.springboot.model.Question;
184- import com.in28minutes.springboot.service.SurveyService;
185-
186- @RestController
187- class SurveyController {
188- @Autowired
189- private SurveyService surveyService;
190-
191- @GetMapping("/surveys/{surveyId}/questions")
192- public List<Question> retrieveQuestions(@PathVariable String surveyId) {
193- return surveyService.retrieveQuestions(surveyId);
194- }
195-
196- @GetMapping(path = "/surveys/{surveyId}/questions/{questionId}")
197- public Question retrieveQuestion(@PathVariable String surveyId,
198- @PathVariable String questionId) {
199- return surveyService.retrieveQuestion(surveyId, questionId);
200- }
201-
202- @PostMapping("/surveys/{surveyId}/questions")
203- ResponseEntity<?> add(@PathVariable String surveyId,
204- @RequestBody Question question) {
205-
206- Question createdTodo = surveyService.addQuestion(surveyId, question);
207-
208- if (createdTodo == null) {
209- return ResponseEntity.noContent().build();
210- }
211-
212- URI location = ServletUriComponentsBuilder.fromCurrentRequest()
213- .path("/{id}").buildAndExpand(createdTodo.getId()).toUri();
214-
215- return ResponseEntity.created(location).build();
216-
217- }
218-
219- }
220- ```
221- ### /src/main/java/com/in28minutes/springboot/model/Question.java
222- ```
223- package com.in28minutes.springboot.model;
224-
225- import java.util.List;
226-
227- public class Question {
228- private String id;
229- private String description;
230- private String correctAnswer;
231- private List<String> options;
232-
233- // Needed by Caused by: com.fasterxml.jackson.databind.JsonMappingException:
234- // Can not construct instance of com.in28minutes.springboot.model.Question:
235- // no suitable constructor found, can not deserialize from Object value
236- // (missing default constructor or creator, or perhaps need to add/enable
237- // type information?)
238- public Question() {
239-
240- }
241-
242- public Question(String id, String description, String correctAnswer,
243- List<String> options) {
244- super();
245- this.id = id;
246- this.description = description;
247- this.correctAnswer = correctAnswer;
248- this.options = options;
249- }
250-
251- public String getId() {
252- return id;
253- }
254-
255- public void setId(String id) {
256- this.id = id;
257- }
258-
259- public String getDescription() {
260- return description;
261- }
262-
263- public String getCorrectAnswer() {
264- return correctAnswer;
265- }
266-
267- public List<String> getOptions() {
268- return options;
269- }
270-
271- @Override
272- public String toString() {
273- return String
274- .format("Question [id=%s, description=%s, correctAnswer=%s, options=%s]",
275- id, description, correctAnswer, options);
276- }
277-
278- @Override
279- public int hashCode() {
280- final int prime = 31;
281- int result = 1;
282- result = prime * result + (id == null ? 0 : id.hashCode());
283- return result;
284- }
285-
286- @Override
287- public boolean equals(Object obj) {
288- if (this == obj) {
289- return true;
290- }
291- if (obj == null) {
292- return false;
293- }
294- if (getClass() != obj.getClass()) {
295- return false;
296- }
297- Question other = (Question) obj;
298- if (id == null) {
299- if (other.id != null) {
300- return false;
301- }
302- } else if (!id.equals(other.id)) {
303- return false;
304- }
305- return true;
306- }
307-
308- }
309- ```
310- ### /src/main/java/com/in28minutes/springboot/model/Survey.java
311- ```
312- package com.in28minutes.springboot.model;
313-
314- import java.util.List;
315-
316- public class Survey {
317- private String id;
318- private String title;
319- private String description;
320- private List<Question> questions;
321-
322- public Survey(String id, String title, String description,
323- List<Question> questions) {
324- super();
325- this.id = id;
326- this.title = title;
327- this.description = description;
328- this.questions = questions;
329- }
330-
331- public String getId() {
332- return id;
333- }
334-
335- public String getTitle() {
336- return title;
337- }
338-
339- public String getDescription() {
340- return description;
341- }
342-
343- public List<Question> getQuestions() {
344- return questions;
345- }
346-
347- @Override
348- public String toString() {
349- return String.format(
350- "Survey [id=%s, title=%s, description=%s, questions=%s]", id,
351- title, description, questions);
352- }
353-
354- }
355- ```
356- ### /src/main/java/com/in28minutes/springboot/service/SurveyService.java
357- ```
358- package com.in28minutes.springboot.service;
359-
360- import java.math.BigInteger;
361- import java.security.SecureRandom;
362- import java.util.ArrayList;
363- import java.util.Arrays;
364- import java.util.List;
365-
366- import org.springframework.stereotype.Component;
367-
368- import com.in28minutes.springboot.model.Question;
369- import com.in28minutes.springboot.model.Survey;
370-
371- @Component
372- public class SurveyService {
373- private static List<Survey> surveys = new ArrayList<>();
374- static {
375- Question question1 = new Question("Question1",
376- "Largest Country in the World", "Russia", Arrays.asList(
377- "India", "Russia", "United States", "China"));
378- Question question2 = new Question("Question2",
379- "Most Populus Country in the World", "China", Arrays.asList(
380- "India", "Russia", "United States", "China"));
381- Question question3 = new Question("Question3",
382- "Highest GDP in the World", "United States", Arrays.asList(
383- "India", "Russia", "United States", "China"));
384- Question question4 = new Question("Question4",
385- "Second largest english speaking country", "India",
386- Arrays.asList("India", "Russia", "United States", "China"));
387-
388- List<Question> questions = new ArrayList<>(Arrays.asList(question1,
389- question2, question3, question4));
390-
391- Survey survey = new Survey("Survey1", "My Favorite Survey",
392- "Description of the Survey", questions);
393-
394- surveys.add(survey);
395- }
396-
397- public List<Survey> retrieveAllSurveys() {
398- return surveys;
399- }
400-
401- public Survey retrieveSurvey(String surveyId) {
402- for (Survey survey : surveys) {
403- if (survey.getId().equals(surveyId)) {
404- return survey;
405- }
406- }
407- return null;
408- }
409-
410- public List<Question> retrieveQuestions(String surveyId) {
411- Survey survey = retrieveSurvey(surveyId);
412-
413- if (survey == null) {
414- return null;
415- }
416-
417- return survey.getQuestions();
418- }
419-
420- public Question retrieveQuestion(String surveyId, String questionId) {
421- Survey survey = retrieveSurvey(surveyId);
422-
423- if (survey == null) {
424- return null;
425- }
426-
427- for (Question question : survey.getQuestions()) {
428- if (question.getId().equals(questionId)) {
429- return question;
430- }
431- }
432-
433- return null;
434- }
435-
436- private SecureRandom random = new SecureRandom();
437-
438- public Question addQuestion(String surveyId, Question question) {
439- Survey survey = retrieveSurvey(surveyId);
440-
441- if (survey == null) {
442- return null;
443- }
444-
445- String randomId = new BigInteger(130, random).toString(32);
446- question.setId(randomId);
447-
448- survey.getQuestions().add(question);
449-
450- return question;
451- }
452- }
453- ```
454- ### /src/main/resources/application.properties
455- ```
456- logging.level.org.springframework: DEBUG
457- ```
0 commit comments