通过 spring 引导大摇大摆地传递自定义 json object
Pass custom json object in swagger with spring boot
我想将自定义 json object 作为 swagger 请求 body 传递。在 Java 中,我们使用 @ApiModelProperty(hidden = true)
注释来隐藏一些 swagger 字段。但就我而言,我想将自定义 json object 作为大摇大摆的请求 body.
这是我的代码,
@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);
}
我将接受下面的请求 body,
{
"phoneNumber":"+919038897580",
"mobileVerificationOtp":"9399"
}
我怎样才能大摇大摆地实现这个。 Swagger body 看起来像这样。
请帮我解决这个问题。
如果你想在 Swagger
中显示一个 json
对象作为请求主体,你可以创建一个模型对象。
您的模型对象可以如下所示。
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;
}
}
然后您可以编辑您的端点所在的模型,如下所示。
@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);
}
我想将自定义 json object 作为 swagger 请求 body 传递。在 Java 中,我们使用 @ApiModelProperty(hidden = true)
注释来隐藏一些 swagger 字段。但就我而言,我想将自定义 json object 作为大摇大摆的请求 body.
这是我的代码,
@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);
}
我将接受下面的请求 body,
{
"phoneNumber":"+919038897580",
"mobileVerificationOtp":"9399"
}
我怎样才能大摇大摆地实现这个。 Swagger body 看起来像这样。
如果你想在 Swagger
中显示一个 json
对象作为请求主体,你可以创建一个模型对象。
您的模型对象可以如下所示。
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;
}
}
然后您可以编辑您的端点所在的模型,如下所示。
@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);
}