服务间的Api接口调用的两种方式Get和Post

2022-12-20 17:08
363
0

在pom.xml文件中引入依赖包

<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-client</artifactId>
  <version>2.22.2</version>
</dependency> 

 

通过Get 方式获取Api返回内容

String htmlContent=ClientBuilder.newClient()
        .target("https://api.example.coom/v1/hello")
        .request()
        .get().readEntity(String.class)

 

通过Post方式获取Api返回内容

String htmlContent=ClientBuilder.newClient() .target("https://api.example.coom/v1/saveUser") .request().post(Entity.entity("{\"userName\":\"aaa\",\"age\":18}", MediaType.APPLICATION_JSON_TYPE)).readEntity(String.class)

全部评论