java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot发送request请求

springboot发送request请求的方式小结

作者:码里法

在Java中,发送HTTP请求是常见需求,hutool工具包和RestTemplate类是实现此功能的两种主流方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

java发送请求的方法有很多,这里只介绍两种。

hutool和RestTemplate

下边提供两种后端发送请求的方式,一个是基于hutool工具的,一个是基于RestTemplate的,为什么要写这两种呢,因为有的时候用hutool的方式不太管用,有的时候用RestTemplate也不太管用,所以就且换着用,谁能用,用谁。

hutool方式

get请求

	@GetMapping("/userEleList")
    @ResponseBody
    public JSONObject userEleList(@RequestParam(name = "userCode") String userCode, HttpServletRequest request) {
        String Authorization = request.getHeader("Authorization");
        String token = request.getHeader("token");
        String body = HttpUtil.createGet("http://ip:8068/userEleList?userCode=" + userCode)
                .header("Authorization", Authorization)
                .header("token", token)
                .execute()
                .body();
        return JSONObject.parseObject(body);
    }
	@GetMapping("/getKdToken")
    @ResponseBody
    public JSONObject userEleList(@RequestParam(name = "appId") String appId,
                                  @RequestParam(name = "appSecret") String appSecret,
                                  @RequestParam(name = "grantType") String grantType) {
        String post = HttpUtil.get("http://ip:8068/getKdToken?appId=" + appId + "&appSecret=" + appSecret + "&grantType=" + grantType);
        return JSONObject.parseObject(post);
    }

post请求

	@PostMapping("/eleRechargeMoneyAllList")
    @ResponseBody
    public JSONObject eleRechargeMoneyAllList(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        String Authorization = request.getHeader("Authorization");
        String token = request.getHeader("token");
        Object elemeterId = map.get("elemeterId");
        Object money = map.get("money");
        Object selOrderno = map.get("selOrderno");
        String post = HttpUtil
                .createPost("http://ip:8068/eleRechargeMoneyAllList?elemeterId=" + elemeterId + "&money=" + money + "&adds=0&selOrderno=" + selOrderno + "&payType=40")
                .header("Authorization", Authorization)
                .header("token", token)
                .execute()
                .body();
        return JSONObject.parseObject(post);
    }
	@PostMapping("/GetClientByCnumber")
	@ResponseBody
	 public JSONObject GetClientByCnumber(@RequestBody Map&lt;String, Object&gt; map) {
	     String post = HttpUtil.post("http://ip:8006/GetClientByCnumber", map);
	     return JSONObject.parseObject(post);
	 }

RestTemplate方式

	@PostMapping("/userPricePay")
	@ResponseBody
	public JSONObject userPricePay(@RequestBody Map<String, Object> map, HttpServletRequest request) {
		   String sign = request.getHeader("sign");
		   RestTemplate restTemplate = new RestTemplate();
		   // 设置请求头,指定Content-Type为application/json
		   HttpHeaders headers = new HttpHeaders();
		   headers.setContentType(MediaType.APPLICATION_JSON);
		//        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		   headers.set("sign", sign);
		   // 将解析后的 JSON 对象转换为 MultiValueMap
		   HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(map, headers);
		   ResponseEntity<String> exchange = restTemplate.exchange("https://ip:8080/userPricePay", HttpMethod.POST, requestEntity, String.class);
		   return JSONObject.parseObject(exchange.getBody());
	}

到此这篇关于springboot发送request请求的方式小结的文章就介绍到这了,更多相关springboot发送request请求内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文