Quick Springboot

Posted on February 20, 2021
Tags: java

1 Setup on new machine

sudo apt install openjdk-17-jdk openjdk-17-jre

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
  1. nano /home/debian/.nvm/versions/node/v18.12.1/lib/node_modules/generator-jhipster/node_modules/yeoman-environment/package.json
  2. Add "./lib/util/namespace": "./lib/util/namespace.js" in exports section

2 VScode commands

3 Vanilla Project

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) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

4 Simple Hello World api

4.1 Adding default Endpoint

@SpringBootApplication
@RestController
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
	@GetMapping("/")
	public String hello() {
		return "Hello World";
	}
}

4.2 List Output

@SpringBootApplication
@RestController
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
	@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) {
		ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
		//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

```{.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(
            name= "student_sequence",
            sequenceName = "student_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "student_sequence"
    )
    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