开发者

springboot发送request请求的方式小结

目录
  • 前言
  • hutool和RestTemplate
  • 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,
                                  @RequesjavascripttParam(name = "grantType") String grantType) {
        String post = HttpUtil.get("http://ip:8068/getKdToken?appId=" + appId + "&appSecret=" + a编程客栈ppSecret + "&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://ipandroid:8068/eleRechargeMoneyAllList?elemeterId=" + elemeterId + "&money=" + money + "&adds=0&selOrderno=" + selOrderno + "&payType=40")
                .header("Authorization", Authorization)
                .header("token", token)
http://www.devze.com                .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 Http编程Entity<>(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请求内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜