Spring Boot 发起http请求

向别的后端发起http请求,需要用到http请求,记一下。

http请求
  • 下面,包含了get和post,就不分开说了。com.alibaba.fastjson.JSONObject这个没有的话,去maven仓库搜一下,加入pom.xml里。
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * 以http发起post和get请求的类
 */
@Service
public class PostData {
    /**
     * 发送的数据之中,可能有字段含有数组
     */
    public void postArray() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8888/";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("name", "刘");
        map.add("name", "流");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, httpHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        System.out.println(response.getBody());
//        {"name":["刘","流"]} 发送的数据
//        {"name":"刘"} 接收到的数据
    }

    /**
     * post发送简单的无数组的数据,数据接口可以使用HttpEntity后面的进行定制
     */
    public void post() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8888/";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "无数组");
        HttpEntity<String> request = new HttpEntity<>(jsonObject.toString(), httpHeaders);
        ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.POST, request, JSONObject.class);
        System.out.println(response.getBody());
//        {"name":"无数组"} 发送的数据
//        {"name":"刘"} 接收的数据
    }

    /**
     * 无参数get输入
     */
    public void get() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8888/he";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(httpHeaders);
        ResponseEntity<JSONObject> response = restTemplate.exchange(url, HttpMethod.GET, request, JSONObject.class);
        System.out.println(response.getBody());
//        {"name":"刘"} 接收到的数据
    }
}
  • 写一下,接收请求的controller。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.Dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//Controller会调用视图解析器,而RestController不会调用,直接返回return的内容
@RestController
public class UserController {
    UserDao userDao;

    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    //测试post
    @RequestMapping(method = RequestMethod.POST, value = "/")
    @ResponseBody
    public JSONObject test(@RequestBody JSONObject map){
        System.out.println(map.toString());
        JSONObject test = new JSONObject();
        test.put("name", "刘");
        return test;
    }
    //测试get
    @RequestMapping(method = RequestMethod.GET, value = "/he")
    public JSONObject test1(){
        JSONObject test = new JSONObject();
        test.put("name", "刘");
        return test;
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值