将Json转实体

算了,不废话了吐舌头

protected static ObjectMapper mapper = new ObjectMapper();

 

//转一个
public <S> S getRequestParams(String json, Class<S> membersClazzs)
  throws JsonParseException, JsonMappingException, IOException {
  return mapper.readValue(json, membersClazzs);
}


//转多个
public static <S> S getRequestParams(String json, Class<?> collectionsClass,
  Class<?>... membersClazzs) throws JsonParseException,
  JsonMappingException, IOException {
  return mapper.readValue(json, mapper.getTypeFactory().constructParametricType(collectionsClass, membersClazzs));
}


//调用转换方法
public voidTest(@RequestParam("entityJSON") StringentityJSON,……){
  ……
  Entity entity= getRequestParams(entityJSON,Entity.class);
  ……
}
原文地址:https://www.cnblogs.com/IceBlueBrother/p/8421758.html