- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找在由 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/
我想知道是否可以安全地编写 catch() 来捕获所有 System.Exception 类型。或者我是否必须坚持使用 catch(Exception) 来完成此任务。我知道对于其他异常类型(例如 I
在 C# 中,'Catch'、'Catch (Exception)' 和 'Catch(Exception e)' 之间有什么区别? MSDN article on try-catch在其示例中使用了
然后一个 Promise 调用另一个 Promise,并且内部 Promise 从 catch .then block 中的外部 Promise 返回 我一般都在这里和谷歌上搜索过。尝试使用简单的 t
我们可以在 Try-Catch 中使用多个 catch 块。 但我的问题是:为什么可以使用单个 catch 块完成时使用多个 catch 块? 假设我想要我的问题的确切原因,我可以通过 Ex.mess
所以我在 service.ts 中有这个用户服务功能其中包括数据库的东西。 export const service = { async getAll(): Promise { try {
我不确定这里发生了什么。很明显为什么内扣会捕获throw 2 ,但为什么外面catch(int x)捕获 throw ?我以为catch(int x)应该只捕获整数值。第二个throw有可能吗?抛出什
我目前正在以不同的方式加载图像,如下所示: try { // way 1 } catch { // way 1 didn't work try { // way 2 }
这两者有什么区别?一个比另一个快吗?两者似乎都有效。有人请解释 没有 promise 的人: client.query(query1) .then(data => { callback(null
它几乎可以在所有语言中找到,而且我大部分时间都在使用它。 我不知道它是内部的,不知道它是如何真正起作用的。 它如何在任何语言的运行时在 native 级别工作? 例如:如果在 try 内部发生 sta
Closed. This question is opinion-based。它当前不接受答案。 想改善这个问题吗?更新问题,以便editing this post用事实和引用来回答。 1年前关闭。
我正在编写一个用于学习目的的短代码,要求用户输入密码才能登录 Facebook。我正在测试异常处理,由于某种原因,当密码错误时,Catch 部分没有执行。代码是: import java.util.S
如果try-catch的catch block 中抛出异常,那么finally block 会被调用吗? try { //some thing which throws error } cat
try { while ((inputLine = bufferedReader.readLine()) != null) { String[] words = inputLine.s
在 C# 上下文中,可以使用如下代码: try { ... } catch { ... } 在其他情况下,代码可以是: try { ... } catch (Exc
有时我在探索 ServiceStack 的代码库时遇到以下构造: try { ... } catch (Exception) { throw; } 在我看来,这种结构没有任何作用。这样做的
我最近遇到了一个 Javascript 问题,捕获错误,因此在抛出异常时崩溃。 funcReturnPromise().then().catch() 我必须将其更改为: try { funcRet
我在编写一些测试的 C++ 文件中遇到此错误: error: no member named 'Session' in namespace 'Catch' testResult = C
CException 是VC++抛出的所有异常的基类型,所以它应该捕获所有的异常吧? 最佳答案 CException 不是所有扩展的基类型(它可能是 MFC 代码使用的所有异常的基类型,但仅此而已)。
每次我看到 catch all 语句时: try { // some code } catch (...) { } 它一直是一种滥用。 反对使用 cache all 子句的论点是显而易见的。它会捕
代码相当简单——问题是 groupPath 字符串中有一个无效字符(准确地说是“/”)。 我正在尝试做的(至少作为权宜之计)是跳过我无法获得 cn 的 DirectoryEntries --- 不管为
我是一名优秀的程序员,十分优秀!