17
17
package de .codecentric .boot .admin .server .utils .jackson ;
18
18
19
19
import java .io .IOException ;
20
+ import java .util .HashMap ;
20
21
import java .util .Iterator ;
21
22
import java .util .Map ;
22
23
23
24
import com .fasterxml .jackson .core .JsonParser ;
25
+ import com .fasterxml .jackson .core .JsonToken ;
24
26
import com .fasterxml .jackson .databind .DeserializationContext ;
25
27
import com .fasterxml .jackson .databind .JsonNode ;
26
28
import com .fasterxml .jackson .databind .deser .std .StdDeserializer ;
27
29
28
30
import de .codecentric .boot .admin .server .domain .values .Registration ;
29
31
32
+ import org .springframework .util .ObjectUtils ;
33
+
30
34
public class RegistrationDeserializer extends StdDeserializer <Registration > {
31
35
32
36
private static final long serialVersionUID = 1L ;
@@ -38,6 +42,7 @@ public RegistrationDeserializer() {
38
42
@ Override
39
43
public Registration deserialize (JsonParser p , DeserializationContext ctxt ) throws IOException {
40
44
JsonNode node = p .readValueAsTree ();
45
+ Map <String , String > normalizedKvPair = getNormalizedKvPair (node );
41
46
Registration .Builder builder = Registration .builder ();
42
47
43
48
if (node .hasNonNull ("name" )) {
@@ -48,14 +53,14 @@ public Registration deserialize(JsonParser p, DeserializationContext ctxt) throw
48
53
builder .healthUrl (url .replaceFirst ("/+$" , "" ) + "/health" ).managementUrl (url );
49
54
}
50
55
else {
51
- if (node . hasNonNull ( "healthUrl" )) {
52
- builder .healthUrl (node .get ("healthUrl" ). asText ( ));
56
+ if (! ObjectUtils . isEmpty ( normalizedKvPair . get ( "healthurl" ) )) {
57
+ builder .healthUrl (normalizedKvPair .get ("healthurl" ));
53
58
}
54
- if (node . hasNonNull ( "managementUrl" )) {
55
- builder .managementUrl (node .get ("managementUrl" ). asText ( ));
59
+ if (! ObjectUtils . isEmpty ( normalizedKvPair . get ( "managementurl" ) )) {
60
+ builder .managementUrl (normalizedKvPair .get ("managementurl" ));
56
61
}
57
- if (node . hasNonNull ( "serviceUrl" )) {
58
- builder .serviceUrl (node .get ("serviceUrl" ). asText ( ));
62
+ if (! ObjectUtils . isEmpty ( normalizedKvPair . get ( "serviceurl" ) )) {
63
+ builder .serviceUrl (normalizedKvPair .get ("serviceurl" ));
59
64
}
60
65
}
61
66
@@ -74,4 +79,21 @@ public Registration deserialize(JsonParser p, DeserializationContext ctxt) throw
74
79
return builder .build ();
75
80
}
76
81
82
+ private Map <String , String > getNormalizedKvPair (JsonNode jn ) throws IOException {
83
+ Map <String , String > normalizedKvPair = new HashMap <>();
84
+ JsonParser jp = jn .traverse ();
85
+ while (!jp .isClosed ()) {
86
+ if (jp .nextToken () == JsonToken .FIELD_NAME ) {
87
+ String fieldName = jp .currentName ();
88
+ if (!ObjectUtils .isEmpty (fieldName )) {
89
+ JsonToken jsonValueToken = jp .nextValue ();
90
+ if (jsonValueToken == JsonToken .VALUE_STRING ) {
91
+ normalizedKvPair .putIfAbsent (fieldName .replaceAll ("[_-]" , "" ).toLowerCase (), jp .getValueAsString ());
92
+ }
93
+ }
94
+ }
95
+ }
96
+ return normalizedKvPair ;
97
+ }
98
+
77
99
}
0 commit comments