html
Spring REST์์ Update Album API ๊ตฌํ: ํฌ๊ด์ ์ธ ๊ฐ์ด๋
๋ชฉ์ฐจ
- ์๊ฐ ......................................................... 1
- Update Album API ์ดํดํ๊ธฐ .... 3
- API ๊ฐ์ .................................................. 3
- ์ฃผ์ ๊ตฌ์ฑ ์์ ............................................ 4
- ํ๋ก์ ํธ ์ค์ ............................ 6
- ํ๋ก์ ํธ ๊ตฌ์กฐ ................................... 6
- Update Album API ๊ตฌํ ..... 8
- DTOs: ๋ฐ์ดํฐ ์ ์ก ๊ฐ์ฒด ................. 8
- ์ปจํธ๋กค๋ฌ ๊ณ์ธต ......................................... 10
- ์๋น์ค ๊ณ์ธต ................................................. 12
- ๋ฆฌํฌ์งํ ๋ฆฌ ๊ณ์ธต ...................................... 14
- ๋ณด์ ๋ฐ ๊ถํ ๋ถ์ฌ ..................... 16
- Update Album API ํ ์คํธ ............... 18
- ๊ฒฐ๋ก .......................................................... 20
์๊ฐ
๋์์์ด ๋ฐ์ ํ๋ ์น ๊ฐ๋ฐ ํ๊ฒฝ์์ API(Application Programming Interfaces)๋ ๋ค์ํ ์ํํธ์จ์ด ๊ตฌ์ฑ ์์ ๊ฐ์ ์ํํ ์ํธ ์์ฉ์ ๊ฐ๋ฅํ๊ฒ ํ๋ ์ค์ถ์ ์ธ ์ญํ ์ ํฉ๋๋ค. ๋ง์ ์ ํ๋ฆฌ์ผ์ด์ ์์ ํ์์ ์ธ API ์ค ํ๋๋ Update Album API๋ก, ์ฌ์ฉ์๊ฐ ๊ธฐ์กด ์จ๋ฒ ์ธ๋ถ ์ ๋ณด๋ฅผ ์์ ํ ์ ์๊ฒ ํด์ค๋๋ค. ์ฌ์ง ๊ฐค๋ฌ๋ฆฌ, ์์ ํ๋ซํผ ๋๋ ์ปฌ๋ ์ ์ ๊ด๋ฆฌํ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ถํ๋ , ์ด๋ฌํ API๋ฅผ ๊ตฌํํ๊ณ ์ต์ ํํ๋ ๋ฐฉ๋ฒ์ ์ดํดํ๋ ๊ฒ์ ๋งค์ฐ ์ค์ํฉ๋๋ค.
์ด ๊ฐ์ด๋๋ Java์์ ๊ฒฌ๊ณ ํ๊ณ ํ์ฅ ๊ฐ๋ฅํ ์น ์๋น์ค๋ฅผ ๊ตฌ์ถํ๊ธฐ ์ํ ๊ฐ๋ ฅํ ํ๋ ์์ํฌ์ธ Spring REST๋ฅผ ์ฌ์ฉํ์ฌ Update Album API๋ฅผ ์์ฑํ๋ ๊ณผ์ ์ ๊น์ด ์๊ฒ ๋ค๋ฃน๋๋ค. ์ด๋ณด์์ ๊ธฐ๋ณธ ์ง์์ ๊ฐ์ง ๊ฐ๋ฐ์๋ฅผ ์ํด ๋ง์ถคํ๋ ์ด ์ ์์ฑ ์ ์ฝ๋ ์ค๋ํซ, ์ค๋ช ๋ฐ ๋ชจ๋ฒ ์ฌ๋ก๋ก ํ๋ถํ๊ฒ ๊ตฌ์ฑ๋ ๋จ๊ณ๋ณ ์ ๊ทผ ๋ฐฉ์์ ์ ๊ณตํ์ฌ ๊ฐ๋ฐ ์ฌ์ ์์ ๋ช ํ์ฑ๊ณผ ํจ์จ์ฑ์ ๋ณด์ฅํฉ๋๋ค.
Update Album API ์ดํดํ๊ธฐ
API ๊ฐ์
Update Album API๋ ์ ํ๋ฆฌ์ผ์ด์ ๋ด์์ ๊ธฐ์กด ์จ๋ฒ์ ์ธ๋ถ ์ ๋ณด๋ฅผ ์์ ํ๋ ์ฃผ์ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. ์ฌ๊ธฐ์๋ ์จ๋ฒ์ ์ด๋ฆ, ์ค๋ช ๋ฐ ๊ด๋ จ ์ฌ์ง๊ณผ ๊ฐ์ ์์ฑ์ ์ ๋ฐ์ดํธํ๋ ๊ฒ์ด ํฌํจ๋ฉ๋๋ค. ์ด API๋ฅผ ๊ตฌํํ๋ฉด ์ฌ์ฉ์๊ฐ ์จ๋ฒ ์ ๋ณด๋ฅผ ์ต์ ์ํ๋ก ์ ์งํ๊ณ ๊ด๋ จ์ฑ์ ์ ์งํ ์ ์์ต๋๋ค.
์ฃผ์ ์์ :
- ์ ํจ์ฑ ๊ฒ์ฌ: ์ ๋ฐ์ดํธ๋ฅผ ์์ฒญํ๋ ์ฌ์ฉ์๊ฐ ํ์ํ ๊ถํ์ ๊ฐ์ง๊ณ ์๋์ง ํ์ธํฉ๋๋ค.
- ํ์ด๋ก๋ ์ฒ๋ฆฌ: ์จ๋ฒ์ ๋ํ ์๋ก์ด ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ณ ์ฒ๋ฆฌํฉ๋๋ค.
- ์ง์์ฑ: ์ ๋ฐ์ดํธ๋ ์ธ๋ถ ์ ๋ณด๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํฉ๋๋ค.
- ์๋ต: ์ ๋ฐ์ดํธ๋ ์จ๋ฒ ์ ๋ณด๋ฅผ ํด๋ผ์ด์ธํธ์ ๋ฐํํฉ๋๋ค.
์ฃผ์ ๊ตฌ์ฑ ์์
- DTOs (Data Transfer Objects): ํด๋ผ์ด์ธํธ์ ์๋ฒ ๊ฐ์ ๋ฐ์ดํฐ ์ ์ก์ ์ฉ์ดํ๊ฒ ํฉ๋๋ค.
- Controller: ๋ค์ด์ค๋ HTTP ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ์ ์ ํ ์๋น์ค ๋ฉ์๋๋ก ์ ๋ฌํฉ๋๋ค.
- Service: ์ ๋ฐ์ดํธ๋ฅผ ์ฒ๋ฆฌํ๋ ๋น์ฆ๋์ค ๋ก์ง์ ํฌํจํฉ๋๋ค.
- Repository: ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ก๋ถํฐ์ ๋ฐ์ดํฐ ์ง์์ฑ๊ณผ ๊ฒ์์ ๊ด๋ฆฌํฉ๋๋ค.
- Security: ๊ถํ์ด ๋ถ์ฌ๋ ์ฌ์ฉ์๋ง ์ ๋ฐ์ดํธ ์์ ์ ์ํํ ์ ์๋๋ก ๋ณด์ฅํฉ๋๋ค.
ํ๋ก์ ํธ ์ค์
๊ตฌํ์ ๋ฐ์ด๋ค๊ธฐ ์ ์ ํ๋ก์ ํธ ํ๊ฒฝ์ ์ฌ๋ฐ๋ฅด๊ฒ ์ค์ ํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
ํ๋ก์ ํธ ๊ตฌ์กฐ
์ ๊ตฌ์ฑ๋ ํ๋ก์ ํธ ๊ตฌ์กฐ๋ ์ ์ง ๋ณด์์ฑ๊ณผ ํ์ฅ์ฑ์ ํฅ์์ํต๋๋ค. ๋ค์์ Spring REST ์ ํ๋ฆฌ์ผ์ด์ ์ ์ผ๋ฐ์ ์ธ ๊ตฌ์กฐ ๊ฐ์์ ๋๋ค:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
spring-restdemo โโโ src โ โโโ main โ โ โโโ java โ โ โ โโโ org.studyeasy.SpringRestdemo โ โ โ โโโ SpringRestdemoApplication.java โ โ โ โโโ config โ โ โ โ โโโ SwaggerConfig.java โ โ โ โโโ controller โ โ โ โ โโโ AlbumController.java โ โ โ โโโ model โ โ โ โ โโโ Album.java โ โ โ โ โโโ Photo.java โ โ โ โโโ payload โ โ โ โ โโโ album โ โ โ โ โโโ AlbumPayloadDTO.java โ โ โ โ โโโ AlbumViewDTO.java โ โ โ โโโ repository โ โ โ โ โโโ AlbumRepository.java โ โ โ โ โโโ PhotoRepository.java โ โ โ โโโ security โ โ โ โ โโโ SecurityConfig.java โ โ โ โโโ service โ โ โ โโโ AlbumService.java โ โ โโโ resources โ โ โโโ application.properties โ โโโ test โ โโโ java โ โโโ org.studyeasy.SpringRestdemo โ โโโ SpringRestdemoApplicationTests.java โโโ pom.xml โโโ README.md |
ํ 1: ํ๋ก์ ํธ ๊ตฌ์กฐ ๊ฐ์
๋๋ ํ ๋ฆฌ/ํ์ผ | ์ค๋ช |
---|---|
controller |
HTTP ์์ฒญ ๋ฐ ์๋ต์ ์ฒ๋ฆฌํฉ๋๋ค. |
model |
๋ฐ์ดํฐ ๋ชจ๋ธ/์ํฐํฐ๋ฅผ ์ ์ํฉ๋๋ค. |
payload |
๋ฐ์ดํฐ ์ ์ก์ ์ํ DTOs๋ฅผ ํฌํจํฉ๋๋ค. |
repository |
๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ ์ ์ํ ์ธํฐํ์ด์ค์ ๋๋ค. |
service |
๋น์ฆ๋์ค ๋ก์ง์ ํฌํจํฉ๋๋ค. |
security |
์ ํ๋ฆฌ์ผ์ด์ ๋ณด์์ ๊ด๋ฆฌํฉ๋๋ค. |
config |
์ค์ ํ์ผ (์: Swagger) |
resources |
์ ํ๋ฆฌ์ผ์ด์ ๋ฆฌ์์ค ๋ฐ ์์ฑ ํ์ผ์ ๋๋ค. |
test |
๋จ์ ๋ฐ ํตํฉ ํ ์คํธ์ ๋๋ค. |
Update Album API ๊ตฌํ
ํ๋ก์ ํธ ๊ตฌ์กฐ๊ฐ ์ค๋น๋์์ผ๋ฏ๋ก, Update Album API๋ฅผ ๊ตฌํํด ๋ณด๊ฒ ์ต๋๋ค.
DTOs: ๋ฐ์ดํฐ ์ ์ก ๊ฐ์ฒด
๋ฐ์ดํฐ ์ ์ก ๊ฐ์ฒด(DTOs)๋ ๋ฐ์ดํฐ๋ฅผ ์บก์ํํ๊ณ ํด๋ผ์ด์ธํธ์ ํ์ํ ์ ๋ณด๋ง ๋ ธ์ถ๋๋๋ก ๋ณด์ฅํ๋ ๋ฐ ์ค์ํฉ๋๋ค.
AlbumPayloadDTO.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.studyeasy.SpringRestdemo.payload.album; public class AlbumPayloadDTO { private String name; private String description; // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } |
AlbumViewDTO.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package org.studyeasy.SpringRestdemo.payload.album; import java.util.List; public class AlbumViewDTO { private String name; private String description; private List<PhotoDTO> photos; // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public List<PhotoDTO> getPhotos() { return photos; } public void setPhotos(List<PhotoDTO> photos) { this.photos = photos; } } |
์ปจํธ๋กค๋ฌ ๊ณ์ธต
์ปจํธ๋กค๋ฌ๋ ๋ค์ด์ค๋ HTTP ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ์๋น์ค ๊ณ์ธต์ ์์ ์ ์์ํฉ๋๋ค.
AlbumController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package org.studyeasy.SpringRestdemo.controller; import org.studyeasy.SpringRestdemo.payload.album.AlbumPayloadDTO; import org.studyeasy.SpringRestdemo.payload.album.AlbumViewDTO; import org.studyeasy.SpringRestdemo.service.AlbumService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/albums") public class AlbumController { @Autowired private AlbumService albumService; /** * ๊ธฐ์กด ์จ๋ฒ์ ์
๋ฐ์ดํธํฉ๋๋ค. * * @param albumId ์
๋ฐ์ดํธํ ์จ๋ฒ์ ID์
๋๋ค. * @param payload ์๋ก์ด ์จ๋ฒ ๋ฐ์ดํฐ์
๋๋ค. * @return ์
๋ฐ์ดํธ๋ ์จ๋ฒ ์ธ๋ถ ์ ๋ณด์
๋๋ค. */ @PutMapping("/{albumId}/action") public ResponseEntity<AlbumViewDTO> updateAlbum( @PathVariable Long albumId, @RequestBody AlbumPayloadDTO payload) { AlbumViewDTO updatedAlbum = albumService.updateAlbum(albumId, payload); return new ResponseEntity<>(updatedAlbum, HttpStatus.NO_CONTENT); } } |
์ค๋ช :
- ์๋ํฌ์ธํธ: PUT /albums/{albumId}/action
- ๋งค๊ฐ๋ณ์:
- albumId: ์ ๋ฐ์ดํธํ ์จ๋ฒ์ ID์ ๋๋ค.
- payload: ์จ๋ฒ์ ์๋ก์ด ๋ฐ์ดํฐ์ ๋๋ค.
- ์๋ต:
204 No Content
์ํ์ ํจ๊ป ์ ๋ฐ์ดํธ๋ ์จ๋ฒ ์ธ๋ถ ์ ๋ณด๋ฅผ ๋ฐํํฉ๋๋ค.
์๋น์ค ๊ณ์ธต
์๋น์ค๋ ์ ๋ฐ์ดํธ ์์ ์ ์ฒ๋ฆฌํ๋ ๋น์ฆ๋์ค ๋ก์ง์ ํฌํจํฉ๋๋ค.
AlbumService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
package org.studyeasy.SpringRestdemo.service; import org.studyeasy.SpringRestdemo.model.Album; import org.studyeasy.SpringRestdemo.payload.album.AlbumPayloadDTO; import org.studyeasy.SpringRestdemo.payload.album.AlbumViewDTO; import org.studyeasy.SpringRestdemo.repository.AlbumRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.server.ResponseStatusException; import java.util.Optional; import java.util.stream.Collectors; @Service public class AlbumService { @Autowired private AlbumRepository albumRepository; /** * ์ฃผ์ด์ง ID๋ฅผ ๊ฐ์ง ์จ๋ฒ์ ์ ๊ณต๋ ํ์ด๋ก๋๋ก ์
๋ฐ์ดํธํฉ๋๋ค. * * @param albumId ์
๋ฐ์ดํธํ ์จ๋ฒ์ ID์
๋๋ค. * @param payload ์๋ก์ด ์จ๋ฒ ๋ฐ์ดํฐ์
๋๋ค. * @return ์
๋ฐ์ดํธ๋ ์จ๋ฒ ์ธ๋ถ ์ ๋ณด์
๋๋ค. */ public AlbumViewDTO updateAlbum(Long albumId, AlbumPayloadDTO payload) { Optional<Album> optionalAlbum = albumRepository.findById(albumId); if (!optionalAlbum.isPresent()) { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "์จ๋ฒ์ ์ฐพ์ ์ ์์ต๋๋ค"); } Album album = optionalAlbum.get(); // ํ์ฌ ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์๋๊ฐ ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค String currentUser = getCurrentLoggedInUser(); // ํ์ฌ ์ฌ์ฉ์๊ฐ ์จ๋ฒ์ ์์ ์์ธ์ง ํ์ธํฉ๋๋ค if (!album.getOwner().equals(currentUser)) { throw new ResponseStatusException(HttpStatus.FORBIDDEN, "๋น์ ์ ์ด ์จ๋ฒ์ ์์ ์๊ฐ ์๋๋๋ค"); } // ์จ๋ฒ ์ธ๋ถ ์ ๋ณด ์
๋ฐ์ดํธ album.setName(payload.getName()); album.setDescription(payload.getDescription()); // ์
๋ฐ์ดํธ๋ ์จ๋ฒ ์ ์ฅ albumRepository.save(album); // AlbumViewDTO๋ก ๋ณํ AlbumViewDTO viewDTO = new AlbumViewDTO(); viewDTO.setName(album.getName()); viewDTO.setDescription(album.getDescription()); viewDTO.setPhotos(album.getPhotos().stream() .map(photo -> { PhotoDTO photoDTO = new PhotoDTO(); photoDTO.setUrl(photo.getUrl()); photoDTO.setDescription(photo.getDescription()); return photoDTO; }) .collect(Collectors.toList())); return viewDTO; } /** * ํ์ฌ ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์์ ์ฌ์ฉ์ ์ด๋ฆ์ ๊ฐ์ ธ์ต๋๋ค. * * @return ์ฌ์ฉ์ ์ด๋ฆ์
๋๋ค. */ private String getCurrentLoggedInUser() { // ํ์ฌ ์ฌ์ฉ์๋ฅผ ๊ฐ์ ธ์ค๋ ๊ตฌํ return "currentUser"; // ํ๋ ์ด์คํ๋ } } |
์ค๋ช :
- ์ ํจ์ฑ ๊ฒ์ฌ: ์จ๋ฒ์ด ์กด์ฌํ๋์ง ํ์ธํฉ๋๋ค. ์กด์ฌํ์ง ์์ผ๋ฉด
404 Not Found
์ค๋ฅ๋ฅผ ๋ฐ์์ํต๋๋ค. - ๊ถํ ๋ถ์ฌ: ํ์ฌ ์ฌ์ฉ์๊ฐ ์์ ์์ธ์ง ํ์ธํฉ๋๋ค. ์์ ์๊ฐ ์๋๋ฉด
403 Forbidden
์ค๋ฅ๋ฅผ ๋ฐ์์ํต๋๋ค. - ์ ๋ฐ์ดํธ ์์ : ์จ๋ฒ์ ์ด๋ฆ๊ณผ ์ค๋ช ์ ์ ๋ฐ์ดํธํฉ๋๋ค.
- ์ง์์ฑ: ์ ๋ฐ์ดํธ๋ ์จ๋ฒ์ ๋ฆฌํฌ์งํ ๋ฆฌ์ ์ ์ฅํฉ๋๋ค.
- ์๋ต ๋งคํ:
Album
์ํฐํฐ๋ฅผAlbumViewDTO
๋ก ๋ณํํ์ฌ ํด๋ผ์ด์ธํธ์ ๋ฐํํฉ๋๋ค.
๋ฆฌํฌ์งํ ๋ฆฌ ๊ณ์ธต
๋ฆฌํฌ์งํ ๋ฆฌ ์ธํฐํ์ด์ค๋ ๋ฐ์ดํฐ ์ง์์ฑ๊ณผ ๊ฒ์ ์์ ์ ์ฒ๋ฆฌํฉ๋๋ค.
AlbumRepository.java
1 2 3 4 5 6 7 8 9 10 11 |
package org.studyeasy.SpringRestdemo.repository; import org.studyeasy.SpringRestdemo.model.Album; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface AlbumRepository extends JpaRepository<Album, Long> { // ์ถ๊ฐ์ ์ธ ์ฟผ๋ฆฌ ๋ฉ์๋๋ฅผ ์ฌ๊ธฐ์ ์ ์ํ ์ ์์ต๋๋ค } |
๋ชจ๋ธ ๊ณ์ธต
๋ฐ์ดํฐ ๋ชจ๋ธ์ ์ ์ํ๋ ๊ฒ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ํฐํฐ๋ฅผ ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ์ฒด๋ก ๋งคํํ๋ ๋ฐ ํ์์ ์ ๋๋ค.
Album.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.studyeasy.SpringRestdemo.model; import javax.persistence.*; import java.util.List; @Entity public class Album { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String description; private String owner; @OneToMany(mappedBy = "album", cascade = CascadeType.ALL) private List<Photo> photos; // Getters and Setters // ... } |
Photo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.studyeasy.SpringRestdemo.model; import javax.persistence.*; @Entity public class Photo { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String url; private String description; @ManyToOne @JoinColumn(name = "album_id") private Album album; // Getters and Setters // ... } |
์ค๋ช :
- Album:
id
,name
,description
,owner
์ ๊ฐ์ ์์ฑ์ ๊ฐ์ง ์จ๋ฒ ์ํฐํฐ๋ฅผ ๋ํ๋ ๋๋ค.Photo
์ ์ผ๋๋ค ๊ด๊ณ๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค. - Photo: ์จ๋ฒ๊ณผ ์ฐ๊ด๋ ์ฌ์ง ์ํฐํฐ๋ฅผ ๋ํ๋
๋๋ค. ๊ฐ ์ฌ์ง์๋
url
๊ณผdescription
์ด ์์ต๋๋ค.
๋ณด์ ๋ฐ ๊ถํ ๋ถ์ฌ
๊ถํ์ด ๋ถ์ฌ๋ ์ฌ์ฉ์๋ง ์ ๋ฐ์ดํธ ์์ ์ ์ํํ ์ ์๋๋ก ๋ณด์ฅํ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค. Spring Security๋ ๋ณด์ ์กฐ์น๋ฅผ ๊ตฌํํ๊ธฐ ์ํ ๊ฐ๋ ฅํ ํ๋ ์์ํฌ๋ฅผ ์ ๊ณตํฉ๋๋ค.
SecurityConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package org.studyeasy.SpringRestdemo.security; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.*; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { // ๋ฐ๋ชจ๋ฅผ ์ํ ์ธ๋ฉ๋ชจ๋ฆฌ ์ธ์ฆ auth.inMemoryAuthentication() .withUser("currentUser").password("{noop}password").roles("USER"); } @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .antMatchers("/albums/**").authenticated() .and() .httpBasic(); } } |
์ค๋ช :
- ์ธ์ฆ:
currentUser
๋ผ๋ ์ฌ์ฉ์๊ฐ ์๋ ์ธ๋ฉ๋ชจ๋ฆฌ ์ธ์ฆ์ ์ค์ ํฉ๋๋ค. - ๊ถํ ๋ถ์ฌ:
/albums/**
์๋์ ๋ชจ๋ ์๋ํฌ์ธํธ์ ๋ํด ์ธ์ฆ์ ์๊ตฌํฉ๋๋ค. - HTTP Basic: ๋จ์์ฑ์ ์ํด HTTP Basic์ ์ฌ์ฉํฉ๋๋ค. ํ๋ก๋์ ํ๊ฒฝ์์๋ JWT์ ๊ฐ์ ๋ณด๋ค ์์ ํ ๋ฐฉ๋ฒ์ ๊ณ ๋ คํ์ธ์.
Update Album API ํ ์คํธ
ํ ์คํธ๋ API๊ฐ ์์๋๋ก ์๋ํ๊ณ ๋ค์ํ ์๋๋ฆฌ์ค๋ฅผ ์ํํ๊ฒ ์ฒ๋ฆฌํ๋์ง ํ์ธํฉ๋๋ค.
์ ๋ฐ์ดํธ ์์ ์ํ
- ์ ์จ๋ฒ ์ถ๊ฐ:
- ์๋ํฌ์ธํธ: POST /albums
- ํ์ด๋ก๋:
1234{"name": "Travel","description": "Summer vacation photos"} - ์๋ต: 201 Created
- ์จ๋ฒ์ ์ฌ์ง ์ถ๊ฐ:
- ์๋ํฌ์ธํธ: POST /albums/2/photos
- ํ์ด๋ก๋:
1234{"url": "http://example.com/photo1.jpg","description": "Beach view"} - ์๋ต: 201 Created
- ์จ๋ฒ ์ธ๋ถ ์ ๋ณด ์
๋ฐ์ดํธ:
- ์๋ํฌ์ธํธ: PUT /albums/2/action
- ํ์ด๋ก๋:
1234{"name": "New Travel Album","description": "Updated description"} - ์๋ต: 204 No Content
- ์
๋ฐ์ดํธ ํ์ธ:
- ์๋ํฌ์ธํธ: GET /albums/2
- ์๋ต:
12345678910{"name": "New Travel Album","description": "Updated description","photos": [{"url": "http://example.com/photo1.jpg","description": "Beach view"}]}
์ค๋ช :
- ์จ๋ฒ ๋ฐ ์ฌ์ง ์ถ๊ฐ: ์ ๋ฐ์ดํธ ์์ ์ ํ ์คํธํ๊ธฐ ์ํ ์ด๊ธฐ ๋ฐ์ดํฐ๋ฅผ ์ค์ ํฉ๋๋ค.
- ์จ๋ฒ ์ ๋ฐ์ดํธ: ์จ๋ฒ์ ์ด๋ฆ๊ณผ ์ค๋ช ์ ๋ณ๊ฒฝํฉ๋๋ค.
- ํ์ธ: ์ ๋ฐ์ดํธ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ ์ฉ๋์๋์ง ํ์ธํฉ๋๋ค.
๊ฒฐ๋ก
Spring REST๋ฅผ ์ฌ์ฉํ์ฌ Update Album API๋ฅผ ๊ตฌํํ๋ ๊ฒ์ DTOs, ์ปจํธ๋กค๋ฌ, ์๋น์ค, ๋ฆฌํฌ์งํ ๋ฆฌ ๋ฐ ๋ณด์ ๊ตฌ์ฑ๊ณผ ๊ฐ์ ์ฌ๋ฌ ์ฃผ์ ๊ตฌ์ฑ ์์๋ฅผ ํฌํจํฉ๋๋ค. ๊ตฌ์กฐํ๋ ์ ๊ทผ ๋ฐฉ์์ ๋ฐ๋ฅด๊ณ ๋ชจ๋ฒ ์ฌ๋ก๋ฅผ ์ค์ํจ์ผ๋ก์จ ๊ฐ๋ฐ์๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ธฐ๋ฅ์ ํฅ์์ํค๋ ๊ฒฌ๊ณ ํ๊ณ ์์ ํ API๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
์ฃผ์ ์์ฌ์ :
- DTOs๋ ํ์์ ์ ๋๋ค: ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ์ ๋ณด์ฅํ๊ณ ํด๋ผ์ด์ธํธ์ ์๋ฒ ๊ฐ์ ์ ๋ณด ํ๋ฆ์ ์ ์ดํฉ๋๋ค.
- ๊ณ์ธตํ๋ ์ํคํ ์ฒ: ์ปจํธ๋กค๋ฌ, ์๋น์ค ๋ฐ ๋ฆฌํฌ์งํ ๋ฆฌ ๊ฐ์ ๊ด์ฌ์ฌ๋ฅผ ๋ถ๋ฆฌํ๋ฉด ์ ์ง ๋ณด์์ฑ์ด ํฅ์๋ฉ๋๋ค.
- ๋ณด์์ ์ต์ฐ์ ์ ๋๋ค: ์ ์ ํ ๊ถํ ๋ถ์ฌ ๊ฒ์ฌ๋ ๋ฌด๋จ ์์ ์ ๋ฐฉ์งํ๊ณ ์ฌ์ฉ์ ๋ฐ์ดํฐ๋ฅผ ๋ณดํธํฉ๋๋ค.
- ์ฒ ์ ํ ํ ์คํธ: ์ ๊ธฐ์ ์ธ ํ ์คํธ๋ API๊ฐ ์์๋๋ก ์๋ํ๊ณ ์ฃ์ง ์ผ์ด์ค๋ฅผ ํจ๊ณผ์ ์ผ๋ก ์ฒ๋ฆฌํ๋์ง ๋ณด์ฅํฉ๋๋ค.
Spring REST๋ก API๋ฅผ ๊ตฌ์ถํ๋ ์ฌ์ ์ ๊ฐ๋ฐ์์๊ฒ ํ์ฅ ๊ฐ๋ฅํ๊ณ ํจ์จ์ ์ธ ์น ์๋น์ค๋ฅผ ๋ง๋ค๊ธฐ ์ํ ๋๊ตฌ์ ์ง์์ ์ ๊ณตํฉ๋๋ค. ๋ ๋ง์ API๋ฅผ ํ๊ตฌํ๊ณ ๊ตฌํํด ๋๊ฐ์ ๋ฐ๋ผ ์ด ๊ฐ์ด๋์์ ๋ค๋ฃฌ ๊ธฐ๋ณธ ๊ฐ๋ ์ด ๊ท์คํ ์ฐธ์กฐ ์๋ฃ๋ก ํ์ฉ๋ ๊ฒ์ ๋๋ค.
SEO ํค์๋: Spring REST, Update Album API, Spring Boot ํํ ๋ฆฌ์ผ, RESTful API ๊ฐ๋ฐ, Spring Security, Java API ๊ฐ๋ฐ, Album ๊ด๋ฆฌ API, Spring Boot REST ์ปจํธ๋กค๋ฌ, Spring์ DTO, API ๊ถํ ๋ถ์ฌ, Spring Boot ํ๋ก์ ํธ ์ค์
์ฐธ๊ณ : ์ด ๊ธฐ์ฌ๋ AI์ ์ํด ์์ฑ๋์์ต๋๋ค.