【Java】post请求示例

2023-12-23 17:55:10

使用上一篇文章分享的RestTemplate工具 发送post请求示例。

已知调用接口:http://www.ccb.com/userInfoConstruct/getUserInfo,传参方式为body形式。参数封装
@Data
public class UserDto {

    //用户唯一编码
    private String uniqueCode;

    //对应用户的唯一安全识别码
    private String accountCode;

    //账户真实名称
    private String name;
    
    //用户的星级:红石:(省正副级以上), 玉硅(军职级),翡金(国家处级),至尊(业务:行长),白金(业务:副行长),黄金:客服一星级经理
    private String accountLevel;

}
参数形式:body,请求方式 post
public class RestTemplateUtilsDemo {
    public static void main(String[] args) {
        //获取建设银行的用户信息
        String url = "http://www.ccb.com/userInfoConstruct/getUserInfo";

        UserDto dto = new UserDto();
        dto.setAccountCode("15835965487965649466154");
        dto.setName("至尊人物,贷款要降低至1.8个点+送一瓶飞天");
        dto.setUniqueCode("15835965487965649466154");
        dto.setAccountLevel("至尊");
        
        //请求头设置
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        httpHeaders.add("Authorization", "Bearer " + "token");
        httpHeaders.add("bankSign", "bankSign");
        httpHeaders.add("level", dto.getAccountLevel());
        
        //参数处理完成,然后请求接口
        log.info("请求建行用户信息:链接:{},dto:{}",url, JSON.toJSONString(dto));
        String jsonObject = RestTemplateUtils.post(null, url, dto, httpHeaders, String.class);
        log.info("请求建行用户信息:{}",JSON.toJSONString(jsonObject));

    }
}
调用接口:http://www.ccb.com/userInfoConstruct/queryAccountBalance,传参方式为query形式,参数封装
@Data
public class AccountBalanceDto {

    //用户唯一编码
    private String uniqueCode;

    //对应用户的唯一安全识别码
    private String accountCode;

    //账户号
    private String accountNumber;

}

参数形式:query,请求方式 post
public class RestTemplateUtilsDemo {
    public static void main(String[] args) {
        //获取账户的余额
        String url = "http://www.ccb.com/userInfoConstruct/queryAccountBalance";

        AccountBalanceDto balanceDto = new AccountBalanceDto();
        balanceDto.setAccountCode("15835965487965649466154");
        balanceDto.setUniqueCode("15835965487965649466154");
        balanceDto.setAccountNumber("6217487965649466154");

        //请求头设置
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        httpHeaders.add("Authorization", "Bearer " + "token");
        httpHeaders.add("bankSign", "bankSign");


        LinkedMultiValueMap<String, String> queryParamMap = new LinkedMultiValueMap<>();
        queryParamMap.add("uniqueCode",balanceDto.getUniqueCode());
        queryParamMap.add("accountCode",balanceDto.getAccountCode());
        queryParamMap.add("accountNumber",balanceDto.getAccountNumber());

        //参数处理完成,然后请求接口
        log.info("请求账户余额:链接:{},dto:{}",url, JSON.toJSONString(queryParamMap));
        String jsonObject = RestTemplateUtils.post(null, url, queryParamMap, httpHeaders, String.class);
        log.info("请求账户余额结果:{}",JSON.toJSONString(jsonObject));

    }
}
query的原始形式:
获取账户的余额
    String url = "http://www.ccb.com/userInfoConstruct/queryAccountBalance?uniqueCode=xxx&accountCode=xxx&accountNumber=xxxx";
public class RestTemplateUtilsDemo {
    public static void main(String[] args) {
        //获取账户的余额
        String url = "http://www.ccb.com/userInfoConstruct/queryAccountBalance";

        AccountBalanceDto balanceDto = new AccountBalanceDto();
        balanceDto.setAccountCode("15835965487965649466154");
        balanceDto.setUniqueCode("15835965487965649466154");
        balanceDto.setAccountNumber("6217487965649466154");

        //请求头设置
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        httpHeaders.add("Authorization", "Bearer " + "token");
        httpHeaders.add("bankSign", "bankSign");


        StringBuilder sb = new StringBuilder();
        sb.append(url)
                .append("?uniqueCode=")
                .append(balanceDto.getUniqueCode())
                .append("&accountCode=")
                .append(balanceDto.getAccountCode())
                .append("&accountNumber=")
                .append(balanceDto.getAccountNumber());

        //参数处理完成,然后请求接口
        log.info("请求账户余额:链接:{},dto:{}",url, JSON.toJSONString(balanceDto));
        String jsonObject = RestTemplateUtils.post(null, sb.toString(),null,null, httpHeaders, String.class);
        log.info("请求账户余额结果:{}",JSON.toJSONString(jsonObject));
    }
}

根据业务不同,传参不同,来分别满足自己的业务即可。

文章来源:https://blog.csdn.net/like_start/article/details/135165471
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。