Goal
- UriComponents
- UriComponentsBuilder
- ServletUriComponenetsBuilder
UriComponenets
Represents an immutable collection of URI components, mapping component type to String values. Contains convenience getters for all components. Effectively similar to URI, but with more powerful encoding options and support for URI template variables.
UriComponenets는 각 URI 부분들의 불변의 집합이다.
UriComponentsBuilder
Builder for UriComponenets
Set the various URI components through the respective methods
Build the UriComponenets instance with the build() method
UriComponenetsBuilder는 UriCponenets를 만들기 위해서 build를 해주는 Type이다.
UriComponents uriComponents =
UriComponentsBuilder.newInstance()
.scheme("http").host("example.com").path("/hotels/{hotel}/bookings/{booking}").build()
.expand("42", "21")
.encode();
UriComponents는 UriComponenetsBuilder를 통해서 만들어진다. newInstance()를 통해 URI를 새롭게 만들 수 있다.
UriComponents uriComponents =
UriComponentsBuilder.fromUriString("http://example.com/hotels/{hotel}/bookings/{booking}").build();
URI uri = uriComponents.expand("42", "21").encode().toUri();
uriComponenets.toUri()를 통해 URI 주소로 변환이 가능하다.
ServletComponenetsBuilder
UriComponentsBuilder with additional static factory methods to create links based on the current HttpServletRequest.
ServletUriComponenetsBuilder extends UriComponenetsBuilder
UriComponenetsBuilder를 상속하기 때문에 다양한 메소드를 구현할 수 있다.
current HttpServletRequest에 특화되어서 link를 만들어 주기 때문에 fromCurrent- 에 관한 명령어들이 다수 있고 따라서 빠르고 간결하게 Uri를 만들 수 있다.
URI location = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(savedUser.getId()).toUri();
이전 예시들에서는 .build()와 expand()를 따로 썼지만, buildAndExpand()로 한번에 사용할 수 있다.
또한 fromCurrentRequest()로 간결하고 쉽게 Uri를 만들 수 있다.