Quick Springboot
Posted on February 20, 2021
Tags: java
1 Setup on new machine
- First install Java
sudo apt install openjdk-17-jdk openjdk-17-jre
- Install Vs-code springboot Extension pack by Pivotal
1.1 Install jhipster
#jhipster only runs on LTS
nvm install lts/hydrogen
npm install -g generator-jhipster
jhipster
node:internal/modules/cjs/loader:535
throw e;
^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/util/namespace' is not defined by "exports" in /home/debian/.nvm/versions/node/v18.12.1/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/package.json
nano /home/debian/.nvm/versions/node/v18.12.1/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/package.json
- Add
"./lib/util/namespace": "./lib/util/namespace.js"
in exports section
2 VScode commands
- New Class
- Ctrl+Shift+P
>Java: New Java Class
- Ctrl+Shift+P
- Adding spring plugins after creating spring project.
- Ctrl+Shift+P
>Spring Initializr: Add Starters...
- Ctrl+Shift+P
- Adding Class Constructors, getters/setters
- Right-Click > “Generate Constructors”
- Right-Click > “Generate Getters and Setters”
3 Vanilla Project
- mvnw
- pom.xml
- src/
- main /
- java/
- com.example.demo
- DemoApplication.java
- com.example.demo
- resources/
- static/
- templates/
- application.properties
- test/
- java/
- com.example.demo
- DemoApplicationTests.java
- com.example.demo
- java/
- java/
- main /
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
.run(DemoApplication.class, args);
SpringApplication}
}
4 Simple Hello World api
4.1 Adding default Endpoint
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
.run(DemoApplication.class, args);
SpringApplication}
@GetMapping("/")
public String hello() {
return "Hello World";
}
}
4.2 List Output
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
.run(DemoApplication.class, args);
SpringApplication}
@GetMapping("/")
public List<String> hello() {
return List.of("Hello"," World");
}
}
5 Demo Project
5.1 Entry Point
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
= SpringApplication.run(DemoApplication.class, args);
ConfigurableApplicationContext context //Alien a = context.getBean(Alien.class);
}
}
right-click on ‘com.example.demo’ package >> New >> ‘com.example.demo.student’
right-click on ‘com.example.demo.student’ >> New >> Java Class
Java can autogenerate class constructor, getter, setters if you first make the class variables
public class Student {
private Long id;
private String name;
private LocalDate dob;
private String email;
private Integer age;
//Auto-generate the constuctors/getter/setters
}
6 @Autowire @Component
- Controller calls Service functions
@Autowire
is dependency injection and@Component
is the class to be injected.
```{.java filename =StudentController.java} @RestController @RequestMapping(path=“api/v1/student”) public class StudentController { private final StudentService studentService;
//always pair @Autowired function "StudentController" with the associated @Component function parameter's class "Student Service"
@Autowired
public StudentController(StudentService studentService){
this.studentService = studentService;
}
@GetMapping("")
public List<Student> getStudents() {
return studentService.getStudents();
}
}
```{.java filename=StudentService.java}
@Component
public class StudentService {
public List<Student> getStudents() {
return List.of(new Student(1L,"Ma", LocalDate.of(2000, Month.AUGUST,5), "ada",4),
new Student(1L,"Ma",LocalDate.of(2000, Month.AUGUST,5), "ada",4));
}
}
7 Adding a DB
Remember to add spring-jpa dependency in pom.xml
@Entity
@Table
public class Student {
@Id
@SequenceGenerator(
= "student_sequence",
name= "student_sequence",
sequenceName = 1
allocationSize )
@GeneratedValue(
= GenerationType.SEQUENCE,
strategy = "student_sequence"
generator )
private Long id;
private String name;
private LocalDate dob;
private String email;
private Integer age;
public Student() {
}
8 How much physical ram
debian@debian:~/springdemo$ jps
# 3273627 MavenWrapperMain
# 3290617 Jps
# 91642 PropertiesLauncher
# 3274238 springdemoApp
jstat -gccapacity 3274238
top -o %MEM