springboot接口调用RestTemplate使用

核心类:

1、application.properties配置

framework.org-sync-url=http://127.0.0.1:8089/datahub/exposeApi/master/rpcOrg/api/orgTree

2、导入配置文件

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @Description 系统属性, 配置文件
 * @Author lxk
 * @Date 2020/3/4 0004
 */
@ConfigurationProperties("framework")
public class FrameworkOrgProperties {

    private String orgSyncUrl;

    public String getOrgSyncUrl() {
        return orgSyncUrl;
    }

    public void setOrgSyncUrl(String orgSyncUrl) {
        this.orgSyncUrl = orgSyncUrl;
    }
}

3、spring加载配置文件

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @Description 系统属性, 加载文件
 * @Author lxk
 * @Date 2020/3/4 0004
 */
@Configuration
@EnableConfigurationProperties(FrameworkOrgProperties.class)
public class FrameworkOrgConfig {

}

4、spring的接口调用工具初始化设置

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @Description Rest接口调用工具初始化
 * @Author lxk
 * @Date 2020/3/4 0004
 */
@Configuration
public class HttpRestTempateConfig {

    @Bean(name = "framework:system:orgSync")
    public RestTemplate customerRestTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

}

5、接口调用

import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.entity.BaseTreeNode;
import com.magus.framework.core.util.collect.CollectionUtils;
import com.magus.framework.core.util.lang.DateUtils;
import com.magus.framework.core.util.lang.StringUtils;
import com.magus.framework.core.webbean.JsonResult;
import com.magus.framework.core.webbean.TreeResult;
import com.magus.framework.system.config.FrameworkOrgProperties;
import com.magus.framework.system.config.SystemConstants;
import com.magus.framework.system.entity.*;
import com.magus.framework.system.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 组织机构与人员重构
 */
@RequestMapping("/system/organizations2")
@RestController("framework:system:Organization2Controller")
public class Organization2Controller extends BaseController {

    @Autowired
    @Qualifier("framework:system:orgSync")
    private RestTemplate customerRestTemplate;

    @Autowired
    private FrameworkOrgProperties properties;

    @GetMapping("/orgSync")
    @ApiOperation(value = "同步组织机构人员", notes = "组织机构")
    public ResponseEntity<JsonResult> orgSyncHandler() {
        String rootUrl = properties.getOrgSyncUrl();
        if (StringUtils.isBlank(rootUrl)) {
            return ResponseEntity.ok(JsonResult.failure("任务执行失败!"));
        }
        TreeResult result = customerRestTemplate.getForObject(rootUrl, TreeResult.class);
        List<BaseTreeNode> treeNodes = (List<BaseTreeNode>) result.getChildren();
        if (treeNodes.size() > 0) {
            saveChildren(treeNodes);
        }
        return ResponseEntity.ok(JsonResult.success("任务执行成功!"));
    }

    
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值