BAEL-1746 added custom property editor implementation

This commit is contained in:
Erdem 2018-05-03 23:27:12 +03:00
parent ef481d19ee
commit 043f6bade7
9 changed files with 81 additions and 24 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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());
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.propertyeditor;
package com.baeldung.propertyeditor.creditcard;
public class CreditCard {

View File

@ -1,4 +1,4 @@
package com.baeldung.propertyeditor;
package com.baeldung.propertyeditor.creditcard;
import java.beans.PropertyEditorSupport;

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -1,2 +1,2 @@
server.port= 8082
server.port= 8080
server.context-path=/spring-rest

View File

@ -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 {