Swager CodGen generating code ApiController.java with multiple try catch

情到浓时终转凉″ 提交于 2020-01-06 04:57:07

问题


I am looking for generating code with multiple try catch in APIController.java which is generated by SwagerCodeGen.

swagger.yaml file

   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 generated code as below,

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);
  }

Is there any way to add try catch auto generated from Swagger CodeGen like below?

  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));
    }
  }

Or please suggest alternate way. Now I am handling all exceptions in SystemService.java, not throwing any exception to Controller. Because of this I need to rollback all Transactions manually in service side.


回答1:


Swagger codegen uses moustache templates to generate your code. You can refer to https://github.com/swagger-api/swagger-codegen#modifying-the-client-library-format to customize the syntax of generated classes



来源:https://stackoverflow.com/questions/58521289/swager-codgen-generating-code-apicontroller-java-with-multiple-try-catch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!