Java Code Examples for cn.hutool.json.JSONUtil#toBean()

The following examples show how to use cn.hutool.json.JSONUtil#toBean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserServiceImpl.java    From fw-spring-cloud with Apache License 2.0 6 votes vote down vote up
@Override
public User getUserById(long id) {
    String userRedis = redisTemplate.opsForValue().get(String.valueOf(id));
    if(StrUtil.isEmpty(userRedis)){
        User userByDao = getUserByDao(id);
        if(null!=userByDao){
            redisTemplate.opsForValue().set(String.valueOf(id),JSONUtil.toJsonStr(userByDao));
            return  userByDao;
        }else{
            return null;
        }
    }else{
        log.info("我是缓存的,有点叼");
        return JSONUtil.toBean(userRedis,User.class);
    }
}
 
Example 2
Source File: HutoolController.java    From mall-learning with Apache License 2.0 6 votes vote down vote up
@ApiOperation("JSONUtil使用:JSON解析工具类")
@GetMapping("/jsonUtil")
public CommonResult jsonUtil() {
    PmsBrand brand = new PmsBrand();
    brand.setId(1L);
    brand.setName("小米");
    brand.setShowStatus(1);
    //对象转化为JSON字符串
    String jsonStr = JSONUtil.parse(brand).toString();
    LOGGER.info("jsonUtil parse:{}", jsonStr);
    //JSON字符串转化为对象
    PmsBrand brandBean = JSONUtil.toBean(jsonStr, PmsBrand.class);
    LOGGER.info("jsonUtil toBean:{}", brandBean);
    List<PmsBrand> brandList = new ArrayList<>();
    brandList.add(brand);
    String jsonListStr = JSONUtil.parse(brandList).toString();
    //JSON字符串转化为列表
    brandList = JSONUtil.toList(new JSONArray(jsonListStr), PmsBrand.class);
    LOGGER.info("jsonUtil toList:{}", brandList);
    return CommonResult.success(null, "操作成功");
}
 
Example 3
Source File: DingMsgForm.java    From WePush with MIT License 6 votes vote down vote up
@Override
public void init(String msgName) {
    clearAllField();
    initAppNameList();
    List<TMsgDing> tMsgDingList = msgDingMapper.selectByMsgTypeAndMsgName(MessageTypeEnum.DING_CODE, msgName);
    if (tMsgDingList.size() > 0) {
        TMsgDing tMsgDing = tMsgDingList.get(0);
        String dingMsgType = tMsgDing.getDingMsgType();
        getInstance().getAppNameComboBox().setSelectedItem(agentIdToAppNameMap.get(tMsgDing.getAgentId()));
        getInstance().getMsgTypeComboBox().setSelectedItem(dingMsgType);
        DingMsg dingMsg = JSONUtil.toBean(tMsgDing.getContent(), DingMsg.class);
        getInstance().getContentTextArea().setText(dingMsg.getContent());
        getInstance().getTitleTextField().setText(dingMsg.getTitle());
        getInstance().getPicUrlTextField().setText(dingMsg.getPicUrl());
        getInstance().getUrlTextField().setText(dingMsg.getUrl());
        getInstance().getBtnTxtTextField().setText(dingMsg.getBtnTxt());
        getInstance().getWebHookTextField().setText(tMsgDing.getWebHook());

        switchDingMsgType(dingMsgType);

        switchRadio(tMsgDing.getRadioType());
    } else {
        switchDingMsgType("文本消息");
    }
}
 
Example 4
Source File: AboutListener.java    From WePush with MIT License 6 votes vote down vote up
/**
 * 初始化二维码
 */
public static void initQrCode() {
    String qrCodeContent = HttpUtil.get(UiConsts.QR_CODE_URL);
    if (StringUtils.isNotEmpty(qrCodeContent)) {
        Map<String, String> urlMap = JSONUtil.toBean(qrCodeContent, Map.class);
        JLabel qrCodeLabel = AboutForm.getInstance().getQrCodeLabel();

        try {
            URL url = new URL(urlMap.get("url"));
            BufferedImage image = ImageIO.read(url);
            qrCodeLabel.setIcon(new ImageIcon(image));
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e);
        }

        MainWindow.getInstance().getAboutPanel().updateUI();
    }
}
 
Example 5
Source File: ValidateCodeServiceImpl.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(ServletWebRequest request) {
	String imageRes = redisTemplate.opsForValue().get(SESSION_KEY);
	ImageCode codeInSession = JSONUtil.toBean(imageRes, ImageCode.class);
	String codeInRequest;
	try {
		codeInRequest = ServletRequestUtils.getStringParameter(request.getRequest(),
				"imageCode");
	} catch (ServletRequestBindingException e) {
		throw new ValidateCodeException("获取验证码的值失败");
	}

	if (StrUtil.isBlank(codeInRequest)) {
		throw new ValidateCodeException("验证码的值不能为空");
	}

	if (codeInSession == null) {
		throw new ValidateCodeException("验证码不存在");
	}

	if (codeInSession.isExpried()) {
		redisTemplate.delete(SESSION_KEY);
		throw new ValidateCodeException("验证码已过期");
	}

	if (!StrUtil.equals(codeInSession.getCode(), codeInRequest)) {
		throw new ValidateCodeException("验证码不匹配");
	}

	redisTemplate.delete(SESSION_KEY);
}
 
Example 6
Source File: SmUtil.java    From uccn with Apache License 2.0 5 votes vote down vote up
/**
 * @param multipartFile 表单名称。上传图片用到
 * @param ssl 是否使用 https 输出,强制开启
 * @param format 输出的格式。可选值有 json、xml。默认为 json
 * @return
 */
public static Sm saveFile(MultipartFile multipartFile, boolean ssl, String format){
    Map<String, Object> map = new HashMap<>(16);
    map.put("smfile", getFile(multipartFile));
    map.put("ssl", ssl);
    map.put("format", format);

    String smStr = HttpUtil.post(Config.SM_URL, map);

    return JSONUtil.toBean(smStr, Sm.class);
}
 
Example 7
Source File: SmUtil.java    From uccn with Apache License 2.0 5 votes vote down vote up
/**
 * @param multipartFile 表单名称。上传图片用到
 * @param format 输出的格式。可选值有 json、xml。默认为 json
 * @return
 */
public static Sm saveFile(MultipartFile multipartFile, String format){
    Map<String, Object> map = new HashMap<>(16);
    map.put("smfile", getFile(multipartFile));
    map.put("format", format);

    String smStr = HttpUtil.post(Config.SM_URL, map);

    return JSONUtil.toBean(smStr, Sm.class);
}
 
Example 8
Source File: SmUtil.java    From uccn with Apache License 2.0 5 votes vote down vote up
/**
 * @param multipartFile 表单名称。上传图片用到
 * @param ssl 是否使用 https 输出,强制开启
 * @return
 */
public static Sm saveFile(MultipartFile multipartFile, boolean ssl){
    Map<String, Object> map = new HashMap<>(16);
    map.put("smfile", getFile(multipartFile));
    map.put("ssl", ssl);

    String smStr = HttpUtil.post(Config.SM_URL, map);

    return JSONUtil.toBean(smStr, Sm.class);
}
 
Example 9
Source File: SmUtil.java    From uccn with Apache License 2.0 5 votes vote down vote up
/**
 * @param multipartFile 表单名称。上传图片用到
 * @return
 */
public static Sm saveFile(MultipartFile multipartFile){
    Map<String, Object> map = new HashMap<>(16);
    map.put("smfile", getFile(multipartFile));

    String smStr = HttpUtil.post(Config.SM_URL, map);

    return JSONUtil.toBean(smStr, Sm.class);
}
 
Example 10
Source File: FabricEventListener.java    From fabric-java-block with GNU General Public License v3.0 5 votes vote down vote up
public static String setChainCodeEventListener(Channel channel,
                                             ContractEvent contractEvent,
                                             RemoteBlockEventService remoteBlockEventService)
        throws InvalidArgumentException {

    ChaincodeEventListener chaincodeEventListener = (handle, blockEvent, chaincodeEvent) -> {


        log.info("RECEIVED CHAINCODE EVENT with handle: " +
                handle +
                ", chaincodeId: " + chaincodeEvent.getChaincodeId() +
                ", chaincode event name: " + chaincodeEvent.getEventName() +
                ", transactionId: " + chaincodeEvent.getTxId() +
                ", event Payload: " + new String(chaincodeEvent.getPayload()));
        //解析PayLoad 对比 是否 同一联盟信息
        EventPayLoad eventPayLoad = JSONUtil.toBean(new String(chaincodeEvent.getPayload()),EventPayLoad.class);
        if(contractEvent.getLeagueCode().equals(eventPayLoad.getLeagueCode())) {
            contractEvent.setEventPayLoad(new String(chaincodeEvent.getPayload()));
            contractEvent.setBlockNo(blockEvent.getBlockNumber());
            contractEvent.setTxId(chaincodeEvent.getTxId());
            //调用接口返回事件信息
            ThreadPoolManager.newInstance().addExecuteTask(() -> remoteBlockEventService.recordBlockEvent(contractEvent));
        }

    };
    // chaincode events.
    String chaincodeEventHandler = channel.registerChaincodeEventListener(Pattern.compile(".*"),
            Pattern.compile(Pattern.quote(contractEvent.getEventKey())), chaincodeEventListener);
    return chaincodeEventHandler;
}
 
Example 11
Source File: RedisUtils.java    From fw-spring-cloud with Apache License 2.0 4 votes vote down vote up
/**
 * JSON数据,转成Object
 */
private <T> T fromJson(String json,Class<T> clazz){
    return JSONUtil.toBean(json, clazz);
}
 
Example 12
Source File: DocumentBrowser.java    From book118-downloader with MIT License 4 votes vote down vote up
private String getNextPage(PdfInfo pdfInfo) {
    String nextUrl = pdfInfo.getNextUrl();
    String nextPageJson = HttpUtil.get(nextUrl);
    PageInfo pageInfo = JSONUtil.toBean(nextPageJson, PageInfo.class);
    return pageInfo.getNextPage();
}