接口调用三种方式

创建服务端

一个controller,一个启动类,配置端口

  • controller
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServerController {

    @RequestMapping("/server/test1")
    public String getStr(String a){
        System.out.println("请求到server端");
        return a;
    }

}

原始httpclient请求

参考链接:httpClient

只需要一个controller测试发送请求

  • controller
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@RestController
public class HttpController {

    @RequestMapping("/http/test1")
    public String test1(String a) throws IOException {
    	//服务端的地址
        String urlPath="http://192.168.3.32:7071/server/test1?a="+a;
        //CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
        /*
            HttpClients是一个工具类,等同上面的方法
            可关闭的httpclient客户端,相当于打开的一个浏览器
         */
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();

        //httpGet
        HttpGet httpGet=new HttpGet(urlPath);

        //可关闭的响应:DecopressingEntity
        CloseableHttpResponse response=null;
        try {
            //发送请求
            response=closeableHttpClient.execute(httpGet);
            //获取响应结果:HttpEntity不仅可以作为结果,也可以作为请求的参数实体,有很多的实现
            HttpEntity entity = response.getEntity();
            //对HttpEntity操作的工具类,转成string看下结果
            String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            System.out.println(toStringResult);
            return toStringResult;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (closeableHttpClient!=null){
                closeableHttpClient.close();
            }
            if(response!=null){
                response.close();
            }
        }
        return "null";
    }
}

接口调用三种方式_第1张图片

RestTemplate调用接口

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RestTemplateController {

    @RequestMapping("/rest/test1")
    public String test1(String a){

        String urlPath="http://192.168.3.32:7071/server/test1?a="+a;

        // 创建 RestTemplate 实例
        RestTemplate restTemplate = new RestTemplate();


        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
        ResponseEntity<String> forEntity = restTemplate.postForEntity(urlPath,httpEntity,String.class);
        return forEntity.getBody();
    }

}

接口调用三种方式_第2张图片

feign调用接口

需要一个service接口使用注解配置服务端的相关信息,还需要在启动类使用注解@EnableFeignClients

引入pom

  • pom
    要用openfeign,而不是feign,feign早就废弃了
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
  • service
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

//服务端请求接口的地址为192.168.3.32:7071/server/test1
@Service
//url为要请求接口的ip和端口,name随便
@FeignClient(url = "192.168.3.32:7071",name = "client")
public interface ClientService {

	//请求的接口名称,对应服务端的controller
    @RequestMapping("/server/test1")
    @ResponseBody
    //需要绑定参数,不然可能收到的值为null
    public String getStr(@RequestParam("a")String a);
}
  • controller
import com.wzw.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ClientController {

    @Autowired
    private ClientService service;

    @RequestMapping("/client/getStr")
    public String getStr(String a){
        System.out.println(a);
        return service.getStr(a);
    }

}

接口调用三种方式_第3张图片

你可能感兴趣的:(http,接口调用)