From 043f6bade7aea7004907e2495b945b23dd7c9381 Mon Sep 17 00:00:00 2001 From: Erdem Date: Thu, 3 May 2018 23:27:12 +0300 Subject: [PATCH] BAEL-1746 added custom property editor implementation --- .../CreditCardRestController.java | 17 ---------- .../PropertyEditorApplication.java | 6 ++-- .../PropertyEditorRestController.java | 34 +++++++++++++++++++ .../{ => creditcard}/CreditCard.java | 2 +- .../{ => creditcard}/CreditCardEditor.java | 2 +- .../editor/CustomExoticTypeEditor.java | 23 +++++++++++++ .../exotictype/model/ExoticType.java | 14 ++++++++ .../src/main/resources/application.properties | 2 +- .../CreditCardEditorTest.java | 5 ++- 9 files changed, 81 insertions(+), 24 deletions(-) delete mode 100644 spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardRestController.java create mode 100644 spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorRestController.java rename spring-rest/src/main/java/com/baeldung/propertyeditor/{ => creditcard}/CreditCard.java (94%) rename spring-rest/src/main/java/com/baeldung/propertyeditor/{ => creditcard}/CreditCardEditor.java (96%) create mode 100644 spring-rest/src/main/java/com/baeldung/propertyeditor/exotictype/editor/CustomExoticTypeEditor.java create mode 100644 spring-rest/src/main/java/com/baeldung/propertyeditor/exotictype/model/ExoticType.java rename spring-rest/src/test/java/com/baeldung/propertyeditor/{ => creditcard}/CreditCardEditorTest.java (88%) diff --git a/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardRestController.java b/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardRestController.java deleted file mode 100644 index bf8e0586f8..0000000000 --- a/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardRestController.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.propertyeditor; - -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping(value = "/credit-card") -public class CreditCardRestController { - - @GetMapping(value = "/parse/{card-no}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public CreditCard parseCreditCardNumber(@PathVariable("card-no") CreditCard creditCard) { - return creditCard; - } -} diff --git a/spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java b/spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java index b0d75cd072..f98648c6b2 100644 --- a/spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java +++ b/spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java @@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class PropertyEditorApplication { - public static void main(String[] args) { - SpringApplication.run(PropertyEditorApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(PropertyEditorApplication.class, args); + } } diff --git a/spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorRestController.java b/spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorRestController.java new file mode 100644 index 0000000000..24f1b33471 --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/propertyeditor/PropertyEditorRestController.java @@ -0,0 +1,34 @@ +package com.baeldung.propertyeditor; + +import org.springframework.http.MediaType; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.InitBinder; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.propertyeditor.creditcard.CreditCard; +import com.baeldung.propertyeditor.exotictype.editor.CustomExoticTypeEditor; +import com.baeldung.propertyeditor.exotictype.model.ExoticType; + +@RestController +@RequestMapping(value = "/property-editor") +public class PropertyEditorRestController { + + @GetMapping(value = "/credit-card/{card-no}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public CreditCard parseCreditCardNumber(@PathVariable("card-no") CreditCard creditCard) { + return creditCard; + } + + @GetMapping(value = "/exotic-type/{value}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) + public ExoticType parseCreditCardNumber(@PathVariable("value") ExoticType exoticType) { + return exoticType; + } + + @InitBinder + public void initBinder(WebDataBinder binder) { + binder.registerCustomEditor(ExoticType.class, new CustomExoticTypeEditor()); + } + +} diff --git a/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCard.java b/spring-rest/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCard.java similarity index 94% rename from spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCard.java rename to spring-rest/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCard.java index 2b1fbb9b6c..f3adfb5d9f 100644 --- a/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCard.java +++ b/spring-rest/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCard.java @@ -1,4 +1,4 @@ -package com.baeldung.propertyeditor; +package com.baeldung.propertyeditor.creditcard; public class CreditCard { diff --git a/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardEditor.java b/spring-rest/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCardEditor.java similarity index 96% rename from spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardEditor.java rename to spring-rest/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCardEditor.java index 4b1374a76a..d413afee85 100644 --- a/spring-rest/src/main/java/com/baeldung/propertyeditor/CreditCardEditor.java +++ b/spring-rest/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCardEditor.java @@ -1,4 +1,4 @@ -package com.baeldung.propertyeditor; +package com.baeldung.propertyeditor.creditcard; import java.beans.PropertyEditorSupport; diff --git a/spring-rest/src/main/java/com/baeldung/propertyeditor/exotictype/editor/CustomExoticTypeEditor.java b/spring-rest/src/main/java/com/baeldung/propertyeditor/exotictype/editor/CustomExoticTypeEditor.java new file mode 100644 index 0000000000..08dbceae3d --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/propertyeditor/exotictype/editor/CustomExoticTypeEditor.java @@ -0,0 +1,23 @@ +package com.baeldung.propertyeditor.exotictype.editor; + +import java.beans.PropertyEditorSupport; + +import com.baeldung.propertyeditor.exotictype.model.ExoticType; + +public class CustomExoticTypeEditor extends PropertyEditorSupport { + + @Override + public String getAsText() { + ExoticType exoticType = (ExoticType) getValue(); + + return exoticType == null ? "" : exoticType.getName(); + } + + @Override + public void setAsText(String text) throws IllegalArgumentException { + ExoticType exoticType = new ExoticType(); + exoticType.setName(text.toUpperCase()); + + setValue(exoticType); + } +} diff --git a/spring-rest/src/main/java/com/baeldung/propertyeditor/exotictype/model/ExoticType.java b/spring-rest/src/main/java/com/baeldung/propertyeditor/exotictype/model/ExoticType.java new file mode 100644 index 0000000000..95ba95643d --- /dev/null +++ b/spring-rest/src/main/java/com/baeldung/propertyeditor/exotictype/model/ExoticType.java @@ -0,0 +1,14 @@ +package com.baeldung.propertyeditor.exotictype.model; + +public class ExoticType { + + private String name; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + +} diff --git a/spring-rest/src/main/resources/application.properties b/spring-rest/src/main/resources/application.properties index 300589f561..c4722f3c36 100644 --- a/spring-rest/src/main/resources/application.properties +++ b/spring-rest/src/main/resources/application.properties @@ -1,2 +1,2 @@ -server.port= 8082 +server.port= 8080 server.context-path=/spring-rest \ No newline at end of file diff --git a/spring-rest/src/test/java/com/baeldung/propertyeditor/CreditCardEditorTest.java b/spring-rest/src/test/java/com/baeldung/propertyeditor/creditcard/CreditCardEditorTest.java similarity index 88% rename from spring-rest/src/test/java/com/baeldung/propertyeditor/CreditCardEditorTest.java rename to spring-rest/src/test/java/com/baeldung/propertyeditor/creditcard/CreditCardEditorTest.java index b7da905f13..e87adbc712 100644 --- a/spring-rest/src/test/java/com/baeldung/propertyeditor/CreditCardEditorTest.java +++ b/spring-rest/src/test/java/com/baeldung/propertyeditor/creditcard/CreditCardEditorTest.java @@ -1,4 +1,4 @@ -package com.baeldung.propertyeditor; +package com.baeldung.propertyeditor.creditcard; import org.junit.Assert; import org.junit.Before; @@ -6,6 +6,9 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.runners.MockitoJUnitRunner; +import com.baeldung.propertyeditor.creditcard.CreditCard; +import com.baeldung.propertyeditor.creditcard.CreditCardEditor; + @RunWith(MockitoJUnitRunner.class) public class CreditCardEditorTest {