sábado, 20 de outubro de 2018

how to check if attribute has '@notNull' annotation dynamic

This solution was used in a java project with String jdbcTemplate following the idea of @NotNull JPA Annotation to check ScreenMapper from Controller and @All attributes need to have the same name column in Database.


public Boolean validationRequiredFieldsInMapper(Class<?> clazz, final Map<String, Object> screenFieldMap) {
Boolean isError = false;

Object objValue = null;
Field field = null;
String strValue = "";
for (String column : screenFieldMap.keySet()) {
field = clazz.getDeclaredField(column);
if (field != null) {
if (field.isAnnotationPresent(NotNull.class)) {
objValue = screenFieldMap.get(column);
strValue = (String) objValue;
if (StringUtils.isBlank(strValue)) {
isError = true;
break;
}
}
}
}
}

Class:

@NotNull
private Integer sap;
@NotNull
private String address;
@NotNull
private Date dateRegister;

Map: 

@RequestMapping(value="examplecontroller", method=RequestMethod.POST)
public @ResponseBody ReturnObject setData(
@RequestParam(value="sap",required= true ) String sap,
@RequestParam(value="address", required= true) String address,
@RequestParam(value="dateRegister", required= true) String dateRegister,

Map<String,Object> screenFieldMap = new HashMap<String,Object>();
screenFieldMap.put("sap",sap);
screenFieldMap.put("address",address);
screenFieldMap.put("dateRegister",dateRegister);




Nenhum comentário:

Postar um comentário