在Quarkus REST API中,在@requestbody OpenAPI标签中指定多个”implementation=”标签。

huangapple go评论86阅读模式
英文:

Specifying multiple "implementation=" tags in @requestbody openapi tag in quarkus REST api

问题

我有一个Quarkus REST API,它接受JSON字符串(各种类型)作为事件,然后将它们传播到一个KAFKA主题。

我现在想将可能的JSON字符串结构放入OpenAPI合同中(而不必手动键入)。

我已经找到了@requestbody标签,允许我指定一个结构,但我有多种类型的事件,它们都必须发送到同一个Kafka主题。

例如:

  1. @POST
  2. @Path("/student")
  3. @Consumes(MediaType.APPLICATION_JSON)
  4. @Produces(MediaType.APPLICATION_JSON)
  5. @Tag(name = "Student Events Service")
  6. @Operation(summary = "Accepts student type events", description = "Accepts student type events")
  7. @APIResponse(responseCode = "200", description = "Event sent successfully")
  8. @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 "
  9. + "See Response Body for detail. - "
  10. + "Error code 400-1 = Bad message structure or unknown message. Log error and continue to next message - "
  11. + "Error code 400-2 = Bad primary key. Fix data (client or server side) and retry later - "
  12. + "Error code 400-3 = Invalid shared data. Fix shared data and retry later",
  13. content = @Content(mediaType = "application/json",
  14. schema = @Schema(implementation = ErrorResponse.class)))
  15. @APIResponse(responseCode = "500", description = "Unexpected server error. See Response Body for detail. - "
  16. + "Error code 500-1 = Unable to send to KAFKA, please wait and retry later - "
  17. + "Error code 500-2 = Unable to connect to verification database, please wait and retry later - "
  18. + "Error code 500-3 = General server error, Please wait and retry later",
  19. content = @Content(mediaType = "application/json",
  20. schema = @Schema(implementation = ErrorResponse.class)))
  21. public Response postStudentEvent(String eventJSON) {

现在,我可以使用以下标签来生成一个类事件JSON字符串的OpenAPI合同:

  1. @RequestBody(
  2. required = true,
  3. content = @Content(
  4. schema = @Schema(implementation = Application.class)
  5. )
  6. )

但假设我还想能够接受具有CourseEnrollment.class结构的JSON字符串(这些类都是项目的一部分)。

  1. public class CourseEnrollment {
  2. private String eventID;
  3. private String eventTime;
  4. private String eventType;
  5. private String eventUserSource;
  6. private String externalReference1;
  7. private String firstName;
  8. private String lastName;
  9. private String emailAddressPrimary;
  10. private String emailAddressAlternate;
  11. private String periodStartDate;
  12. private String periodEndDate;
  13. private String intakeIntakeYearCode;
  14. private String programmeCode;
  15. }
  16. public class Application {
  17. private String eventID;
  18. private String eventTime;
  19. private String eventType;
  20. private String eventUserSource;
  21. private String eventTrigger;
  22. private ApplicationInfo applicationDetails = null;
  23. private Person personDetails = null;
  24. private School schoolDetails = null;
  25. }

这是一个奇怪的情景,我知道,但这就是我要处理的情况,因为我们有一个外部系统生成事件并将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:

  1. @POST
  2. @Path("/student")
  3. @Consumes(MediaType.APPLICATION_JSON)
  4. @Produces(MediaType.APPLICATION_JSON)
  5. @Tag(name = "Student Events Service")
  6. @Operation(summary = "Accepts student type events", description = "Accepts student type events")
  7. @APIResponse(responseCode = "200", description = "Event sent successfully")
  8. @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 "
  9. + "See Response Body for detail. - "
  10. + "Error code 400-1 = Bad message structure or unknown message. Log error and continue to next message - "
  11. + "Error code 400-2 = Bad primary key. Fix data (client or server side) and retry later - "
  12. + "Error code 400-3 = Invalid shared data. Fix shared data and retry later",
  13. content = @Content(mediaType = "application/json",
  14. schema = @Schema(implementation = ErrorResponse.class)))
  15. @APIResponse(responseCode = "500", description = "Unexpected server error. See Response Body for detail. - "
  16. + "Error code 500-1 = Unable to send to KAFKA, please wait and retry later - "
  17. + "Error code 500-2 = Unable to connect to verification database, please wait and retry later - "
  18. + "Error code 500-3 = General server error, Please wait and retry later",
  19. content = @Content(mediaType = "application/json",
  20. schema = @Schema(implementation = ErrorResponse.class)))
  21. public Response postStudentEvent(String eventJSON) {

Now I can put the following tag in to generate the openapi contract for 1 class of eventJSON string

  1. @RequestBody(
  2. required = true,
  3. content = @Content(
  4. schema = @Schema(implementation = Application.class)
  5. )
  6. )

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).

  1. public class CourseEnrollment {
  2. private String eventID;
  3. private String eventTime;
  4. private String eventType;
  5. private String eventUserSource;
  6. private String externalReference1;
  7. private String firstName;
  8. private String lastName;
  9. private String emailAddressPrimary;
  10. private String emailAddressAlternate;
  11. private String periodStartDate;
  12. private String periodEndDate;
  13. private String intakeIntakeYearCode;
  14. private String programmeCode;
  15. public class Application {
  16. private String eventID;
  17. private String eventTime;
  18. private String eventType;
  19. private String eventUserSource;
  20. private String eventTrigger;
  21. private ApplicationInfo applicationDetails = null;
  22. private Person personDetails = null;
  23. 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:

  1. @RequestBody(
  2. required = true,
  3. content = @Content(
  4. schema = @Schema(oneOf = {Application.class, CourseEnrollment.class})
  5. )
  6. )
英文:

You can use oneOf:

  1. @RequestBody(
  2. required = true,
  3. content = @Content(
  4. schema = @Schema(oneOf = {Application.class, CourseEnrollment.class})
  5. )
  6. )

huangapple
  • 本文由 发表于 2023年2月8日 16:31:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定