springboot发送request请求的方式

前言

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<String, Object> 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());
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot发送Ajax请求,你可以按照以下步骤进行操作: 1. 首先,在前端页面中引入jQuery库,你可以使用如下方式引入:。 2. 接着,编写发送Ajax请求JavaScript代码。你可以使用jQuery的`$.ajax()`方法或`$.post()`方法来发送请求。以下是一个示例代码: ```javascript $.ajax({ url: "your-url", // 替换为你的请求地址 type: "POST", // 请求方法,可以是GET、POST等 data: { // 请求参数,如果有的话 key1: value1, key2: value2 }, success: function(response) { // 请求成功后的处理逻辑 console.log(response); }, error: function(xhr, status, error) { // 请求失败后的处理逻辑 console.log(error); } }); ``` 或者使用`$.post()`方法: ```javascript $.post("your-url", { key1: value1, key2: value2 }) .done(function(response) { // 请求成功后的处理逻辑 console.log(response); }) .fail(function(xhr, status, error) { // 请求失败后的处理逻辑 console.log(error); }); ``` 3. 在Spring Boot后端,编写对应的Controller来处理Ajax请求。你可以使用`@RestController`注解来标识一个Controller类,并使用`@RequestMapping`注解指定请求的URL路径。以下是一个示例代码: ```java @RestController public class AjaxController { @RequestMapping(value = "/your-url", method = RequestMethod.POST) public String handleAjaxRequest(@RequestParam("key1") String value1, @RequestParam("key2") String value2) { // 处理Ajax请求的业务逻辑 // 返回响应结果 return "Response"; } } ``` 4. 最后,你可以根据具体的业务需求进行处理Ajax请求的逻辑。在Controller中,你可以根据请求参数进行相应的处理,并返回响应结果。 请注意,为了解决Ajax发送跨域请求的问题,你可能还需要在后台添加相应的配置。根据引用提供的信息,你可以在前端Ajax请求中添加如下代码:`xhrFields: {withCredentials: true}`。此外,在后台服务器中,你还需要设置相关的跨域配置。 希望以上信息对你有帮助!如有更多问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值