File tree Expand file tree Collapse file tree
spring-boot-ops/src/main/java/org/baeldung/boot Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package org .baeldung .boot .config ;
22
3+ import java .util .List ;
4+
35import org .baeldung .boot .converter .GenericBigDecimalConverter ;
6+ import org .baeldung .boot .converter .StringToAbstractEntityConverterFactory ;
47import org .baeldung .boot .converter .StringToEmployeeConverter ;
5- import org .baeldung .boot .converter .StringToEnumConverterFactory ;
8+ import org .baeldung .boot .converter .StringToEnumConverter ;
69import org .baeldung .boot .web .resolver .HeaderVersionArgumentResolver ;
710import org .springframework .context .annotation .Configuration ;
811import org .springframework .format .FormatterRegistry ;
912import org .springframework .web .method .support .HandlerMethodArgumentResolver ;
1013import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
1114
12- import java .util .List ;
13-
1415@ Configuration
1516public class WebConfig implements WebMvcConfigurer {
1617
@@ -22,7 +23,8 @@ public void addArgumentResolvers(final List<HandlerMethodArgumentResolver> argum
2223 @ Override
2324 public void addFormatters (FormatterRegistry registry ) {
2425 registry .addConverter (new StringToEmployeeConverter ());
25- registry .addConverterFactory (new StringToEnumConverterFactory ());
26+ registry .addConverter (new StringToEnumConverter ());
27+ registry .addConverterFactory (new StringToAbstractEntityConverterFactory ());
2628 registry .addConverter (new GenericBigDecimalConverter ());
2729 }
2830}
Original file line number Diff line number Diff line change 1+ package org .baeldung .boot .converter ;
2+
3+ import org .baeldung .boot .domain .AbstractEntity ;
4+ import org .baeldung .boot .domain .Bar ;
5+ import org .baeldung .boot .domain .Foo ;
6+ import org .springframework .core .convert .converter .Converter ;
7+ import org .springframework .core .convert .converter .ConverterFactory ;
8+
9+ public class StringToAbstractEntityConverterFactory implements ConverterFactory <String , AbstractEntity >{
10+
11+ @ Override
12+ public <T extends AbstractEntity > Converter <String , T > getConverter (Class <T > targetClass ) {
13+
14+ return new StringToAbstractEntityConverter <>(targetClass );
15+ }
16+
17+
18+ private static class StringToAbstractEntityConverter <T extends AbstractEntity > implements Converter <String , T > {
19+
20+ private Class <T > targetClass ;
21+
22+ public StringToAbstractEntityConverter (Class <T > targetClass ) {
23+ this .targetClass = targetClass ;
24+ }
25+
26+ @ Override
27+ public T convert (String source ) {
28+ long id = Long .parseLong (source );
29+ if (this .targetClass == Foo .class ) {
30+ return (T ) new Foo (id );
31+ }
32+ else if (this .targetClass == Bar .class ) {
33+ return (T ) new Bar (id );
34+ } else {
35+ return null ;
36+ }
37+ }
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .boot .converter ;
2+
3+ import org .baeldung .boot .domain .Modes ;
4+ import org .springframework .core .convert .converter .Converter ;
5+
6+ public class StringToEnumConverter implements Converter <String , Modes > {
7+
8+ @ Override
9+ public Modes convert (String from ) {
10+ return Modes .valueOf (from );
11+ }
12+ }
Load diff This file was deleted.
Original file line number Diff line number Diff line change 1+ package org .baeldung .boot .converter .controller ;
2+
3+ import org .baeldung .boot .domain .Bar ;
4+ import org .baeldung .boot .domain .Foo ;
5+ import org .baeldung .boot .domain .Modes ;
6+ import org .springframework .http .ResponseEntity ;
7+ import org .springframework .web .bind .annotation .GetMapping ;
8+ import org .springframework .web .bind .annotation .PathVariable ;
9+ import org .springframework .web .bind .annotation .RequestMapping ;
10+ import org .springframework .web .bind .annotation .RequestParam ;
11+ import org .springframework .web .bind .annotation .RestController ;
12+
13+ @ RestController
14+ @ RequestMapping ("/string-to-abstract" )
15+ public class AbstractEntityController {
16+
17+ @ GetMapping ("/foo/{foo}" )
18+ public ResponseEntity <Object > getStringToFoo (@ PathVariable Foo foo ) {
19+ return ResponseEntity .ok (foo );
20+ }
21+
22+ @ GetMapping ("/bar/{bar}" )
23+ public ResponseEntity <Object > getStringToBar (@ PathVariable Bar bar ) {
24+ return ResponseEntity .ok (bar );
25+ }
26+
27+ @ GetMapping
28+ public ResponseEntity <Object > getStringToMode (@ RequestParam ("mode" ) Modes mode ) {
29+ return ResponseEntity .ok (mode );
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .boot .domain ;
2+
3+ public abstract class AbstractEntity {
4+
5+ long id ;
6+ public AbstractEntity (long id ){
7+ this .id = id ;
8+ }
9+
10+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .boot .domain ;
2+
3+ public class Bar extends AbstractEntity {
4+
5+ private int value ;
6+
7+ public Bar (long id ) {
8+ super (id );
9+ }
10+
11+ public Bar (long id , int value ) {
12+ super (id );
13+ this .value = value ;
14+ }
15+
16+ public int getValue () {
17+ return value ;
18+ }
19+
20+ public void setValue (int value ) {
21+ this .value = value ;
22+ }
23+
24+ @ Override
25+ public String toString () {
26+ return "Bar [value=" + value + ", id=" + id + "]" ;
27+ }
28+
29+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung .boot .domain ;
2+
3+ import static org .apache .commons .lang3 .RandomStringUtils .randomAlphanumeric ;
4+ public class Foo extends AbstractEntity {
5+
6+ private String name ;
7+
8+ public Foo (long id ) {
9+ super (id );
10+ name = randomAlphanumeric (4 );
11+ }
12+
13+ public Foo (long id , String name ) {
14+ super (id );
15+ this .name = name ;
16+ }
17+
18+ public String getName () {
19+ return name ;
20+ }
21+
22+ public void setName (String name ) {
23+ this .name = name ;
24+ }
25+
26+ @ Override
27+ public String toString () {
28+ return "Foo [name=" + name + ", id=" + id + "]" ;
29+ }
30+
31+ }
You can’t perform that action at this time.
0 commit comments