use of com.rebuild.api.RespBody in project rebuild by getrebuild.
the class ControllerRespBodyAdvice method beforeBodyWrite.
@Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
HttpServletRequest request = ((ServletServerHttpRequest) serverHttpRequest).getServletRequest();
HttpServletResponse response = ((ServletServerHttpResponse) serverHttpResponse).getServletResponse();
// Controller send status of error
if (response.getStatus() != 200) {
if (o instanceof Map) {
String path404 = (String) ((Map<?, ?>) o).get("path");
if (path404 != null && path404.endsWith(".map")) {
return o;
}
}
log.warn("Response Error Status : {}", o);
return o;
}
// @ResponseBody 方法返回 `null` `String` 时 mediaType=TEXT_PLAIN
if (MediaType.TEXT_PLAIN.equals(mediaType)) {
// @Deprecated
log.warn("Force conversion TEXT_PLAIN to APPLICATION_JSON : {}", request.getRequestURI());
response.addHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
}
if (o instanceof RespBody) {
return ((RespBody) o).toJSON();
} else {
return RespBody.ok(o).toJSON();
}
}
use of com.rebuild.api.RespBody in project rebuild by getrebuild.
the class GeneralOperatingController method save.
@PostMapping("record-save")
public JSONAware save(HttpServletRequest request) {
final ID user = getRequestUser(request);
final JSON formJson = ServletUtils.getRequestJson(request);
final Object details = ((JSONObject) formJson).remove(GeneralEntityService.HAS_DETAILS);
Record record;
try {
record = EntityHelper.parse((JSONObject) formJson, user);
} catch (DataSpecificationException known) {
log.warn(">>>>> {}", known.getLocalizedMessage());
return RespBody.error(known.getLocalizedMessage());
}
// 非业务实体(兼容所有类型的实体)
if (!MetadataHelper.isBusinessEntity(record.getEntity())) {
return CommonOperatingController.saveRecord(record);
}
// 明细
List<Record> detailsList = new ArrayList<>();
if (details != null) {
try {
for (Object d : (JSONArray) details) {
Record detail = EntityHelper.parse((JSONObject) d, user);
detailsList.add(detail);
}
} catch (DataSpecificationException known) {
log.warn(">>>>> {}", known.getLocalizedMessage());
return RespBody.error(known.getLocalizedMessage());
}
}
final EntityService ies = Application.getEntityService(record.getEntity().getEntityCode());
// 检查重复值
List<Record> repeated = ies.getAndCheckRepeated(record, 20);
if (!repeated.isEmpty()) {
return new RespBody(DefinedException.CODE_RECORDS_REPEATED, Language.L("存在重复记录"), buildRepeatedData(repeated));
}
if (!detailsList.isEmpty()) {
record.setObjectValue(GeneralEntityService.HAS_DETAILS, detailsList);
GeneralEntityServiceContextHolder.setRepeatedCheckMode(GeneralEntityServiceContextHolder.RCM_CHECK_DETAILS);
}
try {
record = ies.createOrUpdate(record);
} catch (RepeatedRecordsException know) {
return new RespBody(DefinedException.CODE_RECORDS_REPEATED, Language.L("存在重复记录"), buildRepeatedData(know.getRepeatedRecords()));
} catch (AccessDeniedException | DataSpecificationException known) {
log.warn(">>>>> {}", known.getLocalizedMessage());
return RespBody.error(known.getLocalizedMessage());
} catch (GenericJdbcException ex) {
String known = KnownExceptionConverter.convert2ErrorMsg(ex);
if (known != null)
return RespBody.error(known);
log.error(null, ex);
return RespBody.error(ex.getLocalizedMessage());
} finally {
// 确保清除
GeneralEntityServiceContextHolder.getRepeatedCheckModeOnce();
}
JSONObject ret = new JSONObject();
ret.put("id", record.getPrimary());
// 单字段修改立即返回新值
boolean viaSingle = getBoolParameter(request, "single");
if (viaSingle) {
for (String field : record.getAvailableFields()) {
Field fieldMeta = record.getEntity().getField(field);
if (MetadataHelper.isCommonsField(field) || fieldMeta.getType() == FieldType.PRIMARY) {
continue;
}
Object newValue = FormsBuilder.instance.wrapFieldValue(record, EasyMetaFactory.valueOf(fieldMeta), null);
ret.put(field, newValue);
}
}
return ret;
}
Aggregations