Programming/JAVA
[JAVA] Springboot 정보를 담는 클래스 생성
신샤인
2022. 1. 1. 11:26
반응형
예시로 학생 정보를 담는 Class를 생성한다.
구조체, Getter & Setter 생성하여 객체 정보를 받아올 수 있도록 설정함
package com.js.app.student;
import java.time.LocalDate;
import org.apache.tomcat.jni.Local;
public class Student {
private Long id;
private String name;
private String email;
private LocalDate dob;
private Integer age;
// 객체가 가진 정보를 String으로 리턴, 생성하지 않을 경우 제대로 된 값을 확인 못함
@Override
public String toString() {
return "{" +
" id='" + getId() + "'" +
", name='" + getName() + "'" +
", email='" + getEmail() + "'" +
", dob='" + getDob() + "'" +
", age='" + getAge() + "'" +
"}";
}
//Getter & Setter
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public LocalDate getDob() {
return this.dob;
}
public void setDob(LocalDate dob) {
this.dob = dob;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
//Empty Constructor
public Student() {
}
//OverLoading
public Student(Long id, String Name,String email, LocalDate dob, Integer age) {
this.id = id;
this.name = name;
this.email = email;
this.dob = dob;
this.age = age;
}
public Student(String Name,String email, LocalDate dob, Integer age) {
this.name = name;
this.email = email;
this.dob = dob;
this.age = age;
}
}
AppApplication 설정
package com.js.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController // REST API 정의
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
}
@GetMapping
public List<Student> hello() {
return List.of(
new Student(1L, "Mariam", "Mariam@gmail.com",
LocalDate.of(2000, Month.JANUARY, 5),21)
);
}
}
VSCode에서 Getter & Setter, 구조체 등 자동 생성하는 플러그인
toString(), getter & setter, constructor 등등 유용하게 사용할 수 있음.
마우스 오른쪽 클릭 or 커맨드 실행
참조
https://www.youtube.com/watch?v=9SGDpanrc8U&t=1600s
끝
반응형