2
2
.
.
8
8
.
.
2
2
B
B
u
u
s
s
i
i
n
n
e
e
s
s
s
s
L
L
o
o
g
g
i
i
c
c
-
-
I
I
n
n
s
s
i
i
d
d
e
e
S
S
e
e
r
r
v
v
i
i
c
c
e
e
I
I
n
n
f
f
o
o
[
[
G
G
]
]
The above problems can be solved by creating separate Service Class for each part of business logic.
That way we get multiple smaller more manageable Classes each focusing on a specific part of business logic.
With this approach
● Controller now has a single task of routing incoming HTTP Requests to appropriate Service
● to change business logic we don't need to change the Controller potentially introducing bugs in it
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
@Service
MyService
http://localhost:8080/Hello
Tomcat
Browser
MyController
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_service (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
Create Package: services (inside main package)
– Create Class: MyService.java (inside package controllers)
MyService.java
package com.ivoronline.springboot_service.services;
import org.springframework.stereotype.Service;
@Service
public class MyService {
public String hello() {
return "Hello from Service";
}
}
MyController.java
package com.ivoronline.springboot_service.controllers;
import com.ivoronline.springboot_service.services.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@Autowired MyService myService;
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
//CALL SERVICE
String result = myService.hello();
//RETURN RESULT
return result;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Application Structure