BAEL-1746 added custom property editor implementation
This commit is contained in:
parent
ef481d19ee
commit
043f6bade7
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class PropertyEditorApplication {
|
public class PropertyEditorApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(PropertyEditorApplication.class, args);
|
SpringApplication.run(PropertyEditorApplication.class, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.propertyeditor;
|
package com.baeldung.propertyeditor.creditcard;
|
||||||
|
|
||||||
public class CreditCard {
|
public class CreditCard {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.propertyeditor;
|
package com.baeldung.propertyeditor.creditcard;
|
||||||
|
|
||||||
import java.beans.PropertyEditorSupport;
|
import java.beans.PropertyEditorSupport;
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,2 +1,2 @@
|
||||||
server.port= 8082
|
server.port= 8080
|
||||||
server.context-path=/spring-rest
|
server.context-path=/spring-rest
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.propertyeditor;
|
package com.baeldung.propertyeditor.creditcard;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
@ -6,6 +6,9 @@ import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.runners.MockitoJUnitRunner;
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import com.baeldung.propertyeditor.creditcard.CreditCard;
|
||||||
|
import com.baeldung.propertyeditor.creditcard.CreditCardEditor;
|
||||||
|
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class CreditCardEditorTest {
|
public class CreditCardEditorTest {
|
||||||
|
|
Loading…
Reference in New Issue