我有一个quarkus REST api,它接受JSON字符串(各种类型)作为事件,然后将它们传播到KAFKA主题。
我现在想将可能的JSON字符串的结构放在openAPI合约中(无需自己手动键入)。
我已经找到了@requestbody标记,它允许我指定一个结构,但我有多种类型的事件,它们都必须指向同一个Kafka主题。
例子:
@POST @Path("/student") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Tag(name = "Student Events Service") @Operation(summary = "Accepts student type events", description = "Accepts student type events") @APIResponse(responseCode = "200", description = "Event sent successfully") @APIResponse(responseCode = "400", description = "The JSON packet sent did not conform to any known message or the primary key or shared data in the packet is invalid. \n " + "See Response Body for detail. - " + "Error code 400-1 = Bad message structure or unknown message. Log error and continue to next message - " + "Error code 400-2 = Bad primary key. Fix data (client or server side) and retry later - " + "Error code 400-3 = Invalid shared data. Fix shared data and retry later", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))) @APIResponse(responseCode = "500", description = "Unexpected server error. See Response Body for detail. - " + "Error code 500-1 = Unable to send to KAFKA, please wait and retry later - " + "Error code 500-2 = Unable to connect to verification database, please wait and retry later - " + "Error code 500-3 = General server error, Please wait and retry later", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))) public Response postStudentEvent(String eventJSON) {
现在,我可以放入以下标记来生成一类事件JSON字符串的openapi合约
@RequestBody( required = true, content = @Content( schema = @Schema(implementation = Application.class) ) )
但是,假设我也希望能够接受具有CourseEnrollment.class结构的JSON字符串(这些类都是项目btw的一部分)。
public class CourseEnrollment { private String eventID; private String eventTime; private String eventType; private String eventUserSource; private String externalReference1; private String firstName; private String lastName; private String emailAddressPrimary; private String emailAddressAlternate; private String periodStartDate; private String periodEndDate; private String intakeIntakeYearCode; private String programmeCode; public class Application { private String eventID; private String eventTime; private String eventType; private String eventUserSource; private String eventTrigger; private ApplicationInfo applicationDetails = null; private Person personDetails = null; private School schoolDetails = null;
我知道这是一个糟糕的场景,但这是我必须处理的,因为我们有一个外部系统生成事件并将JSON发送到我的REST服务,我必须将JSON传播到本地的KAFKA主题。
感谢您的投入