读取yml 的文件静态方法调用

yml 文件

在这里插入图片描述
写一个实体类

package org.jeecg.modules.ddp.entity;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * User: Administrator
 * Date: 2021/6/1/001
 * Time: 15:39
 * Description:
 */
@Data
@Component
@ConfigurationProperties(prefix = "parameter")
public class TableuaConfig {

    /**
     * tableua服务器地址
     */
    private String serverAddress;

    /**
     * tableua用户名
     */
    private String name;
    /**
     * tableua信任ip
     */
    private String trustIp;

    /**
     * 单点登录-视图地址
     */
    private String viewAddress;

静态方法调用

package org.jeecg.modules.ddp.utils;

import org.jeecg.modules.ddp.entity.TableuaConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * User: HZX
 * Date: 2021/5/25/025
 * Time: 13:34
 * Description:获取票据
 * @author Administrator
 */
@Component
public class GetTableuaBill {

    private final static Logger LOGGER = LoggerFactory.getLogger(GetTableuaBill.class);


    /**
     * 注入参数
     */
    private static TableuaConfig tableuaConfig;
    @Autowired
    private void getTableuaConfig(TableuaConfig tableuaConfig) {
        GetTableuaBill.tableuaConfig = tableuaConfig;
    }


    /**
     * restTemplate http工具
     */
    @Resource
    private RestTemplate restTemplate;

    private static GetTableuaBill codeMapUtils;

    @PostConstruct
    public void init() {
        codeMapUtils = this;
        codeMapUtils.restTemplate = this.restTemplate;
    }



    public static String getBill(){


        String name = tableuaConfig.getName();
        String serverAddress = tableuaConfig.getServerAddress();
        String trustIp = tableuaConfig.getTrustIp();
        LOGGER.info("tableua服务器地址:"+ serverAddress +"   tableua用户名:"+ name +"  tableua信任ip:"+ trustIp);
        // 请求头设置,x-www-form-urlencoded格式的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("username", name);
        map.add("client_ip", trustIp);
        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request =
                new HttpEntity<MultiValueMap<String, String>>(map, headers);
        // 发送post请求,并打印结果,以String类型接收响应结果JSON字符串
        String result = codeMapUtils.restTemplate.postForObject(serverAddress, request, String.class);
        LOGGER.info("获取的票据是:"+result);
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值