美文网首页
restful template

restful template

作者: 抽刀断水2 | 来源:发表于2021-12-01 22:46 被阅读0次

    fiddler代理

    RestTemplate restTemplate = new RestTemplate();
    SimpleClientHttpRequestFactory reqfac = new SimpleClientHttpRequestFactory();
    reqfac.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)));
    restTemplate.setRequestFactory(reqfac);
    

    三种类型

    image.png

    object

            RequestObject request = new RequestObject();
            request.setAge(18);
            request.setName("小芳");
            request.setAddress("广东深圳");
            RestTemplate restTemplate = new RestTemplate();
    
            HttpEntity request2 = new HttpEntity<>(request);
    
            String url = "http://localhost:8080/user/object";
            ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, request2, String.class);
            System.out.println(result);
    

    map

    Map request = new HashMap<>();
            request.put("age", 18);
            request.put("name", "小芳");
            request.put("address", "广东深圳");
            RestTemplate restTemplate = new RestTemplate();
            HttpEntity request2 = new HttpEntity<>(request);
    
            String url = "http://localhost:8080/user/map";
            ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, request2, String.class);
    

    form

    HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    
            MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
            map.add("id", "11");
    
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
    
            String fooResourceUrl
                    = "http://localhost:8080/user/form";
    
            ResponseEntity<String> result = restTemplate.postForEntity(
                    fooResourceUrl , request , String.class);
    
            System.out.println("result:" + result);
    

    相关文章

      网友评论

          本文标题:restful template

          本文链接:https://www.haomeiwen.com/subject/mzmexrtx.html