Swager CodGen生成具有多个try catch的代码ApiController.java

时间:2019-10-23 10:57:06

标签: java rest spring-boot swagger-codegen

我正在寻找由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抛出任何异常。因此,我需要在服务端手动回滚所有事务。

1 个答案:

答案 0 :(得分:0)

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