Spring Boot项目中的Swagger配置,对于相同的请求模型,需要显示不同的响应,请求模型中的示例
以下是我的2个API的定义:
@PutMapping("/valA")
public ResponseEntity<DummyResponse> getValA(@RequestBody DummyModel model) {
DummyResponse dummyResponse = new DummyResponse();
dummyResponse.setResA(model.getValA());
return new ResponseEntity<>(dummyResponse, HttpStatus.OK);
}
@PutMapping("/valB")
public ResponseEntity<DummyResponse> getValB(@RequestBody DummyModel model) {
DummyResponse dummyResponse = new DummyResponse();
dummyResponse.setResB(model.getValB());
return new ResponseEntity<>(dummyResponse, HttpStatus.OK);
}
dummymodel.java
package com.dummy.mo.model;
import java.io.Serializable;
import lombok.Data;
@Data
public class DummyModel implements Serializable {
private String valA;
private String valB;
}
,现在摇摇晃晃,对于两个API,示例如下所示:
但是,我的重新构建是仅在第二个API中显示vala和valb in second API在大肆的例子中。我的意思是,我只想显示相应API所需的参数。
是否有任何注释或配置可以在API/控制器级别定义所需的请求参数。 请注意:我无法更改API结构或模型类。
Below is the definition of my 2 APIs:
@PutMapping("/valA")
public ResponseEntity<DummyResponse> getValA(@RequestBody DummyModel model) {
DummyResponse dummyResponse = new DummyResponse();
dummyResponse.setResA(model.getValA());
return new ResponseEntity<>(dummyResponse, HttpStatus.OK);
}
@PutMapping("/valB")
public ResponseEntity<DummyResponse> getValB(@RequestBody DummyModel model) {
DummyResponse dummyResponse = new DummyResponse();
dummyResponse.setResB(model.getValB());
return new ResponseEntity<>(dummyResponse, HttpStatus.OK);
}
DummyModel.java
package com.dummy.mo.model;
import java.io.Serializable;
import lombok.Data;
@Data
public class DummyModel implements Serializable {
private String valA;
private String valB;
}
Now in swagger, For both the apis, example shows like below:
But, my reuirement is to show only valA in first api and valB in second api in swagger example. I mean, I only want to show the params which are required to the corresponding APIs.
Is there any annotation or configuration out there to define required request parameters at API/Controller level.
Note that: I cannot change the API structure or model class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为什么不拆分DummyModel类。
如果我们创建2个接口,则为下面:
虚拟模型类
更新您的API:
Why don't you split the DummyModel class.
What if we, create 2 interfaces like below :
Dummy Model class
Update your apis :