java远程调用

来源:互联网 发布:工信部域名备案系统 编辑:程序博客网 时间:2024/06/16 10:12

远程调用是为在java业务处理过程中,在后台调用其他项目api的工具。


1、在pom.xml注入依赖。

        <dependency>            <groupId>org.jodd</groupId>            <artifactId>jodd-http</artifactId>            <version>3.6.6</version>        </dependency>        <dependency>            <groupId>org.jodd</groupId>            <artifactId>jodd-json</artifactId>            <version>3.6.6</version>        </dependency>        <dependency>            <groupId>org.jodd</groupId>            <artifactId>jodd-bean</artifactId>            <version>3.6.6</version>        </dependency>

2、在java代码中调用

   //api路径   String requestUrl="api";   //请求参数   Map<String,String> map=new HashMap<>();   //get请求  HttpResponse response = HttpRequest.get(requestUrl)              .basicAuthentication("账号","密码")//需要账号、密码验证时,需要放入              .query(map)              .send();  //post请求
  HttpResponse response = HttpRequest.post(requestUrl)              .basicAuthentication("账号","密码")//需要账号、密码验证时,需要放入              .query(map)              .send();
 if (response.statusCode() == 200) { String responseBody = rsp.charset("utf-8").bodyText(); }

 
原创粉丝点击