核心类:
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("任务执行成功!"));
}
}