diff --git a/typeconverter/src/main/java/hello/typeconverter/WebConfig.java b/typeconverter/src/main/java/hello/typeconverter/WebConfig.java new file mode 100644 index 00000000..716ea095 --- /dev/null +++ b/typeconverter/src/main/java/hello/typeconverter/WebConfig.java @@ -0,0 +1,21 @@ +package hello.typeconverter; + +import hello.typeconverter.converter.IntegerToStringConverter; +import hello.typeconverter.converter.IpPortToStringConverter; +import hello.typeconverter.converter.StringToIntegerConverter; +import hello.typeconverter.converter.StringToIpPortConverter; +import org.springframework.context.annotation.Configuration; +import org.springframework.format.FormatterRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + + @Override + public void addFormatters(FormatterRegistry registry) { + registry.addConverter(new StringToIntegerConverter()); + registry.addConverter(new IntegerToStringConverter()); + registry.addConverter(new StringToIpPortConverter()); + registry.addConverter(new IpPortToStringConverter()); + } +} diff --git a/typeconverter/src/main/java/hello/typeconverter/controller/HelloController.java b/typeconverter/src/main/java/hello/typeconverter/controller/HelloController.java index fcaddcf1..a6390b75 100644 --- a/typeconverter/src/main/java/hello/typeconverter/controller/HelloController.java +++ b/typeconverter/src/main/java/hello/typeconverter/controller/HelloController.java @@ -1,5 +1,6 @@ package hello.typeconverter.controller; +import hello.typeconverter.type.IpPort; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -23,4 +24,10 @@ public class HelloController { System.out.println("data = " + data); return "ok"; } + + @GetMapping("/ip-port") + public String ipPort(@RequestParam IpPort ipPort) { + System.out.println("ipPort = " + ipPort); + return "ok"; + } } diff --git a/typeconverter/src/main/java/hello/typeconverter/type/IpPort.java b/typeconverter/src/main/java/hello/typeconverter/type/IpPort.java index 02f43b72..713d6a76 100644 --- a/typeconverter/src/main/java/hello/typeconverter/type/IpPort.java +++ b/typeconverter/src/main/java/hello/typeconverter/type/IpPort.java @@ -2,9 +2,11 @@ package hello.typeconverter.type; import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.ToString; @Getter @EqualsAndHashCode +@ToString public class IpPort { private String ip; private int port;