Problem

Spring Data REST ๋Š” entity type ์˜ name ์„ ๊ธฐ๋ฐ˜์œผ๋กœ endpoints ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜, ์˜์–ด๋Š” ๋งค์šฐ ํ˜ผ์žก์Šค๋Ÿฌ์šด ์–ธ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„ํ˜น โ€œGoose, Geeseโ€ ์™€ ๊ฐ™์€ ๋ถˆ๊ทœ์น™ํ•œ plural form ์ด ์กด์žฌํ•œ๋‹ค. Spring Data REST ๋Š” ์ด๊ฒƒ๊นŒ์ง€ ์ง€์›ํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์ด์™€ ๊ฐ™์€ noun ์„ ์‚ฌ์šฉํ•  ๋•Œ์—๋‚˜, entity type ๊ณผ ๋‹ค๋ฅธ ๋‹จ์–ด๋กœ endpoint ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์„ ๋•Œ์—๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ?

Solution
@RepositoryRestResource(path="members")
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {}

์œ„์™€ ๊ฐ™์ด @RepositoryRestResource annotation ์„ ์‚ฌ์šฉํ•˜์—ฌ explicit ํ•˜๊ฒŒ endpoint name ์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

Pagination

default ๋กœ, Spring Data REST ๋Š” first 20 elements ๋ฅผ return ํ•œ๋‹ค. ์ฆ‰, Page size ๋Š” 20 ์ธ ๊ฒƒ์ด๋‹ค.

http://localhost:8080/employees?page=0 ๊ณผ ๊ฐ™์ด, URL ์—์„œ ? ๋’ค์— ๋ถ™๋Š” key=value ํ˜•ํƒœ์˜ data ์ธ Query Parameter ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ different page ๋ฅผ navigate ํ•  ์ˆ˜ ์žˆ๋‹ค. page ๋Š” 0 ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

spring.data.rest.base-path, spring.data.rest.default-page-size, spring.data.rest.max-page-size ๋“ฑ, spring.data.rest.* ์„ application.properties file ์— configure ํ•˜๋Š” ๊ฒƒ์„ ํ†ตํ•˜์—ฌ Pagination ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ Spring Document ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Sorting

entity ์˜ property name ์œผ๋กœ sorting ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. lastName ์„ ๊ธฐ์ค€์œผ๋กœ sorting ์„ ์›ํ•˜๋ฉด,

http://localhost:8080/employees?sort=lastName

๊ณผ ๊ฐ™์ด query parameter sort ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , default ๋Š” ascending order ์ด๊ธฐ ๋•Œ๋ฌธ์—,

http://localhost:8080/employees?sort=lastName,desc

๋ฅผ ํ†ตํ•˜์—ฌ descending order ๋กœ๋„ sorting ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ last name ์„ ๊ธฐ์ค€์œผ๋กœ sorring ํ•˜๊ณ , ๊ทธ ์•ˆ์—์„œ first name ์œผ๋กœ sorting ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด,

http://localhost:8080/employees?sort=lastName,firstNamem,asc

last name ์€ ๋‚ด๋ฆผ์ฐจ์ˆœ, ๊ทธ ๋‹ค์Œ first name ์€ ์˜ค๋ฆ„์ฐจ์ˆœ์ด๋ผ๋ฉด,

http://localhost:8080/employees?sort=lastName,desc&sort=firstName,asc

๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.

Test - Configuration

๋จผ์ € @RepositoryRestResource annotation ์„ ํ†ตํ•˜์—ฌ endpoint ๋ฅผ members ๋กœ ๋ณ€๊ฒฝํ•ด๋ณด์ž.

response body ๋ฅผ ๋ณด๋ฉด, HATEOAS ์— ์˜ํ•˜์—ฌ page ์— ๋Œ€ํ•œ meta-data ๋„ ๊ฐ™์ด ๋„˜์–ด์˜จ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. size ๊ฐ€ 20 ์ด๋ผ๋Š” ๊ฒƒ์€ ์•„๊นŒ ์–ธ๊ธ‰ํ•œ page size ๊ฐ€ default ๋Š” 20 ์ด๋ผ๋Š” ๊ฒƒ์„ ์ฆ๋ช…ํ•œ๋‹ค.

Test - Pagination

application.properties ์— spring.data.rest.default-page-size=3 ์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ๋‹ค์‹œ request ๋ฅผ ๋ณด๋‚ด๋ฉด, ์œ„์™€ ๊ฐ™์ด size ๋Š” 3, totalElements ๋Š” 4 ์ด๋ฏ€๋กœ, totalPages ๊ฐ€ 2 ๊ฐ€ ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ first 3 elements ๊ฐ€ return ๋˜๋ฏ€๋กœ, query parameter ๋ฅผ ํ†ตํ•˜์—ฌ page ๊ฐ„์˜ navigation ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋‹ˆ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด first 4 elements ๋กœ ๊ฐ•์ œํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

http://localhost:8080/magic-api/members?page=0&size=4
Test - Sorting

๋งˆ์ง€๋ง‰์œผ๋กœ Sorting ์— ๋Œ€ํ•˜์—ฌ test ํ•ด๋ณด์ž. query parameter sort ๋ฅผ ์ด์šฉํ•˜์—ฌ firstName ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๋ฉด, ๊ณผ ๊ฐ™์ด ๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ๋‚˜์˜จ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๊ณ , firstName ์ด "Arne" ์ธ entity ๋ฅผ ํ•˜๋‚˜ ๋” POST ํ•˜๊ณ , last name ๋„ ๊ฐ™์ด sorting ์— ํฌํ•จ์‹œํ‚ค๋ฉด,

์œ„์™€ ๊ฐ™์ด ๋™์ผํ•œ first name ์„ ๊ธฐ์ค€์œผ๋กœ ๋จผ์ € sorting ํ•˜๊ณ , ์ดํ›„์— ๊ฐ™์€ first name ์— ๋Œ€ํ•˜์—ฌ ๋‹ค์‹œ last name ์„ ๊ธฐ์ค€์œผ๋กœ sorting ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.