将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);
  ……
}
posted @ 2018-02-06 12:41  Ice_Blue_Brother  阅读(253)  评论(0编辑  收藏  举报