iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring-Boot访问外部接口的方案总结
  • 809
分享到

Spring-Boot访问外部接口的方案总结

Spring-Boot访问外部接口SpringBoot外部接口 2022-12-08 20:12:39 809人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录一、简介二、spring-Boot项目中访问外部接口2.1 方案一 采用原生的Http请求2.2 方案二 采用Feign进行消费2.3、方案三 采用RestTemplate方法参

一、简介

在Spring-Boot项目开发中,存在着本模块的代码需要访问外面模块接口,或外部url链接的需求,针对这一需求目前存在着三种解决方案,下面将对这三种方案进行整理和说明。

二、Spring-Boot项目中访问外部接口

2.1 方案一 采用原生的Http请求

在代码中采用原生的http请求,代码参考如下:

@RequestMapping("/doPostGetJSON")
public String doPostGetjson() throws ParseException {
   //此处将要发送的数据转换为json格式字符串
   String jsonText = "{id:1}";
   JSONObject json = (JSONObject) JSONObject.parse(jsonText);
   JSONObject sr = this.doPost(json);
   System.out.println("返回参数:" + sr);
   return sr.toString();
}

public static JSONObject doPost(JSONObject date) {
   HttpClient client = HttpClients.createDefault();
   // 要调用的接口方法
   String url = "http://192.168.1.101:8080/getJson";
   HttpPost post = new HttpPost(url);
   JSONObject jsonObject = null;
   try {
      StringEntity s = new StringEntity(date.toString());
      s.setContentEncoding("UTF-8");
      s.setContentType("application/json");
      post.setEntity(s);
      post.addHeader("content-type", "text/xml");
      HttpResponse res = client.execute(post);
      String response1 = EntityUtils.toString(res.getEntity());
      System.out.println(response1);
      if (res.getStatusLine().getStatusCode() == httpstatus.SC_OK) {
         String result = EntityUtils.toString(res.getEntity());// 返回json格式:
         jsonObject = JSONObject.parseObject(result);
      }
   } catch (Exception e) {
      throw new RuntimeException(e);
   }
   return jsonObject;
}

2.2 方案二 采用Feign进行消费

1、在Maven项目中添加依赖

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-feign</artifactId>
      <version>1.2.2.RELEASE</version>
</dependency>

2、编写接口,放置在service层

@FeignClient(url = "${decisionEngine.url}",name="engine")
public interface DecisionEngineService {
  @RequestMapping(value="/decision/person",method= RequestMethod.POST)
  public JSONObject getEngineMesasge(@RequestParam("uid") String uid,@RequestParam("productCode") String productCode);

}

这里的decisionEngine.url 是配置在properties中的 是ip地址和端口号
decisionEngine.url=http://10.2.1.148:3333
/decision/person 是接口名字

3、在Java的启动类上加上@EnableFeignClients

@EnableFeignClients //参见此处
@EnableDiscoveryClient
@SpringBootApplication
@EnableResourceServer
public class Application   implements CommandLineRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
    @Autowired
    private AppMetricsExporter appMetricsExporter;

    @Autowired
    private AddMonitorUnitService addMonitorUnitService;

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).WEB(true).run(args);
    }    
}

4、在代码中调用接口即可

@Autowired
    private DecisionEngineService decisionEngineService ;
    decisionEngineService.getEngineMesasge("uid" ,  "productCode");

2.3、方案三 采用RestTemplate方法

在Spring-Boot开发中,RestTemplate同样提供了对外访问的接口api,这里主要介绍Get和Post方法的使用。Get请求提供了两种方式的接口getForObject 和 getForEntity,getForEntity提供如下三种方法的实现。
Get请求之——getForEntity(Stringurl,Class responseType,Object…urlVariables)
该方法提供了三个参数,其中url为请求的地址,responseType为请求响应body的包装类型,urlVariables为url中的参数绑定,该方法的参考调用如下:

//http://USER-SERVICE/user?name={1}
getForEntity("http://USER-SERVICE/user?name={1}",String.class,"didi")

Get请求之——getForEntity(String url,Class responseType,Map urlVariables)
该方法提供的参数中urlVariables的参数类型使用了Map类型,因此在使用该方法进行参数绑定时需要在占位符中指定Map中参数的key值,该方法的参考调用如下:

// http://USER-SERVICE/user?name={name)
RestTemplate restTemplate=new RestTemplate();
Map<String,String> params=new HashMap<>();
params.put("name","dada");  //
ResponseEntity<String> responseEntity=restTemplate.getForEntity("http://USERSERVICE/user?name={name}",String.class,params);

Get请求之——getForEntity(URI url,Class responseType)
该方法使用URI对象来替代之前的url和urlVariables参数来指定访问地址和参数绑定。URI是jdk java.net包下的一个类,表示一个统一资源标识符(UnifORM Resource Identifier)引用。参考如下:

RestTemplate restTemplate=new RestTemplate();
UriComponents uriComponents=UriComponentsBuilder.fromUriString(
"http://USER-SERVICE/user?name={name}")
.build()
.expand("dodo")
.encode();
URI uri=uriComponents.toUri();
ResponseEntity<String> responseEntity=restTemplate.getForEntity(uri,String.class).getBody();

Get请求之——getForObject
getForObject方法可以理解为对getForEntity的进一步封装,它通过HttpMessageConverterExtractor对HTTP的请求响应体body内容进行对象转换,实现请求直接返回包装好的对象内容。getForObject方法有如下:

getForObject(String url,Class responseType,Object...urlVariables)
getForObject(String url,Class responseType,Map urlVariables)
getForObject(URI url,Class responseType)

Post请求提供有三种方法,postForEntity、postForObject和postForLocation。其中每种方法都存在三种方法,postForEntity方法使用如下:

RestTemplate restTemplate=new RestTemplate();
User user=newUser("didi",30);
ResponseEntity<String> responseEntity=restTemplate.postForEntity("http://USER-SERVICE/user",user,String.class); //提交的body内容为user对象,请求的返回的body类型为String
String body=responseEntity.getBody();

postForEntity存在如下三种方法的重载

postForEntity(String url,Object request,Class responseType,Object... uriVariables)
postForEntity(String url,Object request,Class responseType,Map uriVariables)
postForEntity(URI url,Object request,Class responseType)

postForEntity中的其它参数和getForEntity的参数大体相同在此不做介绍。

参考文献

1.浅析SpringBoot微服务中异步调用数据提交数据库的问题
2.Spring Boot 常见http请求url参数获取方法
3.Spring学习笔记之RestTemplate使用小结
4.Spring RestTemplate中几种常见的请求方式
5.Springboot使用RestTemplate调用第三方接口的操作代码

到此这篇关于Spring-Boot 访问外部接口的方案总结的文章就介绍到这了,更多相关Spring-Boot 访问外部接内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Spring-Boot访问外部接口的方案总结

本文链接: https://www.lsjlt.com/news/174280.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作