๐Ÿ‘ฉ‍๐Ÿ’ป Learn programming

[Spring] Spring ๊ตฌ์กฐ์™€ API ํ๋ฆ„ ์ดํ•ดํ•˜๊ธฐ1 (์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ - ์›น๊ฐœ๋ฐœ์˜ ๋ด„, Spring 2์ฃผ์ฐจ)

๋ฐ๊ตฅ์ž‰ 2022. 5. 25. 21:41
๋ฐ˜์‘ํ˜•

{ โญ ๋” ์ •๋ฆฌ๋œ ๋…ธ์…˜ ๋ฒ„์ „ โญ}

 

week 2 ์ˆ™์ œ ์ฝ”๋“œ ํ๋ฆ„ ์ดํ•ดํ•˜๊ธฐ

์ˆ™์ œ ์„ค๋ช…

fair-cheetah-80d.notion.site


โ—Spring์˜ ๊ตฌ์กฐ

1.Controller : ๊ฐ€์žฅ ๋ฐ”๊นฅ ๋ถ€๋ถ„, ์š”์ฒญ/์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•จ.
2. Service : ์ค‘๊ฐ„ ๋ถ€๋ถ„, ์‹ค์ œ ์ค‘์š”ํ•œ ์ž‘๋™์ด ๋งŽ์ด ์ผ์–ด๋‚˜๋Š” ๋ถ€๋ถ„
3. Repo : ๊ฐ€์žฅ ์•ˆ์ชฝ ๋ถ€๋ถ„, DB์™€ ๋งž๋‹ฟ์•„ ์žˆ์Œ. (Repository, Entity) - ๊ฐ•์˜์—์„œ๋Š” models

models - person.java

  1. lombok (import๋„ ํ•ด์ฃผ๊ธฐ)
    • person ํด๋ž˜์Šค์— @Getter ์ ์šฉ (๊ธฐ์กด getName, getJob…์„ ๋Œ€์ฒดํ•จ)
  2. ์ƒ์„ฑ์ž ๋งŒ๋“ค๊ธฐ
public Person(PersonRequestDto requestDto) {
        this.name = requestDto.getName();
        this.job = requestDto.getJob();
        this.age = requestDto.getAge();
        this.address = requestDto.getAddress();
    }

 

 

models - PersonRequestDto.java

  1. PersonRequestDto.java ํด๋ž˜์Šค ์ƒ์„ฑ
import lombok.Getter;

@Getter
public class PersonRequestDto {
    private String name;
    private String job;
    private int age;
    private String address;
}

API

POST - ์ƒ์„ฑ

  1. controller - PersonController.java
@PostMapping("/api/persons")
//ํฌ์ŠคํŠธ ์š”์ฒญ์ด ์˜ฌ ๋•Œ ํ•ญ์ƒ ์‹คํ–‰๋˜๋Š” ๋ฉ”์†Œ๋“œ 
    public Person createPerson(@RequestBody PersonRequestDto requestDto) {
        Person person = new Person(requestDto);
        return **personRepository**.save(person);
    }
  • PersonRequestDto requestDto : ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์†Œ์žฌ(ํŒŒ๋ผ๋ฏธํ„ฐ)
  • @RequestBody : Request์˜ body ์˜์—ญ์— ์žˆ๋Š” ๊ฒŒ ์ œ๋Œ€๋กœ ์ „๋‹ฌ๋˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
  • personRepository์˜ save๋ฅผ ์‚ฌ์šฉํ•ด์„œ person ๊ฐ’์„ ์ €์žฅํ•ด์•ผํ•จ.
    • (๐Ÿ˜‹) But! personRepository ์•„์ง ์ƒ์„ฑ ์•ˆ๋จ

 

2. models - PersonRepository.java ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ

  • JPA๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ JpaRepository ์ƒ์†์„ ํ•ด์ฃผ๋Š”๋ฐ (extends JpaRepository)
  • person์— ๋Œ€ํ•œ ๊ฒƒ์ด๊ณ  Long์ด ๋น ์ ธ์žˆ๋‹ค. (<Person, Long>)
public interface PersonRepository extends JpaRepository<Person, Long> {
}

 

 

3. models - person.java ํŒŒ์ผ

  • @Entity (ํ…Œ์ด๋ธ”์ž„์„ ์•Œ๋ ค์คŒ), @NoArgsConstructor (๊ธฐ๋ณธ ์ƒ์„ฑ์ž ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ฒƒ) ์„ Person ํด๋ž˜์Šค์— ์ถ”๊ฐ€
  • private key๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ Long ํƒ€์ž…์˜ Id ๋งŒ๋“ค๊ธฐ
@GeneratedValue(strategy = GenerationType.AUTO)
// ์ƒ์„ฑ ์ „๋žต์ด ์ž๋™์œผ๋กœ ์ฆ๊ฐ€ ๋ถ€์—ฌ
@Id
private Long id;
// ๊ฐ๊ฐ์˜ ์ปฌ๋Ÿผ์— ๋Œ€ํ•ด์„œ ์ง€์ •ํ•˜๊ธฐ
// ์—†์œผ๋ฉด ์•ˆ๋œ๋‹ค nullable = false

@Column(nullable = false)
private String name;

@Column(nullable = false)
private String job;

@Column(nullable = false)
private int age;

@Column(nullable = false)
private String address;

 

4. ๋‹ค์‹œ controller - PersonController.java

@RequiredArgsConstructor
//๊ถŒํ•œ์„ ์ค„ํ…Œ๋‹ˆ๊นŒ ์Šคํ”„๋ง์—๊ฒŒ ์•Œ์•„์„œ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ
@RestController
public class PersonController {
    private final PersonRepository personRepository;
		// PersonController ํ•  ๋•Œ ๊ผญ ํ•„์š”ํ•œ๊ฑฐ์•ผ (final)
}
  • @RequiredArgsConstructor : ‘๋‹ˆ๊ฐ€ PersonController ๊ฐ–๋‹ค ์“ฐ๋ ค๊ณ  ํ•  ๋•Œ, ๋ฉค๋ฒ„๋กœ ์„ ์–ธ๋œ ๊ฒƒ ์ค‘์— final๋กœ ๋œ ๊ฑฐ ์žˆ์œผ๋ฉด ์•Œ์•„์„œ ์ƒ์„ฑํ•ด์ค˜’ ๋ผ๊ณ  ๊ถŒํ•œ์„ ๋„˜๊ฒจ์คŒ
  • (๐Ÿ˜‹) ์ด๋ ‡๊ฒŒ personRepository ์ƒ์„ฑ ํ•ด์คฌ์Œ!!

 

5. controller - PersonController.java ์—์„œ api ์™„์„ฑํ•˜๊ธฐ

@PostMapping("/api/persons")
// ํฌ์ŠคํŠธ ์š”์ฒญ์ด ์˜ฌ ๋•Œ ํ•ญ์ƒ ์‹คํ–‰๋˜๋Š” ๋ฉ”์†Œ๋“œ 
    public Person createPerson(@RequestBody PersonRequestDto requestDto) {
        Person person = new Person(requestDto);
        return personRepository.save(person);
    }
  • personRepository๋ฅผ return ํ•ด์ฃผ๋ฉด ์ƒ์„ฑํ•˜๋Š” POST API๊ฐ€ ์™„์„ฑ๋จ !

 

GET - ์กฐํšŒ

  1. controller - PersonController.java
@GetMapping("/api/persons")
    public List<Person> readPersons() {
        return personRepository.findAll();
    }
  • personRepository๋ฅผ ์ด์šฉํ•ด findAll์„ ํ•ด์„œ ๋ชจ๋“  ์‚ฌ์šฉ์ž์˜ ๋ชฉ๋ก์„ ๋Œ๋ ค์คŒ
  • ๋„๋ฉ”์ธ์ด ๊ฐ™์•„๋„ ์š”์ฒญ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์คŒ. ์ตœ๊ณ  ..

 

PUT - ์ˆ˜์ •

  1. controller - PersonController.java
@PutMapping("/api/persons/{id}")
    public Long updatePerson(@PathVariable Long id, @RequestBody PersonRequestDto requestDto) {
    }
  • api ๋„๋ฉ”์ธ์— {id}๋ฅผ ์ถ”๊ฐ€ํ•ด ์–ด๋–ค id์˜ ๊ฐ’์„ ์ˆ˜์ •ํ• ์ง€ ์•Œ๋ ค์ค˜์•ผํ•จ
  • ํŒŒ๋ผ๋ฏธํ„ฐ ์ค‘์š” !
    • (Long id, PersonRequestDto requestDto) ๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋„๋ฉ”์ธ์˜ {id}์™€ ๋งค์นญ์ด ๋˜๋Š”์ง€, ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” body์™€ requestDto๊ฐ€ ๋งค์นญ์ด ๋˜๋Š”์ง€ ์Šคํ”„๋ง์ด ์•Œ ์ˆ˜ ์—†์Œ!
    • ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ์— @PathVariable (์ฃผ์†Œ), @RequestBody ๋กœ ์•Œ๋ ค์ค˜์•ผํ•จ! ํ•„์ˆ˜!
    • Long id : ์–ด๋–ค id์˜ ๊ฐ’์„ ์ˆ˜์ •ํ•˜๋Š”์ง€ ์ฃผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ
    • PersonRequestDto requestDto : ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ
  • ์ˆ˜์ •์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” (1) id์— ํ•ด๋‹นํ•˜๋Š” person ๊ฐ’์„ ์ฐพ์•„์„œ, (2) ๊ทธ๊ฒƒ์„ ์—…๋ฐ์ดํŠธ ์‹œ์ผœ์ค˜์•ผํ•จ

(1) id์— ํ•ด๋‹นํ•˜๋Š” person ๊ฐ’ ์ฐพ๊ธฐ (personService์— ์ ์šฉํ• ๊ฑฐ์•ผ)

Person person = personRepository.findById(id).orElseThrow(
				() -> new IllegalArgumentException("์•„์ด๋””๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
);
  • orElseThrow( ) : ์—†์„ ๋•Œ๋Š” ์ด๋Ÿฐ ์˜ค๋ฅ˜๋ฅผ ์ผ์œผ์ผœ์„œ ๋‚˜ํ•œํ…Œ ์•Œ๋ ค์ค˜.
  • (โ—) ํ•˜์ง€๋งŒ, ์ด ์ฝ”๋“œ๋ฅผ controller์˜ PUT์—์„œ ์ฒ˜๋ฆฌํ•˜๋ฉด, service์™€ controller์˜ ์˜์—ญ์ด ๊ฒน์น˜๊ฒŒ ๋จ.

(2) ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ

@PutMapping("/api/persons/{id}")
    public Long updatePerson(@PathVariable Long id, @RequestBody PersonRequestDto requestDto) {
        return personService.update(id, requestDto);
    }
  • personService์˜ update ๋ฉ”์†Œ๋“œ์—์„œ id๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” return ์ฝ”๋“œ ์ž‘์„ฑ.
    • (๐Ÿคฉ) But! personService ์•„์ง ์ƒ์„ฑ ์•ˆ๋จ.

 

2. service - personService.java ํด๋ž˜์Šค ์ƒ์„ฑ

  • (๐Ÿคฉ) personService ์ƒ์„ฑํ•ด์คฌ์Œ!
@RequiredArgsConstructor
@Service //์„œ๋น„์Šค๋ผ๊ณ  ์•Œ๋ ค์ค˜์•ผ ํ•จ. 
public class PersonService {
    private final PersonRepository personRepository;

    @Transactional //์ด ๋ฉ”์†Œ๋“œ์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜๋“œ์‹œ ๋ฐ˜์˜ ๋ผ์•ผ ํ•ด
    public Long update(Long id, PersonRequestDto requestDto) {
        Person person = personRepository.findById(id).orElseThrow(
                () -> new IllegalArgumentException("ํ•ด๋‹น id๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
        ); 
    }
}
  • update ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ id๋ฅผ ๋Œ๋ ค์ค„ ๊ฑฐ๋‹ˆ๊นŒ Long ํƒ€์ž…
  • (โ—) PUT 1 - (1) ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ์ฃผ๊ธฐ
  • ์ฆ‰, id ๊ฐ’์œผ๋กœ ํ•ด๋‹นํ•˜๋Š” person ๊ฐ’ ์ฐพ๋Š” ๊ณผ์ •์„ ๋ถ™์—ฌ์คŒ.
  • personRepository๋ฅผ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๊ณ  (private final PersonRepository personRepository;)
  • ๊ถŒํ•œ ์ฃผ๊ธฐ (@RequiredArgsConstructor)

 

3. models - person.java ์—์„œ update ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ธฐ

public void update(PersonRequestDto requestDto) {
        this.name = requestDto.getName();
        this.job = requestDto.getJob();
        this.age = requestDto.getAge();
        this.address = requestDto.getAddress();
    }
  • Person ๋ฉ”์†Œ๋“œ์™€ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ์ดํ„ฐ(ํŒŒ๋ผ๋ฏธํ„ฐ)์™€ ํ•˜๋Š” ์ผ๋„ ๋˜‘๊ฐ™์Œ
  • requestDto์—์„œ name, job, age, address ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ๋‚ด๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” person์„ ์—…๋ฐ์ดํŠธ ํ•จ.

 

4. service - personService.java

  • service์˜ update ๋ฉ”์†Œ๋“œ ์•ˆ์— update ํ•ด์„œ return ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•จ.
person.update(requestDto);
return person.getId();

 

 

5. controller - PersonController.java

@RequiredArgsConstructor
//๊ถŒํ•œ์„ ์ค„ํ…Œ๋‹ˆ๊นŒ ์Šคํ”„๋ง์—๊ฒŒ ์•Œ์•„์„œ ์ƒ์„ฑ์ž๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ
@RestController
public class PersonController {
    private final PersonRepository personRepository;
		private final PersonService personService;
		//ํ•„์š”ํ•˜๋ฉด ์–ธ์ œ๋“ ์ง€ personService ์จ๋จน์–ด~
}
  • personService๋ฅผ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๊ณ  personService์— ์‚ฌ์šฉ ๊ถŒํ•œ์„ ์คŒ.

 

DELETE - ์‚ญ์ œ

  1. controller - PersonController.java
@DeleteMapping("/api/persons/{id}")
    public Long deletePerson(@PathVariable Long id) {
        personRepository.deleteById(id);
        return id;
    }
  • api ๋„๋ฉ”์ธ์— {id}๋ฅผ ์ถ”๊ฐ€ํ•ด ์–ด๋–ค id์˜ ๊ฐ’์„ ์‚ญ์ œํ• ์ง€ ์•Œ๋ ค์ค˜์•ผํ•จ, Id์˜ ํƒ€์ž…์€ Long
  • id๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ @PathVariable (์ฃผ์†Œ)

 


ํ”„๋กœ๊ทธ๋žจ run

  1. ํ”„๋กœ์ ํŠธ ํŒŒ์ผ์— ์žˆ๋Š” HwkWeek01.Application.java
@EnableJpaAuditing
@SpringBootApplication
public class HwkWeek01Application {

    public static void main(String[] args) {
        SpringApplication.run(HwkWeek01Application.class, args);
    }
}
  • @EnableJpaAuditing : JPA๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์คŒ
๋ฐ˜์‘ํ˜•