英文:
Specifying multiple "implementation=" tags in @requestbody openapi tag in quarkus REST api
问题
我有一个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字符串(这些类都是项目的一部分)。
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主题。
谢谢你的建议。
英文:
I have a quarkus REST api that accepts JSON strings (various kinds) as events and then propagates them to a KAFKA topic.
I want to now put the structure of the possible JSON strings in the openAPI contract (without having to type it out myself manually.
I have managed to find the @requestbody tag that allows me to specify the one structure but I have multiple types of events that all have to go to the same Kafka topic.
example:
@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) {
Now I can put the following tag in to generate the openapi contract for 1 class of eventJSON string
@RequestBody(
required = true,
content = @Content(
schema = @Schema(implementation = Application.class)
)
)
But lets say I also want to be able to accept JSON string with the structure of CourseEnrollment.class (These classes are all part of the project 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;
Its a wierd scenario I know, but thats what I have to deal with as we have an external system generating the events and sending the JSON to my REST service and I have to propagate the JSON to the KAFKA topics locally.
Thank you for your inputs
答案1
得分: 0
你可以使用 oneOf
:
@RequestBody(
required = true,
content = @Content(
schema = @Schema(oneOf = {Application.class, CourseEnrollment.class})
)
)
英文:
You can use oneOf
:
@RequestBody(
required = true,
content = @Content(
schema = @Schema(oneOf = {Application.class, CourseEnrollment.class})
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论