[英]Pass custom json object in swagger with spring boot
我想通过自定义 json object 作为 swagger 请求正文。 在 Java 中,我们使用@ApiModelProperty(hidden = true)
注释来隐藏 swagger 中的一些字段。 但在我的情况下,我想通过自定义 json object 作为 swagger 请求正文。
这是我的代码,
@PostMapping(value = "/verifyMobile", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Verify User otp", notes = "Verifies user email")
public ResponseEntity<Map<String, Boolean>> verifyMobile(@RequestBody Map<String, Object> verificationObj) {
Boolean isUpdated = userService.mobileVerification(verificationObj.get("phoneNumber").toString(),
verificationObj.get("mobileVerificationOtp").toString());
Map<String, Boolean> objMap = new HashMap<String, Boolean>();
objMap.put("success", isUpdated);
return isUpdated ? ResponseEntity.status(HttpStatus.ACCEPTED).body(objMap) :
ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(objMap);
}
我将接受波纹管作为请求主体,
{
"phoneNumber":"+919038897580",
"mobileVerificationOtp":"9399"
}
If you want to display a json
object as request body in Swagger
you can create a model object.
您的 model object 可以如下。
public class VerificationRequest {
private String phoneNumber;
private String mobileVerificationOtp;
public VerificationRequest(String phoneNumber, String mobileVerificationOtp) {
this.phoneNumber = phoneNumber;
this.mobileVerificationOtp = mobileVerificationOtp;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getMobileVerificationOtp() {
return mobileVerificationOtp;
}
public void setMobileVerificationOtp(String mobileVerificationOtp) {
this.mobileVerificationOtp = mobileVerificationOtp;
}
}
然后,您可以编辑端点所在的 model,如下所示。
@PostMapping(value = "/verifyMobile", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Verify User otp", notes = "Verifies user email")
public ResponseEntity<Map<String, Boolean>> verifyMobile(@RequestBody VerificationRequest verificationObj) {
Boolean isUpdated = userService.mobileVerification(verificationObj.getPhoneNumber(),
verificationObj.getMobileVerificationOtp());
Map<String, Boolean> objMap = new HashMap<String, Boolean>();
objMap.put("success", isUpdated);
return isUpdated ? ResponseEntity.status(HttpStatus.ACCEPTED).body(objMap) :
ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(objMap);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.