Skip to content

Commit

Permalink
Add InstanceEvents to Jackson module
Browse files Browse the repository at this point in the history
  • Loading branch information
srempfer committed Apr 25, 2020
1 parent 9b72083 commit c46c3da
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,9 @@

import java.time.Instant;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import de.codecentric.boot.admin.server.domain.values.InstanceId;

/**
Expand All @@ -36,7 +39,9 @@ public InstanceDeregisteredEvent(InstanceId instance, long version) {
this(instance, version, Instant.now());
}

public InstanceDeregisteredEvent(InstanceId instance, long version, Instant timestamp) {
@JsonCreator
public InstanceDeregisteredEvent(@JsonProperty("instance") InstanceId instance,
@JsonProperty("version") long version, @JsonProperty("timestamp") Instant timestamp) {
super(instance, version, "DEREGISTERED", timestamp);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,9 @@

import java.time.Instant;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import de.codecentric.boot.admin.server.domain.values.Endpoints;
import de.codecentric.boot.admin.server.domain.values.InstanceId;

Expand All @@ -39,7 +42,10 @@ public InstanceEndpointsDetectedEvent(InstanceId instance, long version, Endpoin
this(instance, version, Instant.now(), endpoints);
}

public InstanceEndpointsDetectedEvent(InstanceId instance, long version, Instant timestamp, Endpoints endpoints) {
@JsonCreator
public InstanceEndpointsDetectedEvent(@JsonProperty("instance") InstanceId instance,
@JsonProperty("version") long version, @JsonProperty("timestamp") Instant timestamp,
@JsonProperty("endpoints") Endpoints endpoints) {
super(instance, version, "ENDPOINTS_DETECTED", timestamp);
this.endpoints = endpoints;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,9 @@

import java.time.Instant;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import de.codecentric.boot.admin.server.domain.values.Info;
import de.codecentric.boot.admin.server.domain.values.InstanceId;

Expand All @@ -39,7 +42,10 @@ public InstanceInfoChangedEvent(InstanceId instance, long version, Info info) {
this(instance, version, Instant.now(), info);
}

public InstanceInfoChangedEvent(InstanceId instance, long version, Instant timestamp, Info info) {
@JsonCreator
public InstanceInfoChangedEvent(@JsonProperty("instance") InstanceId instance,
@JsonProperty("version") long version, @JsonProperty("timestamp") Instant timestamp,
@JsonProperty("info") Info info) {
super(instance, version, "INFO_CHANGED", timestamp);
this.info = info;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,9 @@

import java.time.Instant;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import de.codecentric.boot.admin.server.domain.values.InstanceId;
import de.codecentric.boot.admin.server.domain.values.Registration;

Expand All @@ -39,7 +42,9 @@ public InstanceRegisteredEvent(InstanceId instance, long version, Registration r
this(instance, version, Instant.now(), registration);
}

public InstanceRegisteredEvent(InstanceId instance, long version, Instant timestamp, Registration registration) {
@JsonCreator
public InstanceRegisteredEvent(@JsonProperty("instance") InstanceId instance, @JsonProperty("version") long version,
@JsonProperty("timestamp") Instant timestamp, @JsonProperty("registration") Registration registration) {
super(instance, version, "REGISTERED", timestamp);
this.registration = registration;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,9 @@

import java.time.Instant;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import de.codecentric.boot.admin.server.domain.values.InstanceId;
import de.codecentric.boot.admin.server.domain.values.Registration;

Expand All @@ -39,8 +42,10 @@ public InstanceRegistrationUpdatedEvent(InstanceId instance, long version, Regis
this(instance, version, Instant.now(), registration);
}

public InstanceRegistrationUpdatedEvent(InstanceId instance, long version, Instant timestamp,
Registration registration) {
@JsonCreator
public InstanceRegistrationUpdatedEvent(@JsonProperty("instance") InstanceId instance,
@JsonProperty("version") long version, @JsonProperty("timestamp") Instant timestamp,
@JsonProperty("registration") Registration registration) {
super(instance, version, "REGISTRATION_UPDATED", timestamp);
this.registration = registration;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,9 @@

import java.time.Instant;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import de.codecentric.boot.admin.server.domain.values.InstanceId;
import de.codecentric.boot.admin.server.domain.values.StatusInfo;

Expand All @@ -39,7 +42,10 @@ public InstanceStatusChangedEvent(InstanceId instance, long version, StatusInfo
this(instance, version, Instant.now(), statusInfo);
}

public InstanceStatusChangedEvent(InstanceId instance, long version, Instant timestamp, StatusInfo statusInfo) {
@JsonCreator
public InstanceStatusChangedEvent(@JsonProperty("instance") InstanceId instance,
@JsonProperty("version") long version, @JsonProperty("timestamp") Instant timestamp,
@JsonProperty("statusInfo") StatusInfo statusInfo) {
super(instance, version, "STATUS_CHANGED", timestamp);
this.statusInfo = statusInfo;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,8 @@

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.util.Assert;

@lombok.Data
Expand Down Expand Up @@ -52,7 +54,8 @@ private Endpoint(String id, String url) {
this.url = url;
}

public static Endpoint of(String id, String url) {
@JsonCreator
public static Endpoint of(@JsonProperty("id") String id, @JsonProperty("url") String url) {
return new Endpoint(id, url);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,8 @@

import javax.annotation.Nullable;

import com.fasterxml.jackson.annotation.JsonCreator;

import static java.util.stream.Collectors.toMap;

@lombok.EqualsAndHashCode
Expand Down Expand Up @@ -68,6 +70,7 @@ public static Endpoints single(String id, String url) {
return new Endpoints(Collections.singletonList(Endpoint.of(id, url)));
}

@JsonCreator
public static Endpoints of(@Nullable Collection<Endpoint> endpoints) {
if (endpoints == null || endpoints.isEmpty()) {
return empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import javax.annotation.Nullable;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonCreator;

/**
* Represents the info fetched from the info actuator endpoint
Expand All @@ -46,6 +47,7 @@ private Info(Map<String, Object> values) {
}
}

@JsonCreator
public static Info from(@Nullable Map<String, Object> values) {
if (values == null || values.isEmpty()) {
return empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,9 @@

import javax.annotation.Nullable;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.util.Assert;

import static java.util.Arrays.asList;
Expand Down Expand Up @@ -63,7 +65,9 @@ private StatusInfo(String status, @Nullable Map<String, ?> details) {
this.details = (details != null) ? new HashMap<>(details) : Collections.emptyMap();
}

public static StatusInfo valueOf(String statusCode, @Nullable Map<String, ?> details) {
@JsonCreator
public static StatusInfo valueOf(@JsonProperty("status") String statusCode,
@JsonProperty("details") @Nullable Map<String, ?> details) {
return new StatusInfo(statusCode, details);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@

import com.fasterxml.jackson.databind.module.SimpleModule;

import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.values.Registration;

/**
* Jackson module for Spring Boot Admin Server.
* Jackson module for Spring Boot Admin Server. <br>
* In order to use this module just add this modules into your ObjectMapper configuration.
* <pre>
* ObjectMapper mapper = new ObjectMapper();
* mapper.registerModule(new AdminServerModule());
* mapper.registerModule(new JavaTimeModule());
* </pre>
*
* @author Stefan Rempfer
*/
Expand All @@ -37,6 +44,7 @@ public AdminServerModule(String[] metadataKeyPatterns) {

addDeserializer(Registration.class, new RegistrationDeserializer());
setSerializerModifier(new RegistrationBeanSerializerModifier(new SanitizingMapSerializer(metadataKeyPatterns)));
setMixInAnnotation(InstanceEvent.class, InstanceEventMixin.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.codecentric.boot.admin.server.utils.jackson;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import de.codecentric.boot.admin.server.domain.events.InstanceDeregisteredEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceEndpointsDetectedEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceInfoChangedEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceRegisteredEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceRegistrationUpdatedEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;

/**
* Jackson Mixin class helps in serialize/deserialize {@link InstanceEvent}s.
*
* @author Stefan Rempfer
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(value = InstanceEndpointsDetectedEvent.class, name = "ENDPOINTS_DETECTED"),
@JsonSubTypes.Type(value = InstanceRegistrationUpdatedEvent.class, name = "REGISTRATION_UPDATED"),
@JsonSubTypes.Type(value = InstanceInfoChangedEvent.class, name = "INFO_CHANGED"),
@JsonSubTypes.Type(value = InstanceDeregisteredEvent.class, name = "DEREGISTERED"),
@JsonSubTypes.Type(value = InstanceRegisteredEvent.class, name = "REGISTERED"),
@JsonSubTypes.Type(value = InstanceStatusChangedEvent.class, name = "STATUS_CHANGED") })
public abstract class InstanceEventMixin {

}
Loading

0 comments on commit c46c3da

Please sign in to comment.