commit
7ec90ff6ef
|
@ -1,16 +1,17 @@
|
|||
package org.baeldung.boot.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.boot.converter.GenericBigDecimalConverter;
|
||||
import org.baeldung.boot.converter.StringToAbstractEntityConverterFactory;
|
||||
import org.baeldung.boot.converter.StringToEmployeeConverter;
|
||||
import org.baeldung.boot.converter.StringToEnumConverterFactory;
|
||||
import org.baeldung.boot.converter.StringToEnumConverter;
|
||||
import org.baeldung.boot.web.resolver.HeaderVersionArgumentResolver;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.format.FormatterRegistry;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
|
@ -22,7 +23,8 @@ public class WebConfig implements WebMvcConfigurer {
|
|||
@Override
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
registry.addConverter(new StringToEmployeeConverter());
|
||||
registry.addConverterFactory(new StringToEnumConverterFactory());
|
||||
registry.addConverter(new StringToEnumConverter());
|
||||
registry.addConverterFactory(new StringToAbstractEntityConverterFactory());
|
||||
registry.addConverter(new GenericBigDecimalConverter());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package org.baeldung.boot.converter;
|
||||
|
||||
import org.baeldung.boot.domain.AbstractEntity;
|
||||
import org.baeldung.boot.domain.Bar;
|
||||
import org.baeldung.boot.domain.Foo;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.ConverterFactory;
|
||||
|
||||
public class StringToAbstractEntityConverterFactory implements ConverterFactory<String, AbstractEntity>{
|
||||
|
||||
@Override
|
||||
public <T extends AbstractEntity> Converter<String, T> getConverter(Class<T> targetClass) {
|
||||
|
||||
return new StringToAbstractEntityConverter<>(targetClass);
|
||||
}
|
||||
|
||||
|
||||
private static class StringToAbstractEntityConverter<T extends AbstractEntity> implements Converter<String, T> {
|
||||
|
||||
private Class<T> targetClass;
|
||||
|
||||
public StringToAbstractEntityConverter(Class<T> targetClass) {
|
||||
this.targetClass = targetClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T convert(String source) {
|
||||
long id = Long.parseLong(source);
|
||||
if(this.targetClass == Foo.class) {
|
||||
return (T) new Foo(id);
|
||||
}
|
||||
else if(this.targetClass == Bar.class) {
|
||||
return (T) new Bar(id);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package org.baeldung.boot.converter;
|
||||
|
||||
import org.baeldung.boot.domain.Modes;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
|
||||
public class StringToEnumConverter implements Converter<String, Modes> {
|
||||
|
||||
@Override
|
||||
public Modes convert(String from) {
|
||||
return Modes.valueOf(from);
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
package org.baeldung.boot.converter;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.ConverterFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {
|
||||
|
||||
private static class StringToEnumConverter<T extends Enum> implements Converter<String, T> {
|
||||
|
||||
private Class<T> enumType;
|
||||
|
||||
public StringToEnumConverter(Class<T> enumType) {
|
||||
this.enumType = enumType;
|
||||
}
|
||||
|
||||
public T convert(String source) {
|
||||
return (T) Enum.valueOf(this.enumType, source.trim());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Enum> Converter<String, T> getConverter(final Class<T> targetType) {
|
||||
return new StringToEnumConverter(targetType);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package org.baeldung.boot.converter.controller;
|
||||
|
||||
import org.baeldung.boot.domain.Bar;
|
||||
import org.baeldung.boot.domain.Foo;
|
||||
import org.baeldung.boot.domain.Modes;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/string-to-abstract")
|
||||
public class AbstractEntityController {
|
||||
|
||||
@GetMapping("/foo/{foo}")
|
||||
public ResponseEntity<Object> getStringToFoo(@PathVariable Foo foo) {
|
||||
return ResponseEntity.ok(foo);
|
||||
}
|
||||
|
||||
@GetMapping("/bar/{bar}")
|
||||
public ResponseEntity<Object> getStringToBar(@PathVariable Bar bar) {
|
||||
return ResponseEntity.ok(bar);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<Object> getStringToMode(@RequestParam("mode") Modes mode) {
|
||||
return ResponseEntity.ok(mode);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package org.baeldung.boot.domain;
|
||||
|
||||
public abstract class AbstractEntity {
|
||||
|
||||
long id;
|
||||
public AbstractEntity(long id){
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package org.baeldung.boot.domain;
|
||||
|
||||
public class Bar extends AbstractEntity {
|
||||
|
||||
private int value;
|
||||
|
||||
public Bar(long id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
public Bar(long id, int value) {
|
||||
super(id);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Bar [value=" + value + ", id=" + id + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package org.baeldung.boot.domain;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
|
||||
public class Foo extends AbstractEntity {
|
||||
|
||||
private String name;
|
||||
|
||||
public Foo(long id) {
|
||||
super(id);
|
||||
name = randomAlphanumeric(4);
|
||||
}
|
||||
|
||||
public Foo(long id, String name) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Foo [name=" + name + ", id=" + id + "]";
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue