在获取信息之前,对每个参数进行非空判断,如果为空则给出相应的错误提示。可以使用以下代码进行非空判断:

if (disease_type == null || disease_type.isEmpty()) {
    hashMap.put("message", "疾病类型不能为空");
    response.getWriter().append(gson.toJson(hashMap));
    return;
}

if (describetion == null || describetion.isEmpty()) {
    hashMap.put("message", "描述不能为空");
    response.getWriter().append(gson.toJson(hashMap));
    return;
}

if (describe_img == null || describe_img.isEmpty()) {
    hashMap.put("message", "描述图片不能为空");
    response.getWriter().append(gson.toJson(hashMap));
    return;
}

if (doctor_id == null || doctor_id.isEmpty()) {
    hashMap.put("message", "医生ID不能为空");
    response.getWriter().append(gson.toJson(hashMap));
    return;
}

if (user_id == null || user_id.isEmpty()) {
    hashMap.put("message", "用户ID不能为空");
    response.getWriter().append(gson.toJson(hashMap));
    return;
}

if (book_time == null || book_time.isEmpty()) {
    hashMap.put("message", "预约时间不能为空");
    response.getWriter().append(gson.toJson(hashMap));
    return;
}

//数据封装
Registration registration = new Registration(0, disease_type, describetion, describe_img, null, Integer.parseInt(doctor_id), Integer.parseInt(user_id), 0, book_time);
System.out.println(registration);

//判断及响应
boolean insert = registrationService.insert(registration);
if (!insert) {
    hashMap.put("message", "预约挂号出错");
}
hashMap.put("flag", insert);
response.getWriter().append(gson.toJson(hashMap));

这样,如果有任何一个参数为空,都会给出相应的错误提示并返回

protected void insertHttpServletRequest request HttpServletResponse response throws ServletException IOException HashMap hashMap = new HashMap; responsesetContentTypetestjson;charset=ut

原文地址: https://www.cveoy.top/t/topic/ilVY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录