springboot 使用验证注解:需要引入spring-boot-starter-validation(使用Hibernate Validator 框架提供验证功能) 依赖进行参数校验
- 要验证的pojo 配置
package com.hjkj.facecomparison.person;
import javax.validation.constraints.NotBlank;
/**
* 比对对象
*/
public class Person {
@NotBlank(message = "待核验用户姓名不可为空")
private String nameText;
@NotBlank(message = "待核验用户身份证号不可为空")
private String numberText;
@NotBlank(message = "待核验用户现场照片不可为空")
private String idPhoto;
public String getNameText() {
return nameText;
}
public void setNameText(String nameText) {
this.nameText = nameText;
}
public String getNumberText() {
return numberText;
}
public void setNumberText(String numberText) {
this.numberText = numberText;
}
public String getIdPhoto() {
return idPhoto;
}
public void setIdPhoto(String idPhoto) {
this.idPhoto = idPhoto;
}
@Override
public String toString() {
return "Person{" +
"nameText='" + nameText + '\'' +
", numberText='" + numberText + '\'' +
", idPhoto='" + idPhoto + '\'' +
'}';
}
}
- controller 配置
/**
* 人脸比对处理类
*/
package com.hjkj.facecomparison.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hjkj.facecomparison.common.ResponseResult;
import com.hjkj.facecomparison.person.Person;
import com.hjkj.facecomparison.utils.HttpConnectionUtil;
import com.pinecone.utils.EncryptionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
/**
* 人脸比对处理类
*/
@RestController
public class FaceComparisonController {
@Value("${sm4Key}")
private String sm4Key;
@Value("${requestUrl}")
private String requestUrl;
@Value("${clientCode}")
private String clientCode;
/**
* 获取比对结果
*
* @param person {"nameText":"",numberText:"",idPhoto:""}
* @return json
*/
@PostMapping(value = "/comparisonResult")
@ResponseBody
public ResponseResult faceComparisonResult(@RequestBody @Valid Person person) throws Exception {
JSONObject source = new JSONObject();
source.put("clientCode", clientCode);
source.put("encryptString", EncryptionUtils.encryptSM4(sm4Key, JSON.toJSONString(person)));
// return HttpConnectionUtil.doPost(requestUrl, source.toString());
JSONObject jsonObject = JSON.parseObject(HttpConnectionUtil.doPost(requestUrl, source.toString()));
String result = jsonObject.getString("result");
if (result != null && result.equals("-1")) {
return ResponseResult.error(result, jsonObject.getString("errCode"),
jsonObject.getString("resultDetail"), "比对失败");
}
return ResponseResult.success(jsonObject.getString("result"), jsonObject.getString("similarity"), jsonObject.getString("resultDetail"));
}
}
- 验证异常处理
package com.hjkj.facecomparison.exception
import com.hjkj.facecomparison.common.ResponseResult;
import com.hjkj.facecomparison.common.ResponseStatuses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
/**
* 全局异常处理类
*
* @author Jack
*/
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 校验对象参数异常
* @param ex
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ResponseResult validExceptionHandler(MethodArgumentNotValidException ex) {
BindingResult bindingResult = ex.getBindingResult();
StringBuffer stringBuffer = new StringBuffer();
if (bindingResult.hasErrors()) {
for (FieldError fieldError : bindingResult.getFieldErrors()) {
//该格式仅仅作为response展示和log作用,前端应自己做校验
stringBuffer.append(fieldError.getDefaultMessage() + " ");
}
}
log.error(stringBuffer.toString());
return ResponseResult.error(ResponseStatuses.ERROR_PARAMS_NOT_IS_BLANK.getCode(),stringBuffer.toString());
}
- 高复应响应对象
package com.hjkj.facecomparison.common;
public class ResponseResult {
private String errCode;
private String result;
private String resultDetail;
private String similarity;
public ResponseResult(String result, String resultDetail, String similarity) {
this.result = result;
this.resultDetail = resultDetail;
this.similarity = similarity;
}
public ResponseResult(String errCode, String result, String resultDetail, String similarity) {
this.errCode = errCode;
this.result = result;
this.resultDetail = resultDetail;
this.similarity = similarity;
}
public ResponseResult(String result, String resultDetail) {
this.result = result;
this.resultDetail = resultDetail;
}
public static ResponseResult success(String result, String similarity, String resultDetail) {
return new ResponseResult(result, similarity, resultDetail);
}
public static ResponseResult error(String result, String errCode, String resultDetail, String similarity){
return new ResponseResult( errCode,result, resultDetail,similarity);
}
public static ResponseResult errorException(String result, String resultDetail){
return new ResponseResult( result, resultDetail);
}
public String getSimilarity() {
return similarity;
}
public void setSimilarity(String similarity) {
this.similarity = similarity;
}
public String getErrCode() {
return errCode;
}
public void setErrCode(String errCode) {
this.errCode = errCode;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getResultDetail() {
return resultDetail;
}
public void setResultDetail(String resultDetail) {
this.resultDetail = resultDetail;
}
}
网友评论