• 后台调用接口进行数据传输


    分send和accept两个项目来说明

    send架构

    TestSend类

    /**
    * 后端测试调用接口
    *
    *
    */
    @RequestMapping(value = "/send")
    @RestController
    public class TestSend {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
    * 发送的是Map包着的List的Josn字符串(可用)
    * Post方式
    */
    @RequestMapping(value = "/send01")
    public void send01(){
    logger.info("进来send01");
    Data data = new Data("hlh",23);
    List<Data> list = new ArrayList<>();
    list.add(data);
    String url = "http://接口ip:8091/accept/accept01";
    Map<String,Object> map = new HashMap<>();
    map.put("list",list);
    String string = JSON.toJSONString(map);
    Send send = new Send();
    String result = send.sendPost(url,string);

    logger.info("result:"+result);//返回被调用接口返回的信息
    }

    /**
    * 发给的是Map格式包着的实体类的Json字符串(可用)
    * Post
    */
    @RequestMapping(value = "/send02")
    public void send02(){
    logger.info("进来send02");
    Data data = new Data("hlh",23);
    String url = "http://接口ip:8091/accept/accept02";
    Map<String,Object> map = new HashMap<>();
    map.put("data",data);
    logger.info("map:"+map);//map:{data=Data(name=hlh, age=23)}
    String string = JSON.toJSONString(map);
    logger.info("string:"+string);//string:{"data":{"age":23,"name":"hlh"}} 转成了json字符串格式
    Send send = new Send();
    String result = send.sendPost(url,string);
    logger.info("result:"+result);//返回被调用接口返回的信息

    }

    /**
    * 发给的是实体类不用Map包着的Json字符串(可用)
    */
    @RequestMapping(value = "/send03")
    public void send03(){
    logger.info("进来send03");
    Data data = new Data("hlh",23);
    String url = "http://接口ip:8091/accept/accept03";
    logger.info("data:"+data);
    String string = JSON.toJSONString(data);
    logger.info("string:"+string);
    Send send = new Send();
    String result = send.sendPost(url,string);
    logger.info("result:"+result);//返回被调用接口返回的信息
    }

    /**
    *
    * 直接实体类传(这个直接报错,不行的)(不可用)
    *
    *
    */
    @RequestMapping(value = "/send04")
    public void send04(){
    logger.info("进来send04");
    Data data = new Data("hlh",23);
    String url = "http://接口ip:8091/accept/accept04";
    logger.info("data:"+data);
    Send send = new Send();
    String result = send.sendPost02(url,data);
    logger.info("result:"+result);//返回被调用接口返回的信息
    }


    /**
    * 传的是List转成的字符串(可用)
    */
    @RequestMapping("/send05")
    public void send05(){
    logger.info("进来send05");
    Data data = new Data("hlh",23);
    List<Data> list = new ArrayList<>();
    list.add(data);
    String url = "http://接口ip:8091/accept/accept05";
    logger.info("list:"+list);
    String string = JSON.toJSONString(list);
    Send send = new Send();
    String result = send.sendPost(url,string);
    logger.info("result:"+result);
    }


    /**
    * * **********************************************************
    * * 体验Spring中 RestTemplate
    *
    * get请求
    * 1.不带参数
    * 2.带参数
    * post请求
    */


    @RequestMapping(value = "/send06")
    public void send06() throws URISyntaxException {

    /**
    * 不带参数get请求,并且能返回一些信息
    */
    logger.info("进来send06");
    RestTemplate rs = new RestTemplate();
    String url="http://接口ip:8091/accept/accept06";
    logger.info("url");
    String str = rs.getForObject(new URI(url),String.class);
    logger.info("notice:"+str); // str:success
    }


    /**
    * get方式 带参数请求
    * @throws URISyntaxException
    */
    @RequestMapping(value = "/send07")
    public void send07() throws URISyntaxException {

    /**
    * 带参数get请求,并且能返回一些信息
    */
    logger.info("进来send07");
    RestTemplate rs = new RestTemplate();
    String url="http://接口ip:8091/accept/accept07?name={name}&age={age}";
    Map<String,Object> map = new HashMap<>();
    map.put("name","hlh");
    map.put("age",23);
    String str = rs.getForObject(url,String.class,map);
    logger.info("notice:"+str); // str:success
    }


    /**
    * post方式
    * 1.带实体参数请求
    * 接收实体一定要有无参构造,才能够反序列化接收到数据
    * 这里测试接收是实体类
    */
    @RequestMapping("/send08")
    public void send08(){

    logger.info("进来send08");
    RestTemplate rs = new RestTemplate();
    String url="http://接口ip:8091/accept/accept08";
    RequestEntity requestEntity = new RequestEntity("hlh",23,178);
    RequestEntity requestEntity1 = rs.postForObject(url,requestEntity,RequestEntity.class);
    logger.info("name:"+requestEntity1.getName()+",age:"+requestEntity1.getAge());//name:hlh,age:23
    }


    /**
    * post方式
    * 1.带实体参数请求
    * 接收实体一定要有无参构造,才能够反序列化接收到数据
    * 这里测试接收是Map
    *
    * 总计:
    * 说明Map和实体类没区别在发送数据和接收数据,实体类必须还要加无参构造才行
    */
    @RequestMapping("/send09")
    public void send09(){

    logger.info("进来send09");
    RestTemplate rs = new RestTemplate();
    String url="http://接口ip:8091/accept/accept09";
    RequestEntity requestEntity = new RequestEntity("hlh",23,178);
    RequestEntity requestEntity1 = rs.postForObject(url,requestEntity,RequestEntity.class);
    logger.info("name:"+requestEntity1.getName()+",age:"+requestEntity1.getAge());//name:hlh,age:23
    }





    /**
    * post方式
    * 这个是测试一个实体类中包着基本类型,实体类型,List类型进行数据传送
    *
    */

    @RequestMapping("/send10")
    public void send010(){
    logger.info("进来send10");//进来send10
    RestTemplate rs = new RestTemplate();
    String url="http://接口ip:8091/accept/accept10";
    RequestEntity re1 = new RequestEntity("hlh",10,120);
    RequestEntity re2 = new RequestEntity("hlh2",11,130);
    List<RequestEntity> list = new ArrayList<>();
    list.add(re1);
    list.add(re2);
    Data data = new Data("hlh3",140);
    SendModel sendModel = new SendModel();
    sendModel.setCode("500");
    sendModel.setStatus("ok");
    sendModel.setData(data);
    sendModel.setListEntity(list);

    String str = rs.postForObject(url,sendModel,String.class);
    logger.info("str:"+str);//str:success
    }

    }

    Data类

     RequestEntity类

     SendModel类

    Send类

    /**
    * 发送post请求
    */
    public static String sendPost(String url, String param) {
    PrintWriter out = null;
    BufferedReader in = null;
    String result = "";
    try {
    URL realUrl = new URL(url);
    URLConnection conn = realUrl.openConnection();
    conn.setRequestProperty("accept", "*/*");
    conn.setRequestProperty("connection", "Keep-Alive");
    conn.setRequestProperty("Content-Type", "application/json");// 我加的一个头,传入的是json格式的话,得要加这个,后端接收就得使用@RequestBody
    conn.setRequestProperty("user-agent",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

    conn.setDoOutput(true);
    conn.setDoInput(true);
    out = new PrintWriter(conn.getOutputStream());
    out.print(param);//已经把信息传给了被调用接口
    out.flush();
    in = new BufferedReader(
    new InputStreamReader(conn.getInputStream()));//这里是读入被调用接口返回的信息
    String line;
    while ((line = in.readLine()) != null) {//返回的信息
    result += line;
    }
    } catch (Exception e) {
    System.out.println("异常信息:" + e);
    e.printStackTrace();
    }
    finally{
    try{
    if(out!=null){
    out.close();
    }
    if(in!=null){
    in.close();
    }
    }
    catch(IOException ex){
    ex.printStackTrace();
    }
    }
    System.out.println("result:"+result);
    return result;
    }

    accept架构

    TestAccept类


    /**
    * 接收类
    *Map和实体类接收区别不大,都可以进行接收,但实体类接收必须要有无参构造才行
    */
    @RequestMapping("/accept")
    @RestController
    public class TestAccept {


    Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
    * List
    * @param str
    * @return
    */
    @RequestMapping(value = "/accept01",method = RequestMethod.POST)
    public String accept01(@RequestBody String str){
    logger.info("str:"+str);//str:{"list":[{"age":23,"name":"hlh"}]}
    //解析出来
    Map map = (Map<String, Object>) JSON.parse(str);
    logger.info("map:"+map);//map:{"list":[{"name":"hlh","age":23}]}
    List<Map<String,Object>> list = (List<Map<String,Object>>) map.get("list");

    logger.info("list:"+list);//list:[{"name":"hlh","age":23}]

    for(int i=0;i<list.size();i++){

    logger.info("map2"+i+":"+list.get(i));//map20:{"name":"hlh","age":23}
    logger.info("name:"+list.get(i).get("name")+",age:"+list.get(i).get("age"));//name:hlh,age:23
    }

    /**
    * 这种有问题,实体类转换不了
    */
    // List<Data> list1 = (List<Data>) map.get("list");
    // for(Data ls:list1){
    // logger.info("name:"+ls.getName()+",age"+ls.getAge());
    // }
    return "success";
    }

    /**
    * 实体类用Map包着的Josn字符串
    * @param str
    * @return
    */
    @RequestMapping(value = "/accept02")
    public String accept02(@RequestBody String str){
    logger.info("str:"+str);//str:{"data":{"age":23,"name":"hlh"}}
    Map<String,Object> map = (Map)JSON.parse(str);
    logger.info("map:"+map);//map:{"data":{"name":"hlh","age":23}}
    Map<String,Object> map1 = (Map)map.get("data");
    logger.info("map1:"+map1);//map1:{"name":"hlh","age":23}
    logger.info("name:"+map1.get("name")+",age:"+map1.get("age"));//name:hlh,age:23
    return "success";
    }


    /**
    * 实体类不用Map包着
    * @param str
    * @return
    */
    @RequestMapping(value = "/accept03")
    public String accept03(@RequestBody String str){
    logger.info("str:"+str);//string:{"age":23,"name":"hlh"}
    Map<String,Object> map = (Map)JSON.parse(str);
    logger.info("map:"+map);//map:{"name":"hlh","age":23}
    logger.info("name:"+map.get("name")+",age:"+map.get("age"));//name:hlh,age:23
    return "success";
    }

    /**
    * 实体类直接传,传报错了
    * @param data
    * @return
    */
    @RequestMapping(value = "/accept04")
    public String accept04(@RequestBody Data data){
    logger.info("str:"+data);
    return "success";
    }

    /**
    * List转成JSON字符串传过来
    * @param str
    * @return
    */
    @RequestMapping(value = "/accept05")
    public String accept05(@RequestBody String str){
    logger.info("str:"+str);//str:[{"age":23,"name":"hlh"}]
    List<Map<String,Object>> list = (List)JSON.parse(str);
    logger.info("list:"+list);//list:[{"name":"hlh","age":23}]
    for(int i=0;i<list.size();i++){
    logger.info("name:"+list.get(i).get("name")+",age"+list.get(i).get("age"));//name:hlh,age23
    }
    return "success";
    }

    /**
    * 不带参数get
    * @return
    */
    @RequestMapping(value = "/accept06")
    public String accept06(){
    logger.info("进来accept06");
    return "success";
    }


    /**
    * get请求带参数
    * @param name
    * @param age
    * @return
    */
    @RequestMapping(value = "/accept07")
    public String accept07(String name ,Integer age){
    logger.info("进来accept07");//进来accept07
    logger.info("name:"+name);//name:hlh
    logger.info("age:"+age);//age:23
    return "success";
    }


    /**
    * 用实体来接收一个实体传来的数据,
    * 在接收方实体类中必须提供一个无参数构造,因为JackSon要求反序列要提供无参构造,
    * 那么response也是一样的
    * @param data
    * @return
    */
    @RequestMapping(value = "/accept08",method = RequestMethod.POST)
    public Data accept08(@RequestBody Data data){
    logger.info("data:"+data);//data:{name=hlh, age=23, height=178}
    return data;
    }

    /**
    * 和上面用实体类来接收,发Map和实体类区没区别,都可以用来接收数据
    * @param map
    * @return
    */
    @RequestMapping(value = "/accept09" ,method = RequestMethod.POST)
    public Map<String,Object> accept09(@RequestBody Map<String,Object> map){
    logger.info("data:"+map);//data:{name=hlh, age=23, height=178}
    return map;
    }


    @RequestMapping(value = "/accept10",method = RequestMethod.POST)
    public String accept10(@RequestBody AcceptModelDTO acceptModelDTO){
    logger.info("进来");
    logger.info("code:"+acceptModelDTO.getCode());//code:500
    logger.info("status:"+acceptModelDTO.getStatus());//status:ok
    logger.info("name:"+acceptModelDTO.getData().getName()+",age:"+acceptModelDTO.getData().getAge());//name:hlh3,age:140
    List<AcceptEntity> list =acceptModelDTO.getListEntity();
    for(AcceptEntity ae:list){
    /**
    * 结果
    * name:hlh,age:10,height:120
    * name:hlh2,age:11,height:130
    */
    logger.info("name:"+ae.getName()+",age:"+ae.getAge()+",height:"+ae.getHeight());
    }
    return "success";
    }

    }

     Data类

    AcceptData类

     AcceptEntity类

     AcceptModelDTO类

    完成

  • 相关阅读:
    tomcat8.5.57源码阅读笔记2
    tomcat8.5.57源码阅读笔记1
    KVM openstack
    爬虫进阶版
    react 之setChild子组件传值父组件
    Linux找死锁、cpu100%
    Java定时任务
    Java工具类HttpUtil
    Java后台远程下载url文件并远程上传文件
    jQuery上传文件
  • 原文地址:https://www.cnblogs.com/yiyezhiqiuwuchen/p/12599070.html
Copyright © 2020-2023  润新知