Skip to content

Commit 2176892

Browse files
committed
add support for plain EntityId<?> in web
1 parent 195359b commit 2176892

3 files changed

Lines changed: 110 additions & 1 deletion

File tree

src/main/java/it/aboutbits/springboot/toolbox/autoconfiguration/web/CustomTypeConfiguration.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import it.aboutbits.springboot.toolbox.jackson.CustomTypeDeserializer;
44
import it.aboutbits.springboot.toolbox.jackson.CustomTypeSerializer;
5+
import it.aboutbits.springboot.toolbox.jackson.EntityIdDeserializer;
56
import it.aboutbits.springboot.toolbox.type.CustomType;
7+
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
68
import it.aboutbits.springboot.toolbox.web.CustomTypePropertyEditor;
9+
import it.aboutbits.springboot.toolbox.web.EntityIdPropertyEditor;
710
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
811
import org.springframework.context.annotation.Bean;
912
import org.springframework.context.annotation.Configuration;
@@ -29,6 +32,7 @@ public void initBinder(WebDataBinder binder) {
2932
for (var clazz : types) {
3033
binder.registerCustomEditor(clazz, new CustomTypePropertyEditor<>(clazz));
3134
}
35+
binder.registerCustomEditor(EntityId.class, new EntityIdPropertyEditor());
3236
}
3337
}
3438

@@ -41,8 +45,11 @@ public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer(CustomTypeScanner co
4145
.toList()
4246
.toArray(new CustomTypeDeserializer[types.size()]);
4347

48+
4449
return builder -> builder
4550
.serializers(new CustomTypeSerializer())
46-
.deserializers(deserializers);
51+
.deserializers(deserializers)
52+
.deserializerByType(EntityId.class, new EntityIdDeserializer());
4753
}
54+
4855
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package it.aboutbits.springboot.toolbox.jackson;
2+
3+
import com.fasterxml.jackson.core.JsonParseException;
4+
import com.fasterxml.jackson.core.JsonParser;
5+
import com.fasterxml.jackson.databind.DeserializationContext;
6+
import com.fasterxml.jackson.databind.JsonDeserializer;
7+
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
8+
9+
import java.io.IOException;
10+
11+
public class EntityIdDeserializer extends JsonDeserializer<EntityId<?>> {
12+
/*
13+
This is needed because EntityId is just the interface. Jackson does not know what implementation to take.
14+
So we use this generic implementation.
15+
*/
16+
@Override
17+
public EntityId<?> deserialize(
18+
JsonParser jsonParser,
19+
DeserializationContext deserializationContext
20+
) throws IOException {
21+
// We need to do this because due to type erasure we don't actually know the wrapped type. So we try a number first and fallback to a string.
22+
try {
23+
var theValue = jsonParser.getLongValue();
24+
return new EntityId<Long>() {
25+
@Override
26+
public Long value() {
27+
return theValue;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return String.valueOf(value());
33+
}
34+
};
35+
} catch (JsonParseException e) {
36+
var theValue = jsonParser.getValueAsString();
37+
return new EntityId<String>() {
38+
@Override
39+
public String value() {
40+
return theValue;
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return theValue;
46+
}
47+
};
48+
}
49+
}
50+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package it.aboutbits.springboot.toolbox.web;
2+
3+
import it.aboutbits.springboot.toolbox.type.identity.EntityId;
4+
import org.springframework.lang.Nullable;
5+
6+
import java.beans.PropertyEditorSupport;
7+
8+
public final class EntityIdPropertyEditor extends PropertyEditorSupport {
9+
@SuppressWarnings("unchecked")
10+
@Override
11+
@Nullable
12+
public String getAsText() {
13+
var value = (EntityId<?>) getValue();
14+
15+
return value == null ? null : value.toString();
16+
}
17+
18+
@Override
19+
public void setAsText(String text) throws IllegalArgumentException {
20+
EntityId<?> value = null;
21+
try {
22+
var theValue = Long.parseLong(text);
23+
value = new EntityId<Long>() {
24+
@Override
25+
public Long value() {
26+
return theValue;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return String.valueOf(value());
32+
}
33+
};
34+
} catch (NumberFormatException e) {
35+
value = new EntityId<String>() {
36+
@Override
37+
public String value() {
38+
return text;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return value();
44+
}
45+
};
46+
}
47+
48+
setValue(
49+
value
50+
);
51+
}
52+
}

0 commit comments

Comments
 (0)