-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: create discriminator example (#771)
- Loading branch information
Showing
7 changed files
with
333 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...le/src/main/java/io/github/springwolf/examples/kafka/consumers/DiscriminatorConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.examples.kafka.consumers; | ||
|
||
import io.github.springwolf.examples.kafka.dtos.discriminator.VehicleBase; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class DiscriminatorConsumer { | ||
|
||
@KafkaListener(topics = "vehicle-topic") | ||
public void receiveExamplePayload(VehicleBase payload) { | ||
log.info("Received new message in vehicle-topic: {}", payload.toString()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ple/src/main/java/io/github/springwolf/examples/kafka/dtos/discriminator/VehicleBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.examples.kafka.dtos.discriminator; | ||
|
||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import io.swagger.v3.oas.annotations.media.DiscriminatorMapping; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "vehicleType") | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = VehicleElectricPayloadDto.class, name = "VehicleElectricPayloadDto"), | ||
@JsonSubTypes.Type(value = VehicleGasolinePayloadDto.class, name = "VehicleGasolinePayloadDto"), | ||
}) | ||
@Schema( | ||
subTypes = {VehicleElectricPayloadDto.class}, | ||
discriminatorProperty = "vehicleType", | ||
discriminatorMapping = { | ||
@DiscriminatorMapping(value = "VehicleElectricPayloadDto", schema = VehicleElectricPayloadDto.class), | ||
@DiscriminatorMapping(value = "VehicleGasolinePayloadDto", schema = VehicleGasolinePayloadDto.class), | ||
}, | ||
description = "Demonstrates the use of discriminator for polymorphic deserialization (not publishable)") | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public abstract class VehicleBase { | ||
private String vehicleType; // added for discriminator | ||
|
||
private String powerSource; | ||
private int topSpeed; | ||
} |
25 changes: 25 additions & 0 deletions
25
...ava/io/github/springwolf/examples/kafka/dtos/discriminator/VehicleElectricPayloadDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.examples.kafka.dtos.discriminator; | ||
|
||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
|
||
@JsonTypeName("VehicleElectricPayloadDto") | ||
@Schema( | ||
description = "Electric vehicle implementation of VehicleBase" | ||
|
||
// Alternative, when you do not want to add the discriminator property vehicleType to VehicleBase | ||
// allOf = {VehicleBase.class} | ||
) | ||
@NoArgsConstructor | ||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
@AllArgsConstructor | ||
public class VehicleElectricPayloadDto extends VehicleBase { | ||
private int batteryCapacity; | ||
private int chargeTime; | ||
} |
24 changes: 24 additions & 0 deletions
24
...ava/io/github/springwolf/examples/kafka/dtos/discriminator/VehicleGasolinePayloadDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.springwolf.examples.kafka.dtos.discriminator; | ||
|
||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
|
||
@JsonTypeName("VehicleGasolinePayloadDto") | ||
@Schema( | ||
description = "Gasoline vehicle implementation of VehicleBase" | ||
|
||
// Alternative, when you do not want to add the discriminator property vehicleType to VehicleBase | ||
// allOf = {VehicleBase.class} | ||
) | ||
@NoArgsConstructor | ||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
@AllArgsConstructor | ||
public class VehicleGasolinePayloadDto extends VehicleBase { | ||
private int fuelCapacity; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters