gpt4 book ai didi

java - Swagger CodeGen 生成带有多个 try catch 的代码 ApiController.java

转载 作者:行者123 更新时间:2023-12-01 22:36:50 25 4
gpt4 key购买 nike

我正在寻找在由 SwagerCodeGen 生成的 APIController.java 中生成具有多个 try catch 的代码。

swagger.yaml 文件

   paths:
/system:
post:
tags:
- New
summary: System info
description: Initial System Info
operationId: createSystem
consumes:
- application/json
produces:
- application/json
parameters:
- in: body
name: systemDetails
description: Contains the JSON Object
required: true
schema:
$ref: '#/definitions/SystemDetails'
- name: AuthKey
in: query
description: Key for Authentication and Authorization.
required: true
type: string
format: uuid
responses:
'201':
description: System Created Successfully
schema:
$ref: '#/definitions/Response'
'400':
description: Request Failed
'401':
description: Unauthorized
'500':
description: Internal Server Error

Swager 生成的代码如下,

SystemApi.java

@ApiOperation(value = "System", notes = "Initial System Info", response = Response.class, tags={ "System", })
@ApiResponses(value = {
@ApiResponse(code = 201, message = "System Created Successfully", response = Response.class),
@ApiResponse(code = 400, message = "Request Failed", response = Void.class),
@ApiResponse(code = 401, message = "Unauthorized", response = Void.class),
@ApiResponse(code = 404, message = "Not Found", response = Void.class),
@ApiResponse(code = 500, message = "Internal Server Error", response = Void.class) })

@RequestMapping(value = "/system",
produces = { "application/json" },
consumes = { "application/json" },
method = RequestMethod.POST)
ResponseEntity<Response> createSystem(@ApiParam(value = "Contains the JSON Object" ,required=true ) @Valid @RequestBody SystemDetails systemDetails, @NotNull@ApiParam(value = "Key for Authentication and Authorization.", required = true) @RequestParam(value = "AuthKey", required = true) UUID authKey);

SystemApiController.Java

  public ResponseEntity<Response> createSystem(
@ApiParam(value = "Contains the JSON Object",
required = true) @Valid @RequestBody SystemDetails systemDetails,
@NotNull @ApiParam(
value = "Key for Authentication and Authorization.",
required = true) @RequestParam(value = "AuthKey", required = true) UUID authKey) {
// do some magic!
return delegate.createSystem(systemDetails, authKey);
}

有没有办法添加从 Swagger CodeGen 自动生成的 try catch,如下所示?

  public ResponseEntity<Response> createSystem(
@ApiParam(value = "Contains the JSON Object",
required = true) @Valid @RequestBody SystemDetails systemDetails,
@NotNull @ApiParam(
value = "Key for Authentication and Authorization.",
required = true) @RequestParam(value = "AuthKey", required = true) UUID authKey) {
// do some magic!
try{
return delegate.createSystem(systemDetails, authKey);
}catch(InvalidInputException e){
return new ResponseEntity(new Response(HTTPSTATUS.BAD_REQUEST));
}
}

或者请建议替代方法。现在我正在处理 SystemService.java 中的所有异常,而不是向 Controller 抛出任何异常。因此,我需要在服务端手动回滚所有事务。

最佳答案

Swagger codegen 使用 Mustache 模板来生成代码。您可以引用https://github.com/swagger-api/swagger-codegen#modifying-the-client-library-format自定义生成类的语法

关于java - Swagger CodeGen 生成带有多个 try catch 的代码 ApiController.java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58521289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com