Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

[TypeConverter] WebApplication, Formatter 적용 본문

인프런/[인프런] 스프링 MVC 2

[TypeConverter] WebApplication, Formatter 적용

Seung__ 2021. 10. 2. 14:13
  • WebApplication에 적용해보자.

1. Formmater 등록


  • addFormatters()를 사용하면됨.
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {

        //Sting<->Integer는 아래forammter에서 지원. converter가 우선순위 높음.
        //registry.addConverter(new StringToIntegerConverter());
        registry.addConverter(new StringToIpPortConverter());

        //registry.addConverter(new IntegerToStringConverter());
        registry.addConverter(new IpPortToStringConverter());

        //추가가
       registry.addFormatter(new MyNumberFormatter());
    }
}

 

2. 결과


  • 숫자 -> 문자

  • 문자 -> 숫자
    • http://localhost:8080/hello-v2?data=10,000
2021-10-02 14:10:53.143  INFO 31896 --- [nio-8080-exec-3] h.t.formatter.MyNumberFormatter
: text=10,000, locale=ko
data = 10000

 

3. GitHub : 211002 Formatter, WebApplication


 

GitHub - bsh6463/TypeConverter

Contribute to bsh6463/TypeConverter development by creating an account on GitHub.

github.com

 

Comments