Skip to content

Commit

Permalink
섹션 10 | 포맷터를 지원하는 컨버전 서비스
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhealthy committed Mar 1, 2024
1 parent d5073fd commit bc3aaa6
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,66 @@ MyNumberFormatter - text=1,000, locale=ko_KR
MyNumberFormatter - object=1000, locale=ko_KR
```



## 포맷터를 지원하는 컨버전 서비스

컨버전 서비스에는 컨버터만 등록할 수 있고, 포맷터를 등록할 수 없다.(`DefaultConversionService`)
<u>하지만 포맷터를 지원하는 컨버전 서비스를 사용하면 컨버전 서비스에 포맷터를 추가할 수 있다.</u>

* 내부에서 어댑터 패턴을 사용해서 `Formatter``Converter`처럼 동작하도록 지원한다.
* `FormattingConversionService`는 포맷터를 지원하는 컨버전 서비스이다.



### 예제

[`FormattingConversionServiceTest`] - 테스트 코드

* 컨버전, 포맷터 서비스 등록 및 사용 - `DefaultFormattingConversionService`(`FormattingConversionService` + 부가 기능)

```java
package hello.typeconverter.fomatter;

import hello.typeconverter.converter.IpPortToStringConverter;
import hello.typeconverter.converter.StringToIpPortConverter;
import hello.typeconverter.type.IpPort;
import org.junit.jupiter.api.Test;
import org.springframework.format.support.DefaultFormattingConversionService;

import static org.assertj.core.api.Assertions.assertThat;

public class FormattingConversionServiceTest {

@Test
void formattingConversionService() {

DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();

// 컨버터 등록
conversionService.addConverter(new StringToIpPortConverter());
conversionService.addConverter(new IpPortToStringConverter());
// 포맷터 등록
conversionService.addFormatter(new MyNumberFormatter());


// 컨버터 사용
IpPort ipPort = conversionService.convert("127.0.0.1:8080", IpPort.class);
assertThat(ipPort).isEqualTo(new IpPort("127.0.0.1", 8080));
// 포맷터 사용
assertThat(conversionService.convert(1000, String.class)).isEqualTo("1,000");
assertThat(conversionService.convert("1,000", Long.class)).isEqualTo(1000);

}
}
```



#### DefaultFormattingConversionService 상속 관계

* FormattingConversionService는 ConversionService 관련 기능도 상속받기 때문에 결과적으로 컨버터도 포맷터도 모두 등록할 수 있다.
* 사용할 때는 컨버터, 포맷터 구분 없이 `ConversionService`가 제공하는 `convert()`를 사용하면 된다.



3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package hello.typeconverter.fomatter;

import hello.typeconverter.converter.IpPortToStringConverter;
import hello.typeconverter.converter.StringToIpPortConverter;
import hello.typeconverter.type.IpPort;
import org.junit.jupiter.api.Test;
import org.springframework.format.support.DefaultFormattingConversionService;

import static org.assertj.core.api.Assertions.assertThat;

public class FormattingConversionServiceTest {

@Test
void formattingConversionService() {

DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();

// 컨버터 등록
conversionService.addConverter(new StringToIpPortConverter());
conversionService.addConverter(new IpPortToStringConverter());
// 포맷터 등록
conversionService.addFormatter(new MyNumberFormatter());


// 컨버터 사용
IpPort ipPort = conversionService.convert("127.0.0.1:8080", IpPort.class);
assertThat(ipPort).isEqualTo(new IpPort("127.0.0.1", 8080));
// 포맷터 사용
assertThat(conversionService.convert(1000, String.class)).isEqualTo("1,000");
assertThat(conversionService.convert("1,000", Long.class)).isEqualTo(1000);

}
}

0 comments on commit bc3aaa6

Please sign in to comment.