├── .elasticbeanstalk
└── config.yml
├── .gitignore
├── Otherprojects
├── 01-spring-boot-hello-world-rest-api
│ ├── pom.xml
│ ├── readme.md
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── in28minutes
│ │ │ │ └── rest
│ │ │ │ └── webservices
│ │ │ │ └── restfulwebservices
│ │ │ │ ├── HelloWorldBean.java
│ │ │ │ ├── HelloWorldController.java
│ │ │ │ └── RestfulWebServicesApplication.java
│ │ └── resources
│ │ │ └── application.properties
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── in28minutes
│ │ └── rest
│ │ └── webservices
│ │ └── restfulwebservices
│ │ └── RestfulWebServicesApplicationTests.java
├── 02-spring-boot-todo-rest-api-h2
│ ├── pom.xml
│ ├── readme.md
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── in28minutes
│ │ │ │ └── rest
│ │ │ │ └── webservices
│ │ │ │ └── restfulwebservices
│ │ │ │ ├── RestfulWebServicesApplication.java
│ │ │ │ ├── helloworld
│ │ │ │ ├── HelloWorldBean.java
│ │ │ │ └── HelloWorldController.java
│ │ │ │ └── todo
│ │ │ │ ├── Todo.java
│ │ │ │ ├── TodoHardcodedService.java
│ │ │ │ ├── TodoJpaRepository.java
│ │ │ │ ├── TodoJpaResource.java
│ │ │ │ └── TodoResource.java
│ │ └── resources
│ │ │ ├── application.properties
│ │ │ └── data.sql
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── in28minutes
│ │ └── rest
│ │ └── webservices
│ │ └── restfulwebservices
│ │ └── RestfulWebServicesApplicationTests.java
├── 03-spring-boot-web-application-h2
│ ├── pom.xml
│ ├── readme.md
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── in28minutes
│ │ │ │ └── springboot
│ │ │ │ └── web
│ │ │ │ ├── PropertyLogger.java
│ │ │ │ ├── SpringBootFirstWebApplication.java
│ │ │ │ ├── controller
│ │ │ │ ├── ErrorController.java
│ │ │ │ ├── LogoutController.java
│ │ │ │ ├── TodoController.java
│ │ │ │ └── WelcomeController.java
│ │ │ │ ├── model
│ │ │ │ └── Todo.java
│ │ │ │ ├── security
│ │ │ │ └── SecurityConfiguration.java
│ │ │ │ └── service
│ │ │ │ ├── TodoRepository.java
│ │ │ │ └── TodoService.java
│ │ ├── resources
│ │ │ ├── application.properties
│ │ │ └── data.sql
│ │ └── webapp
│ │ │ └── WEB-INF
│ │ │ └── jsp
│ │ │ ├── common
│ │ │ ├── footer.jspf
│ │ │ ├── header.jspf
│ │ │ └── navigation.jspf
│ │ │ ├── error.jsp
│ │ │ ├── list-todos.jsp
│ │ │ ├── todo.jsp
│ │ │ └── welcome.jsp
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── in28minutes
│ │ └── springboot
│ │ └── web
│ │ └── SpringBootFirstWebApplicationTests.java
├── 04-spring-boot-web-application-mysql
│ ├── pom.xml
│ ├── readme.md
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── in28minutes
│ │ │ │ └── springboot
│ │ │ │ └── web
│ │ │ │ ├── PropertyLogger.java
│ │ │ │ ├── SpringBootFirstWebApplication.java
│ │ │ │ ├── controller
│ │ │ │ ├── ErrorController.java
│ │ │ │ ├── LogoutController.java
│ │ │ │ ├── TodoController.java
│ │ │ │ └── WelcomeController.java
│ │ │ │ ├── model
│ │ │ │ └── Todo.java
│ │ │ │ ├── security
│ │ │ │ └── SecurityConfiguration.java
│ │ │ │ └── service
│ │ │ │ ├── TodoRepository.java
│ │ │ │ └── TodoService.java
│ │ ├── resources
│ │ │ └── application.properties
│ │ └── webapp
│ │ │ └── WEB-INF
│ │ │ └── jsp
│ │ │ ├── common
│ │ │ ├── footer.jspf
│ │ │ ├── header.jspf
│ │ │ └── navigation.jspf
│ │ │ ├── error.jsp
│ │ │ ├── list-todos.jsp
│ │ │ ├── todo.jsp
│ │ │ └── welcome.jsp
│ │ └── test
│ │ ├── java
│ │ └── com
│ │ │ └── in28minutes
│ │ │ └── springboot
│ │ │ └── web
│ │ │ └── SpringBootFirstWebApplicationTests.java
│ │ └── resources
│ │ └── application.properties
├── 05-spring-boot-react-full-stack-h2
│ ├── frontend
│ │ └── todo-app
│ │ │ ├── build
│ │ │ ├── asset-manifest.json
│ │ │ ├── favicon.ico
│ │ │ ├── index.html
│ │ │ ├── manifest.json
│ │ │ ├── precache-manifest.329107c39cdcab0d36989b26995e59f8.js
│ │ │ ├── service-worker.js
│ │ │ └── static
│ │ │ │ ├── css
│ │ │ │ ├── main.8537e868.chunk.css
│ │ │ │ └── main.8537e868.chunk.css.map
│ │ │ │ └── js
│ │ │ │ ├── 2.3f64e426.chunk.js
│ │ │ │ ├── 2.3f64e426.chunk.js.map
│ │ │ │ ├── main.2304f8a7.chunk.js
│ │ │ │ ├── main.2304f8a7.chunk.js.map
│ │ │ │ ├── runtime~main.c5541365.js
│ │ │ │ └── runtime~main.c5541365.js.map
│ │ │ ├── package-lock.json
│ │ │ ├── package.json
│ │ │ ├── public
│ │ │ ├── favicon.ico
│ │ │ ├── index.html
│ │ │ └── manifest.json
│ │ │ └── src
│ │ │ ├── App.css
│ │ │ ├── App.js
│ │ │ ├── App.test.js
│ │ │ ├── Constants.js
│ │ │ ├── api
│ │ │ └── todo
│ │ │ │ ├── HelloWorldService.js
│ │ │ │ └── TodoDataService.js
│ │ │ ├── bootstrap.css
│ │ │ ├── components
│ │ │ ├── counter
│ │ │ │ ├── Counter.css
│ │ │ │ └── Counter.jsx
│ │ │ ├── learning-examples
│ │ │ │ ├── FirstComponent.jsx
│ │ │ │ ├── SecondComponent.jsx
│ │ │ │ └── ThirdComponent.jsx
│ │ │ └── todo
│ │ │ │ ├── AuthenticatedRoute.jsx
│ │ │ │ ├── AuthenticationService.js
│ │ │ │ ├── ErrorComponent.jsx
│ │ │ │ ├── FooterComponent.jsx
│ │ │ │ ├── HeaderComponent.jsx
│ │ │ │ ├── ListTodosComponent.jsx
│ │ │ │ ├── LoginComponent.jsx
│ │ │ │ ├── LogoutComponent.jsx
│ │ │ │ ├── TodoApp.jsx
│ │ │ │ ├── TodoComponent.jsx
│ │ │ │ └── WelcomeComponent.jsx
│ │ │ ├── index.css
│ │ │ ├── index.js
│ │ │ ├── logo.svg
│ │ │ └── serviceWorker.js
│ ├── react_00_architecture.png
│ ├── readme.md
│ └── restful-web-services
│ │ ├── pom.xml
│ │ ├── readme.md
│ │ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── in28minutes
│ │ │ │ └── rest
│ │ │ │ ├── basic
│ │ │ │ └── auth
│ │ │ │ │ ├── AuthenticationBean.java
│ │ │ │ │ ├── BasicAuthenticationController.java
│ │ │ │ │ └── SpringSecurityConfigurationBasicAuth.java
│ │ │ │ └── webservices
│ │ │ │ └── restfulwebservices
│ │ │ │ ├── RestfulWebServicesApplication.java
│ │ │ │ ├── helloworld
│ │ │ │ ├── HelloWorldBean.java
│ │ │ │ └── HelloWorldController.java
│ │ │ │ ├── jwt
│ │ │ │ ├── JWTWebSecurityConfig.java
│ │ │ │ ├── JwtInMemoryUserDetailsService.java
│ │ │ │ ├── JwtTokenAuthorizationOncePerRequestFilter.java
│ │ │ │ ├── JwtTokenUtil.java
│ │ │ │ ├── JwtUnAuthorizedResponseAuthenticationEntryPoint.java
│ │ │ │ ├── JwtUserDetails.java
│ │ │ │ ├── JwtUserDetailsService.java
│ │ │ │ ├── User.java
│ │ │ │ ├── UserRepository.java
│ │ │ │ └── resource
│ │ │ │ │ ├── AuthenticationException.java
│ │ │ │ │ ├── JwtAuthenticationRestController.java
│ │ │ │ │ ├── JwtTokenRequest.java
│ │ │ │ │ └── JwtTokenResponse.java
│ │ │ │ └── todo
│ │ │ │ ├── Todo.java
│ │ │ │ ├── TodoHardcodedService.java
│ │ │ │ ├── TodoJpaRepository.java
│ │ │ │ ├── TodoJpaResource.java
│ │ │ │ └── TodoResource.java
│ │ └── resources
│ │ │ ├── application.properties
│ │ │ └── data.sql
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── in28minutes
│ │ └── rest
│ │ └── webservices
│ │ └── restfulwebservices
│ │ └── RestfulWebServicesApplicationTests.java
├── 06-todo-rest-api-h2-containerized
│ ├── Dockerfile
│ ├── Dockerrun.aws.json
│ ├── pom.xml
│ ├── readme.md
│ └── src
│ │ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── in28minutes
│ │ │ │ └── rest
│ │ │ │ └── webservices
│ │ │ │ └── restfulwebservices
│ │ │ │ ├── RestfulWebServicesApplication.java
│ │ │ │ ├── helloworld
│ │ │ │ ├── HelloWorldBean.java
│ │ │ │ └── HelloWorldController.java
│ │ │ │ └── todo
│ │ │ │ ├── Todo.java
│ │ │ │ ├── TodoHardcodedService.java
│ │ │ │ └── TodoResource.java
│ │ └── resources
│ │ │ └── application.properties
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── in28minutes
│ │ └── rest
│ │ └── webservices
│ │ └── restfulwebservices
│ │ └── RestfulWebServicesApplicationTests.java
└── 07-todo-rest-api-mysql-containerized
│ ├── Dockerfile
│ ├── Dockerrun.aws.json
│ ├── pom.xml
│ ├── readme.md
│ └── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── in28minutes
│ │ │ └── rest
│ │ │ └── webservices
│ │ │ └── restfulwebservices
│ │ │ ├── RestfulWebServicesApplication.java
│ │ │ ├── helloworld
│ │ │ ├── HelloWorldBean.java
│ │ │ └── HelloWorldController.java
│ │ │ └── todo
│ │ │ ├── Todo.java
│ │ │ ├── TodoHardcodedService.java
│ │ │ └── TodoResource.java
│ └── resources
│ │ └── application.properties
│ └── test
│ ├── java
│ └── com
│ │ └── in28minutes
│ │ └── rest
│ │ └── webservices
│ │ └── restfulwebservices
│ │ └── RestfulWebServicesApplicationTests.java
│ └── resources
│ └── application.properties
├── buildspec.yml
├── cmd-line-test
└── .gitignore
├── pom.xml
├── readme.md
└── src
├── main
├── java
│ └── com
│ │ └── in28minutes
│ │ └── rest
│ │ └── webservices
│ │ └── restfulwebservices
│ │ ├── RestfulWebServicesApplication.java
│ │ ├── helloworld
│ │ ├── HelloWorldBean.java
│ │ └── HelloWorldController.java
│ │ └── todo
│ │ ├── Todo.java
│ │ ├── TodoHardcodedService.java
│ │ ├── TodoJpaRepository.java
│ │ ├── TodoJpaResource.java
│ │ └── TodoResource.java
└── resources
│ ├── application.properties
│ └── data.sql
└── test
└── java
└── com
└── in28minutes
└── rest
└── webservices
└── restfulwebservices
└── RestfulWebServicesApplicationTests.java
/.elasticbeanstalk/config.yml:
--------------------------------------------------------------------------------
1 | global:
2 | profile: eb-cli
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # compiled output
2 | /dist
3 | /tmp
4 | /out-tsc
5 |
6 | # dependencies
7 | /node_modules
8 |
9 | # IDEs and editors
10 | /.idea
11 | .project
12 | .classpath
13 | .c9/
14 | *.launch
15 | .settings/
16 | *.sublime-workspace
17 |
18 | # IDE - VSCode
19 | .vscode/*
20 | !.vscode/settings.json
21 | !.vscode/tasks.json
22 | !.vscode/launch.json
23 | !.vscode/extensions.json
24 |
25 | # misc
26 | /.sass-cache
27 | /connect.lock
28 | /coverage
29 | /libpeerconnection.log
30 | npm-debug.log
31 | yarn-error.log
32 | testem.log
33 | /typings
34 |
35 | # System Files
36 | .DS_Store
37 | Thumbs.db
38 |
39 | # Compiled class file
40 | *.class
41 |
42 | # Log file
43 | *.log
44 |
45 | # BlueJ files
46 | *.ctxt
47 |
48 | # Mobile Tools for Java (J2ME)
49 | .mtj.tmp/
50 |
51 | # Package Files #
52 | *.jar
53 | *.war
54 | *.ear
55 | *.tar.gz
56 | *.rar
57 | *.cmd
58 | *.classpath
59 | *.settings
60 | *.project
61 | *.mvn
62 | mvnw
63 | target
64 | *.DS_Store
65 |
66 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
67 | hs_err_pid*
68 |
69 | # Compiled class file
70 | *.class
71 |
72 | # Log file
73 | *.log
74 |
75 | # BlueJ files
76 | *.ctxt
77 |
78 | # Mobile Tools for Java (J2ME)
79 | .mtj.tmp/
80 |
81 | # Package Files #
82 | *.jar
83 | *.war
84 | *.ear
85 | *.zip
86 | *.tar.gz
87 | *.rar
88 | *.cmd
89 | *.classpath
90 | *.settings
91 | *.project
92 | *.mvn
93 | mvnw
94 | target
95 | node_modules
96 | *.DS_Store
97 |
98 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
99 | hs_err_pid*
100 | bin
--------------------------------------------------------------------------------
/Otherprojects/01-spring-boot-hello-world-rest-api/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.in28minutes.rest.webservices
8 | 01-spring-boot-hello-world-rest-api
9 | 0.0.1-SNAPSHOT
10 | jar
11 | Demo project for Spring Boot
12 |
13 |
14 | org.springframework.boot
15 | spring-boot-starter-parent
16 | 2.1.0.RELEASE
17 |
18 |
19 |
20 |
21 | UTF-8
22 | UTF-8
23 | 1.8
24 |
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-data-jpa
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-web
36 |
37 |
38 |
39 | org.springframework.boot
40 | spring-boot-devtools
41 | runtime
42 |
43 |
44 |
45 | com.h2database
46 | h2
47 | runtime
48 |
49 |
50 |
51 | javax.xml.bind
52 | jaxb-api
53 | 2.3.0
54 |
55 |
56 | com.sun.xml.bind
57 | jaxb-impl
58 | 2.3.0
59 |
60 |
61 | org.glassfish.jaxb
62 | jaxb-runtime
63 | 2.3.0
64 |
65 |
66 | javax.activation
67 | activation
68 | 1.1.1
69 |
70 |
71 |
72 | org.springframework.boot
73 | spring-boot-starter-test
74 | test
75 |
76 |
77 |
78 |
79 |
80 |
81 | org.springframework.boot
82 | spring-boot-maven-plugin
83 |
84 |
85 |
86 |
87 |
88 |
89 | spring-snapshots
90 | Spring Snapshots
91 | https://repo.spring.io/snapshot
92 |
93 | true
94 |
95 |
96 |
97 | spring-milestones
98 | Spring Milestones
99 | https://repo.spring.io/milestone
100 |
101 | false
102 |
103 |
104 |
105 |
106 |
107 |
108 | spring-snapshots
109 | Spring Snapshots
110 | https://repo.spring.io/snapshot
111 |
112 | true
113 |
114 |
115 |
116 | spring-milestones
117 | Spring Milestones
118 | https://repo.spring.io/milestone
119 |
120 | false
121 |
122 |
123 |
124 |
125 |
126 |
127 |
--------------------------------------------------------------------------------
/Otherprojects/01-spring-boot-hello-world-rest-api/readme.md:
--------------------------------------------------------------------------------
1 | # Hello World Rest API running on port 5000
2 |
3 | Run com.in28minutes.rest.webservices.restfulwebservices.RestfulWebServicesApplication as a Java Application.
4 |
5 | - http://localhost:5000/hello-world
6 |
7 | ```txt
8 | Hello World
9 | ```
10 |
11 | - http://localhost:5000/hello-world-bean
12 |
13 | ```json
14 | {"message":"Hello World - Changed"}
15 | ```
16 |
17 | - http://localhost:5000/hello-world/path-variable/in28minutes
18 |
19 | ```json
20 | {"message":"Hello World, in28minutes"}
21 | ```
22 |
23 |
--------------------------------------------------------------------------------
/Otherprojects/01-spring-boot-hello-world-rest-api/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/HelloWorldBean.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices;
2 |
3 | public class HelloWorldBean {
4 |
5 | private String message;
6 |
7 | public HelloWorldBean(String message) {
8 | this.message = message;
9 | }
10 |
11 | public String getMessage() {
12 | return message;
13 | }
14 |
15 | public void setMessage(String message) {
16 | this.message = message;
17 | }
18 |
19 | @Override
20 | public String toString() {
21 | return String.format("HelloWorldBean [message=%s]", message);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Otherprojects/01-spring-boot-hello-world-rest-api/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/HelloWorldController.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices;
2 |
3 | import org.springframework.web.bind.annotation.GetMapping;
4 | import org.springframework.web.bind.annotation.PathVariable;
5 | import org.springframework.web.bind.annotation.RestController;
6 |
7 | //Controller
8 | //AWS @CrossOrigin(origins="http://localhost:4200")
9 | @RestController
10 | public class HelloWorldController {
11 |
12 | @GetMapping(path = "/hello-world")
13 | public String helloWorld() {
14 | return "Hello World";
15 | }
16 |
17 | @GetMapping(path = "/hello-world-bean")
18 | public HelloWorldBean helloWorldBean() {
19 | //throw new RuntimeException("Some Error has Happened! Contact Support at ***-***");
20 | return new HelloWorldBean("Hello World - Changed");
21 | }
22 |
23 | ///hello-world/path-variable/in28minutes
24 | @GetMapping(path = "/hello-world/path-variable/{name}")
25 | public HelloWorldBean helloWorldPathVariable(@PathVariable String name) {
26 | return new HelloWorldBean(String.format("Hello World, %s", name));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Otherprojects/01-spring-boot-hello-world-rest-api/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/RestfulWebServicesApplication.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class RestfulWebServicesApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(RestfulWebServicesApplication.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Otherprojects/01-spring-boot-hello-world-rest-api/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | logging.level.org.springframework = debug
2 |
3 | #AWS Elastic Beanstalk assumes that the application will listen on port 5000.
4 | server.port=5000
--------------------------------------------------------------------------------
/Otherprojects/01-spring-boot-hello-world-rest-api/src/test/java/com/in28minutes/rest/webservices/restfulwebservices/RestfulWebServicesApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class RestfulWebServicesApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.in28minutes.rest.webservices
7 | 02-todo-rest-api-h2
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | Demo project for Spring Boot
12 |
13 |
14 | org.springframework.boot
15 | spring-boot-starter-parent
16 | 2.1.0.RELEASE
17 |
18 |
19 |
20 |
21 | UTF-8
22 | UTF-8
23 | 1.8
24 |
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-data-jpa
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-web
36 |
37 |
38 |
39 | org.springframework.boot
40 | spring-boot-devtools
41 | runtime
42 |
43 |
44 |
45 | com.h2database
46 | h2
47 | runtime
48 |
49 |
50 |
51 | org.springframework.boot
52 | spring-boot-starter-test
53 | test
54 |
55 |
56 |
57 |
58 |
59 |
60 | org.springframework.boot
61 | spring-boot-maven-plugin
62 |
63 |
64 |
65 |
66 |
67 |
68 | spring-snapshots
69 | Spring Snapshots
70 | https://repo.spring.io/snapshot
71 |
72 | true
73 |
74 |
75 |
76 | spring-milestones
77 | Spring Milestones
78 | https://repo.spring.io/milestone
79 |
80 | false
81 |
82 |
83 |
84 |
85 |
86 |
87 | spring-snapshots
88 | Spring Snapshots
89 | https://repo.spring.io/snapshot
90 |
91 | true
92 |
93 |
94 |
95 | spring-milestones
96 | Spring Milestones
97 | https://repo.spring.io/milestone
98 |
99 | false
100 |
101 |
102 |
103 |
104 |
105 |
106 |
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/RestfulWebServicesApplication.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.web.servlet.config.annotation.CorsRegistry;
7 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
8 |
9 | @SpringBootApplication
10 | public class RestfulWebServicesApplication {
11 |
12 | // AWS
13 | @Bean
14 | public WebMvcConfigurer corsConfigurer() {
15 | return new WebMvcConfigurer() {
16 | @Override
17 | public void addCorsMappings(CorsRegistry registry) {
18 | registry.addMapping("/**").allowedMethods("*").allowedOrigins("*");
19 | }
20 | };
21 | }
22 |
23 | public static void main(String[] args) {
24 | SpringApplication.run(RestfulWebServicesApplication.class, args);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/helloworld/HelloWorldBean.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices.helloworld;
2 |
3 | public class HelloWorldBean {
4 |
5 | private String message;
6 |
7 | public HelloWorldBean(String message) {
8 | this.message = message;
9 | }
10 |
11 | public String getMessage() {
12 | return message;
13 | }
14 |
15 | public void setMessage(String message) {
16 | this.message = message;
17 | }
18 |
19 | @Override
20 | public String toString() {
21 | return String.format("HelloWorldBean [message=%s]", message);
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/helloworld/HelloWorldController.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices.helloworld;
2 |
3 | import org.springframework.web.bind.annotation.CrossOrigin;
4 | import org.springframework.web.bind.annotation.GetMapping;
5 | import org.springframework.web.bind.annotation.PathVariable;
6 | import org.springframework.web.bind.annotation.RestController;
7 |
8 | //Controller
9 | @RestController
10 | @CrossOrigin(origins="http://localhost:4200")
11 | //
12 | public class HelloWorldController {
13 |
14 | @GetMapping(path = "/hello-world")
15 | public String helloWorld() {
16 | return "Hello World";
17 | }
18 |
19 | @GetMapping(path = "/hello-world-bean")
20 | public HelloWorldBean helloWorldBean() {
21 | return new HelloWorldBean("Hello World");
22 | }
23 |
24 | ///hello-world/path-variable/in28minutes
25 | @GetMapping(path = "/hello-world/path-variable/{name}")
26 | public HelloWorldBean helloWorldPathVariable(@PathVariable String name) {
27 | //throw new RuntimeException("Something went wrong");
28 | return new HelloWorldBean(String.format("Hello World, %s", name));
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/todo/Todo.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices.todo;
2 |
3 | import java.util.Date;
4 |
5 | import javax.persistence.Entity;
6 | import javax.persistence.GeneratedValue;
7 | import javax.persistence.Id;
8 |
9 | @Entity
10 | public class Todo {
11 | @Id
12 | @GeneratedValue
13 | private Long id;
14 | private String username;
15 | private String description;
16 | private Date targetDate;
17 | private boolean isDone;
18 |
19 | public Todo() {
20 |
21 | }
22 |
23 | public Todo(long id, String username, String description, Date targetDate, boolean isDone) {
24 | super();
25 | this.id = id;
26 | this.username = username;
27 | this.description = description;
28 | this.targetDate = targetDate;
29 | this.isDone = isDone;
30 | }
31 |
32 | public Long getId() {
33 | return id;
34 | }
35 |
36 | public void setId(Long id) {
37 | this.id = id;
38 | }
39 |
40 | public String getUsername() {
41 | return username;
42 | }
43 |
44 | public void setUsername(String username) {
45 | this.username = username;
46 | }
47 |
48 | public String getDescription() {
49 | return description;
50 | }
51 |
52 | public void setDescription(String description) {
53 | this.description = description;
54 | }
55 |
56 | public Date getTargetDate() {
57 | return targetDate;
58 | }
59 |
60 | public void setTargetDate(Date targetDate) {
61 | this.targetDate = targetDate;
62 | }
63 |
64 | public boolean isDone() {
65 | return isDone;
66 | }
67 |
68 | public void setDone(boolean isDone) {
69 | this.isDone = isDone;
70 | }
71 |
72 | @Override
73 | public int hashCode() {
74 | final int prime = 31;
75 | int result = 1;
76 | result = prime * result + (int) (id ^ (id >>> 32));
77 | return result;
78 | }
79 |
80 | @Override
81 | public boolean equals(Object obj) {
82 | if (this == obj)
83 | return true;
84 | if (obj == null)
85 | return false;
86 | if (getClass() != obj.getClass())
87 | return false;
88 | Todo other = (Todo) obj;
89 | if (id != other.id)
90 | return false;
91 | return true;
92 | }
93 |
94 |
95 | }
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/todo/TodoHardcodedService.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices.todo;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Date;
5 | import java.util.List;
6 |
7 | import org.springframework.stereotype.Service;
8 |
9 | @Service
10 | public class TodoHardcodedService {
11 |
12 | private static List todos = new ArrayList<>();
13 | private static long idCounter = 0;
14 |
15 | static {
16 | todos.add(new Todo(++idCounter, "in28minutes", "Learn to Dance 2", new Date(), false));
17 | todos.add(new Todo(++idCounter, "in28minutes", "Learn about Microservices 2", new Date(), false));
18 | todos.add(new Todo(++idCounter, "in28minutes", "Learn about Angular", new Date(), false));
19 | }
20 |
21 | public List findAll() {
22 | return todos;
23 | }
24 |
25 | public Todo save(Todo todo) {
26 | if(todo.getId()==-1 || todo.getId()==0) {
27 | todo.setId(++idCounter);
28 | todos.add(todo);
29 | } else {
30 | deleteById(todo.getId());
31 | todos.add(todo);
32 | }
33 | return todo;
34 | }
35 |
36 | public Todo deleteById(long id) {
37 | Todo todo = findById(id);
38 |
39 | if (todo == null)
40 | return null;
41 |
42 | if (todos.remove(todo)) {
43 | return todo;
44 | }
45 |
46 | return null;
47 | }
48 |
49 | public Todo findById(long id) {
50 | for (Todo todo : todos) {
51 | if (todo.getId() == id) {
52 | return todo;
53 | }
54 | }
55 |
56 | return null;
57 | }
58 | }
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/todo/TodoJpaRepository.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices.todo;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.data.jpa.repository.JpaRepository;
6 | import org.springframework.stereotype.Repository;
7 |
8 | @Repository
9 | public interface TodoJpaRepository extends JpaRepository{
10 | List findByUsername(String username);
11 | }
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/todo/TodoJpaResource.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices.todo;
2 |
3 | import java.net.URI;
4 | import java.util.List;
5 |
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.http.HttpStatus;
8 | import org.springframework.http.ResponseEntity;
9 | import org.springframework.web.bind.annotation.DeleteMapping;
10 | import org.springframework.web.bind.annotation.GetMapping;
11 | import org.springframework.web.bind.annotation.PathVariable;
12 | import org.springframework.web.bind.annotation.PostMapping;
13 | import org.springframework.web.bind.annotation.PutMapping;
14 | import org.springframework.web.bind.annotation.RequestBody;
15 | import org.springframework.web.bind.annotation.RestController;
16 | import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
17 |
18 | //@CrossOrigin(origins="http://localhost:4200") Replace with global config
19 | @RestController
20 | public class TodoJpaResource {
21 |
22 | @Autowired
23 | private TodoJpaRepository todoJpaRepository;
24 |
25 |
26 | @GetMapping("/jpa/users/{username}/todos")
27 | public List getAllTodos(@PathVariable String username){
28 | return todoJpaRepository.findByUsername(username);
29 | //return todoService.findAll();
30 | }
31 |
32 | @GetMapping("/jpa/users/{username}/todos/{id}")
33 | public Todo getTodo(@PathVariable String username, @PathVariable long id){
34 | return todoJpaRepository.findById(id).get();
35 | //return todoService.findById(id);
36 | }
37 |
38 | // DELETE /users/{username}/todos/{id}
39 | @DeleteMapping("/jpa/users/{username}/todos/{id}")
40 | public ResponseEntity deleteTodo(
41 | @PathVariable String username, @PathVariable long id) {
42 |
43 | todoJpaRepository.deleteById(id);
44 |
45 | return ResponseEntity.noContent().build();
46 | }
47 |
48 |
49 | //Edit/Update a Todo
50 | //PUT /users/{user_name}/todos/{todo_id}
51 | @PutMapping("/jpa/users/{username}/todos/{id}")
52 | public ResponseEntity updateTodo(
53 | @PathVariable String username,
54 | @PathVariable long id, @RequestBody Todo todo){
55 |
56 | todo.setUsername(username);
57 |
58 | Todo todoUpdated = todoJpaRepository.save(todo);
59 |
60 | return new ResponseEntity(todo, HttpStatus.OK);
61 | }
62 |
63 | @PostMapping("/jpa/users/{username}/todos")
64 | public ResponseEntity createTodo(
65 | @PathVariable String username, @RequestBody Todo todo){
66 |
67 | todo.setUsername(username);
68 |
69 | Todo createdTodo = todoJpaRepository.save(todo);
70 |
71 | //Location
72 | //Get current resource url
73 | ///{id}
74 | URI uri = ServletUriComponentsBuilder.fromCurrentRequest()
75 | .path("/{id}").buildAndExpand(createdTodo.getId()).toUri();
76 |
77 | return ResponseEntity.created(uri).build();
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/src/main/java/com/in28minutes/rest/webservices/restfulwebservices/todo/TodoResource.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices.todo;
2 |
3 | import java.net.URI;
4 | import java.util.List;
5 |
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.http.HttpStatus;
8 | import org.springframework.http.ResponseEntity;
9 | import org.springframework.web.bind.annotation.DeleteMapping;
10 | import org.springframework.web.bind.annotation.GetMapping;
11 | import org.springframework.web.bind.annotation.PathVariable;
12 | import org.springframework.web.bind.annotation.PostMapping;
13 | import org.springframework.web.bind.annotation.PutMapping;
14 | import org.springframework.web.bind.annotation.RequestBody;
15 | import org.springframework.web.bind.annotation.RestController;
16 | import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
17 |
18 | //@CrossOrigin(origins = "http://localhost:4200") Replace with global config
19 | @RestController
20 | public class TodoResource {
21 |
22 | @Autowired
23 | private TodoHardcodedService todoService;
24 |
25 | @GetMapping("/users/{username}/todos")
26 | public List getAllTodos(@PathVariable String username) {
27 | // Thread.sleep(3000);
28 | return todoService.findAll();
29 | }
30 |
31 | @GetMapping("/users/{username}/todos/{id}")
32 | public Todo getTodo(@PathVariable String username, @PathVariable long id) {
33 | // Thread.sleep(3000);
34 | return todoService.findById(id);
35 | }
36 |
37 |
38 | // DELETE /users/{username}/todos/{id}
39 | @DeleteMapping("/users/{username}/todos/{id}")
40 | public ResponseEntity deleteTodo(@PathVariable String username, @PathVariable long id) {
41 |
42 | Todo todo = todoService.deleteById(id);
43 |
44 | if (todo != null) {
45 | return ResponseEntity.noContent().build();
46 | }
47 |
48 | return ResponseEntity.notFound().build();
49 | }
50 |
51 | //Edit/Update a Todo
52 | //PUT /users/{user_name}/todos/{todo_id}
53 | @PutMapping("/users/{username}/todos/{id}")
54 | public ResponseEntity updateTodo(
55 | @PathVariable String username,
56 | @PathVariable long id, @RequestBody Todo todo){
57 |
58 | Todo todoUpdated = todoService.save(todo);
59 |
60 | return new ResponseEntity(todo, HttpStatus.OK);
61 | }
62 |
63 | @PostMapping("/users/{username}/todos")
64 | public ResponseEntity updateTodo(
65 | @PathVariable String username, @RequestBody Todo todo){
66 |
67 | Todo createdTodo = todoService.save(todo);
68 |
69 | //Location
70 | //Get current resource url
71 | ///{id}
72 | URI uri = ServletUriComponentsBuilder.fromCurrentRequest()
73 | .path("/{id}").buildAndExpand(createdTodo.getId()).toUri();
74 |
75 | return ResponseEntity.created(uri).build();
76 | }
77 |
78 | }
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.jpa.show-sql=true
2 | spring.h2.console.enabled=true
3 |
4 | logging.level.org.springframework = info
5 | server.port=5000
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/src/main/resources/data.sql:
--------------------------------------------------------------------------------
1 | insert into todo(id, username,description,target_date,is_done)
2 | values(10001, 'in28minutes', 'Learn JPA', sysdate(), false);
3 |
4 | insert into todo(id, username,description,target_date,is_done)
5 | values(10002, 'in28minutes', 'Learn Data JPA', sysdate(), false);
6 |
7 | insert into todo(id, username,description,target_date,is_done)
8 | values(10003, 'in28minutes', 'Learn Microservices', sysdate(), false);
--------------------------------------------------------------------------------
/Otherprojects/02-spring-boot-todo-rest-api-h2/src/test/java/com/in28minutes/rest/webservices/restfulwebservices/RestfulWebServicesApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.rest.webservices.restfulwebservices;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class RestfulWebServicesApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.in28minutes.springboot.web
8 | 03-spring-boot-web-application-h2
9 | 0.0.1-SNAPSHOT
10 | war
11 |
12 | Demo project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.1.0.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-web
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-data-jpa
36 |
37 |
38 |
39 | com.h2database
40 | h2
41 | runtime
42 |
43 |
44 |
45 | org.springframework.boot
46 | spring-boot-starter-security
47 |
48 |
49 |
50 | javax.servlet
51 | jstl
52 |
53 |
54 |
55 | org.webjars
56 | bootstrap
57 | 3.3.6
58 |
59 |
60 |
61 | org.webjars
62 | bootstrap-datepicker
63 | 1.0.1
64 |
65 |
66 |
67 | org.webjars
68 | jquery
69 | 1.9.1
70 |
71 |
72 |
73 | org.apache.tomcat.embed
74 | tomcat-embed-jasper
75 | provided
76 |
77 |
78 |
79 |
80 | org.springframework.boot
81 | spring-boot-starter-tomcat
82 | provided
83 |
84 |
85 |
86 | org.springframework.boot
87 | spring-boot-devtools
88 | runtime
89 |
90 |
91 |
92 | org.springframework.boot
93 | spring-boot-starter-test
94 | test
95 |
96 |
97 |
98 |
99 |
100 |
101 | org.springframework.boot
102 | spring-boot-maven-plugin
103 |
104 |
105 |
106 |
107 |
108 |
109 | spring-milestones
110 | Spring Milestones
111 | https://repo.spring.io/milestones
112 |
113 |
114 |
115 |
116 |
117 | spring-milestones
118 | Spring Milestones
119 | https://repo.spring.io/milestones
120 |
121 |
122 |
123 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/readme.md:
--------------------------------------------------------------------------------
1 | # Todo Web Application using Spring Boot and H2 In memory database
2 |
3 | Run com.in28minutes.springboot.web.SpringBootFirstWebApplication as a Java Application.
4 |
5 | Runs on default port of Spring Boot - 8080
6 |
7 | ## Can be run as a Jar or a WAR
8 |
9 | `mvn clean install` generate a war which can deployed to your favorite web server.
10 |
11 | We will deploy to Cloud as a WAR
12 |
13 | ## Web Application
14 |
15 | - http://localhost:8080/login with in28minutes/dummy as credentials
16 | - You can add, delete and update your todos
17 | - Spring Security is used to secure the application
18 | - `com.in28minutes.springboot.web.security.SecurityConfiguration` contains the in memory security credential configuration.
19 |
20 | ## H2 Console
21 |
22 | - http://localhost:8080/h2-console
23 | - Use `jdbc:h2:mem:testdb` as JDBC URL
24 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/PropertyLogger.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.springboot.web;
2 |
3 | import java.util.Arrays;
4 | import java.util.stream.StreamSupport;
5 |
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.context.event.ContextRefreshedEvent;
9 | import org.springframework.context.event.EventListener;
10 | import org.springframework.core.env.AbstractEnvironment;
11 | import org.springframework.core.env.EnumerablePropertySource;
12 | import org.springframework.core.env.Environment;
13 | import org.springframework.core.env.MutablePropertySources;
14 | import org.springframework.stereotype.Component;
15 |
16 | @Component
17 | public class PropertyLogger {
18 |
19 | private static final Logger LOGGER = LoggerFactory.getLogger(PropertyLogger.class);
20 |
21 | @EventListener
22 | public void handleContextRefresh(ContextRefreshedEvent event) {
23 | final Environment env = event.getApplicationContext().getEnvironment();
24 | LOGGER.info("====== Environment and configuration ======");
25 | LOGGER.info("Active profiles: {}", Arrays.toString(env.getActiveProfiles()));
26 | final MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
27 | StreamSupport.stream(sources.spliterator(), false).filter(ps -> ps instanceof EnumerablePropertySource)
28 | .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()).flatMap(Arrays::stream).distinct()
29 | .filter(prop -> !(prop.contains("credentials") || prop.contains("password")))
30 | .forEach(prop -> LOGGER.info("{}: {}", prop, env.getProperty(prop)));
31 | LOGGER.info("===========================================");
32 | }
33 |
34 | }
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/SpringBootFirstWebApplication.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.springboot.web;
2 |
3 | import java.util.Map;
4 |
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.boot.SpringApplication;
7 | import org.springframework.boot.autoconfigure.SpringBootApplication;
8 | import org.springframework.boot.builder.SpringApplicationBuilder;
9 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
10 | import org.springframework.context.annotation.ComponentScan;
11 | import org.springframework.core.env.Environment;
12 |
13 | @SpringBootApplication
14 | @ComponentScan("com.in28minutes.springboot.web")
15 | public class SpringBootFirstWebApplication extends SpringBootServletInitializer { // AWS
16 |
17 | @Autowired
18 | private Environment env;
19 |
20 | @Override // AWS
21 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
22 | return application.sources(SpringBootFirstWebApplication.class);
23 | }
24 |
25 | public static void main(String[] args) {
26 |
27 | Map env = System.getenv();
28 |
29 | env.forEach((k, v) -> System.out.println(k + ":" + v));
30 |
31 | SpringApplication.run(SpringBootFirstWebApplication.class, args);
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/controller/ErrorController.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.springboot.web.controller;
2 |
3 | import javax.servlet.http.HttpServletRequest;
4 | import javax.servlet.http.HttpServletResponse;
5 |
6 | import org.springframework.stereotype.Controller;
7 | import org.springframework.web.bind.annotation.ExceptionHandler;
8 | import org.springframework.web.servlet.ModelAndView;
9 |
10 | @Controller("error")
11 | public class ErrorController {
12 |
13 | @ExceptionHandler(Exception.class)
14 | public ModelAndView handleException
15 | (HttpServletRequest request, Exception ex){
16 | ModelAndView mv = new ModelAndView();
17 |
18 | mv.addObject("exception", ex.getLocalizedMessage());
19 | mv.addObject("url", request.getRequestURL());
20 |
21 | mv.setViewName("error");
22 | return mv;
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/controller/LogoutController.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.springboot.web.controller;
2 |
3 | import javax.servlet.http.HttpServletRequest;
4 | import javax.servlet.http.HttpServletResponse;
5 |
6 | import org.springframework.security.core.Authentication;
7 | import org.springframework.security.core.context.SecurityContextHolder;
8 | import org.springframework.security.core.userdetails.UserDetails;
9 | import org.springframework.security.web.authentication.logout.LogoutHandler;
10 | import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
11 | import org.springframework.stereotype.Controller;
12 | import org.springframework.ui.ModelMap;
13 | import org.springframework.web.bind.annotation.RequestMapping;
14 | import org.springframework.web.bind.annotation.RequestMethod;
15 |
16 | @Controller
17 | public class LogoutController {
18 |
19 | @RequestMapping(value = "/logout", method = RequestMethod.GET)
20 | public String logout(HttpServletRequest request,
21 | HttpServletResponse response) {
22 |
23 | Authentication authentication = SecurityContextHolder.getContext()
24 | .getAuthentication();
25 |
26 | if (authentication != null) {
27 | new SecurityContextLogoutHandler().logout(request, response,
28 | authentication);
29 | }
30 |
31 | return "redirect:/";
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/controller/TodoController.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.springboot.web.controller;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 |
6 | import javax.validation.Valid;
7 |
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.beans.propertyeditors.CustomDateEditor;
10 | import org.springframework.security.core.context.SecurityContextHolder;
11 | import org.springframework.security.core.userdetails.UserDetails;
12 | import org.springframework.stereotype.Controller;
13 | import org.springframework.ui.ModelMap;
14 | import org.springframework.validation.BindingResult;
15 | import org.springframework.web.bind.WebDataBinder;
16 | import org.springframework.web.bind.annotation.InitBinder;
17 | import org.springframework.web.bind.annotation.RequestMapping;
18 | import org.springframework.web.bind.annotation.RequestMethod;
19 | import org.springframework.web.bind.annotation.RequestParam;
20 |
21 | import com.in28minutes.springboot.web.model.Todo;
22 | import com.in28minutes.springboot.web.service.TodoRepository;
23 |
24 | @Controller
25 | public class TodoController {
26 |
27 | @Autowired
28 | TodoRepository repository;
29 |
30 | @InitBinder
31 | public void initBinder(WebDataBinder binder) {
32 | // Date - dd/MM/yyyy
33 | SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
34 | binder.registerCustomEditor(Date.class, new CustomDateEditor(
35 | dateFormat, false));
36 | }
37 |
38 | @RequestMapping(value = "/list-todos", method = RequestMethod.GET)
39 | public String showTodos(ModelMap model) {
40 | String name = getLoggedInUserName(model);
41 | model.put("todos", repository.findByUser(name));
42 | //model.put("todos", service.retrieveTodos(name));
43 | return "list-todos";
44 | }
45 |
46 | private String getLoggedInUserName(ModelMap model) {
47 | Object principal = SecurityContextHolder.getContext()
48 | .getAuthentication().getPrincipal();
49 |
50 | if (principal instanceof UserDetails) {
51 | return ((UserDetails) principal).getUsername();
52 | }
53 |
54 | return principal.toString();
55 | }
56 |
57 | @RequestMapping(value = "/add-todo", method = RequestMethod.GET)
58 | public String showAddTodoPage(ModelMap model) {
59 | model.addAttribute("todo", new Todo(0, getLoggedInUserName(model),
60 | "Default Desc", new Date(), false));
61 | return "todo";
62 | }
63 |
64 | @RequestMapping(value = "/delete-todo", method = RequestMethod.GET)
65 | public String deleteTodo(@RequestParam int id) {
66 |
67 | //if(id==1)
68 | //throw new RuntimeException("Something went wrong");
69 | repository.deleteById(id);
70 | //service.deleteTodo(id);
71 | return "redirect:/list-todos";
72 | }
73 |
74 | @RequestMapping(value = "/update-todo", method = RequestMethod.GET)
75 | public String showUpdateTodoPage(@RequestParam int id, ModelMap model) {
76 | Todo todo = repository.findById(id).get();
77 | //Todo todo = service.retrieveTodo(id);
78 | model.put("todo", todo);
79 | return "todo";
80 | }
81 |
82 | @RequestMapping(value = "/update-todo", method = RequestMethod.POST)
83 | public String updateTodo(ModelMap model, @Valid Todo todo,
84 | BindingResult result) {
85 |
86 | if (result.hasErrors()) {
87 | return "todo";
88 | }
89 |
90 | todo.setUser(getLoggedInUserName(model));
91 |
92 | repository.save(todo);
93 | //service.updateTodo(todo);
94 |
95 | return "redirect:/list-todos";
96 | }
97 |
98 | @RequestMapping(value = "/add-todo", method = RequestMethod.POST)
99 | public String addTodo(ModelMap model, @Valid Todo todo, BindingResult result) {
100 |
101 | if (result.hasErrors()) {
102 | return "todo";
103 | }
104 |
105 | todo.setUser(getLoggedInUserName(model));
106 | repository.save(todo);
107 | /*service.addTodo(getLoggedInUserName(model), todo.getDesc(), todo.getTargetDate(),
108 | false);*/
109 | return "redirect:/list-todos";
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/controller/WelcomeController.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.springboot.web.controller;
2 |
3 | import org.springframework.security.core.context.SecurityContextHolder;
4 | import org.springframework.security.core.userdetails.UserDetails;
5 | import org.springframework.stereotype.Controller;
6 | import org.springframework.ui.ModelMap;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RequestMethod;
9 |
10 | @Controller
11 | public class WelcomeController {
12 |
13 | @RequestMapping(value = "/", method = RequestMethod.GET)
14 | public String showWelcomePage(ModelMap model) {
15 | model.put("name", getLoggedinUserName());
16 | return "welcome";
17 | }
18 |
19 | private String getLoggedinUserName() {
20 | Object principal = SecurityContextHolder.getContext()
21 | .getAuthentication().getPrincipal();
22 |
23 | if (principal instanceof UserDetails) {
24 | return ((UserDetails) principal).getUsername();
25 | }
26 |
27 | return principal.toString();
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/model/Todo.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.springboot.web.model;
2 |
3 | import java.util.Date;
4 |
5 | import javax.persistence.Entity;
6 | import javax.persistence.GeneratedValue;
7 | import javax.persistence.Id;
8 | import javax.validation.constraints.Size;
9 |
10 | @Entity
11 | public class Todo {
12 |
13 | @Id
14 | @GeneratedValue
15 | private int id;
16 |
17 | private String user;
18 |
19 | @Size(min=10, message="Enter at least 10 Characters...")
20 | private String desc;
21 |
22 | private Date targetDate;
23 | private boolean isDone;
24 |
25 | public Todo() {
26 | super();
27 | }
28 |
29 | public Todo(int id, String user, String desc, Date targetDate,
30 | boolean isDone) {
31 | super();
32 | this.id = id;
33 | this.user = user;
34 | this.desc = desc;
35 | this.targetDate = targetDate;
36 | this.isDone = isDone;
37 | }
38 |
39 | public int getId() {
40 | return id;
41 | }
42 |
43 | public void setId(int id) {
44 | this.id = id;
45 | }
46 |
47 | public String getUser() {
48 | return user;
49 | }
50 |
51 | public void setUser(String user) {
52 | this.user = user;
53 | }
54 |
55 | public String getDesc() {
56 | return desc;
57 | }
58 |
59 | public void setDesc(String desc) {
60 | this.desc = desc;
61 | }
62 |
63 | public Date getTargetDate() {
64 | return targetDate;
65 | }
66 |
67 | public void setTargetDate(Date targetDate) {
68 | this.targetDate = targetDate;
69 | }
70 |
71 | public boolean isDone() {
72 | return isDone;
73 | }
74 |
75 | public void setDone(boolean isDone) {
76 | this.isDone = isDone;
77 | }
78 |
79 | @Override
80 | public int hashCode() {
81 | final int prime = 31;
82 | int result = 1;
83 | result = prime * result + id;
84 | return result;
85 | }
86 |
87 | @Override
88 | public boolean equals(Object obj) {
89 | if (this == obj) {
90 | return true;
91 | }
92 | if (obj == null) {
93 | return false;
94 | }
95 | if (getClass() != obj.getClass()) {
96 | return false;
97 | }
98 | Todo other = (Todo) obj;
99 | if (id != other.id) {
100 | return false;
101 | }
102 | return true;
103 | }
104 |
105 | @Override
106 | public String toString() {
107 | return String.format(
108 | "Todo [id=%s, user=%s, desc=%s, targetDate=%s, isDone=%s]", id,
109 | user, desc, targetDate, isDone);
110 | }
111 |
112 | }
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/security/SecurityConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.springboot.web.security;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
8 |
9 | @Configuration
10 | public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
11 | //Create User - in28Minutes/dummy
12 | @Autowired
13 | public void configureGlobalSecurity(AuthenticationManagerBuilder auth)
14 | throws Exception {
15 | auth.inMemoryAuthentication().withUser("in28minutes").password("{noop}dummy")
16 | .roles("USER", "ADMIN");
17 | }
18 |
19 | @Override
20 | protected void configure(HttpSecurity http) throws Exception {
21 | http.authorizeRequests().antMatchers("/login", "/h2-console/**").permitAll()
22 | .antMatchers("/", "/*todo*/**").access("hasRole('USER')").and()
23 | .formLogin();
24 |
25 | http.csrf().disable();
26 | http.headers().frameOptions().disable();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/service/TodoRepository.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.springboot.web.service;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.data.jpa.repository.JpaRepository;
6 |
7 | import com.in28minutes.springboot.web.model.Todo;
8 |
9 | public interface TodoRepository extends JpaRepository{
10 | List findByUser(String user);
11 |
12 | //service.retrieveTodos(name)
13 |
14 | //service.deleteTodo(id);
15 | //service.retrieveTodo(id)
16 | //service.updateTodo(todo)
17 | //service.addTodo(getLoggedInUserName(model), todo.getDesc(), todo.getTargetDate(),false);
18 | }
19 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/service/TodoService.java:
--------------------------------------------------------------------------------
1 | package com.in28minutes.springboot.web.service;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Date;
5 | import java.util.Iterator;
6 | import java.util.List;
7 |
8 | import org.springframework.stereotype.Service;
9 |
10 | import com.in28minutes.springboot.web.model.Todo;
11 |
12 | @Service
13 | public class TodoService {
14 | private static List todos = new ArrayList();
15 | private static int todoCount = 3;
16 |
17 | static {
18 | todos.add(new Todo(1, "in28minutes", "Learn Spring MVC", new Date(),
19 | false));
20 | todos.add(new Todo(2, "in28minutes", "Learn Struts", new Date(), false));
21 | todos.add(new Todo(3, "in28minutes", "Learn Hibernate", new Date(),
22 | false));
23 | }
24 |
25 | public List retrieveTodos(String user) {
26 | List filteredTodos = new ArrayList();
27 | for (Todo todo : todos) {
28 | if (todo.getUser().equalsIgnoreCase(user)) {
29 | filteredTodos.add(todo);
30 | }
31 | }
32 | return filteredTodos;
33 | }
34 |
35 | public Todo retrieveTodo(int id) {
36 | for (Todo todo : todos) {
37 | if (todo.getId()==id) {
38 | return todo;
39 | }
40 | }
41 | return null;
42 | }
43 |
44 | public void updateTodo(Todo todo){
45 | todos.remove(todo);
46 | todos.add(todo);
47 | }
48 |
49 | public void addTodo(String name, String desc, Date targetDate,
50 | boolean isDone) {
51 | todos.add(new Todo(++todoCount, name, desc, targetDate, isDone));
52 | }
53 |
54 | public void deleteTodo(int id) {
55 | Iterator iterator = todos.iterator();
56 | while (iterator.hasNext()) {
57 | Todo todo = iterator.next();
58 | if (todo.getId() == id) {
59 | iterator.remove();
60 | }
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.mvc.view.prefix=/WEB-INF/jsp/
2 | spring.mvc.view.suffix=.jsp
3 | logging.level.org.springframework.web=INFO
4 |
5 | spring.jpa.show-sql=true
6 | spring.h2.console.enabled=true
7 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/resources/data.sql:
--------------------------------------------------------------------------------
1 | insert into TODO
2 | values(10001, 'Learn Spring Boot', false, sysdate(), 'in28minutes');
3 | insert into TODO
4 | values(10002, 'Learn Angular JS', false, sysdate(), 'in28minutes');
5 | insert into TODO
6 | values(10003, 'Learn to Dance', false, sysdate(), 'in28minutes');
7 |
--------------------------------------------------------------------------------
/Otherprojects/03-spring-boot-web-application-h2/src/main/webapp/WEB-INF/jsp/common/footer.jspf:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
10 |
11 |