首页 > 其他分享 >spring boot项目访问外部http请求的解决方案

spring boot项目访问外部http请求的解决方案

时间:2023-05-24 21:04:23浏览次数:58  
标签:http String url spring boot 接口 quote public


Spring-Boot项目开发中,存在着本模块的代码需要访问外面模块接口,或外部url链接的需求。针对这一需求以前的做法就是访问外部请求都要经过httpClient 需要专门写一个方法,来发送http请求,这个就不说了,网上一搜全都是现成的方法。springboot 实现外部http请求 是通过FeignClient来请求http数据的,特别简单并且非常实用的一个注解就可以搞定。

采用Feign进行消费

第一步:在maven项目中添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>

第二步:编写接口,放置在service层

@FeignClient(url="${crm.server.url}",name="crmServer")
public interface naturalVisitCustomerService {
    @RequestMapping(value = "/view/naturalVisit/addNaturalVisitCustomer",method = RequestMethod.POST)
    public JSONObject saveCustomer(@RequestParam("customerName") String customerName, @RequestParam("customerPhone") String customerPhone,
                                   @RequestParam("source") String source, @RequestParam("intention") String intention);
}

这里的crm.server.url 是配置在properties配置文件中的  结构是ip地址和端口号

crm.server.url = https://devcrm.yijia.com:5555

/view/naturalVisit/addNaturalVisitCustomer  是接口名字   

第三步:要在启动的java类上加 @EnableFeignClients这个注解

@SpringBootApplication(exclude = PageHelperAutoConfiguration.class)
@MapperScan("com.yijia.website.module.*.dao")
@EnableTransactionManagement
@EnableCaching
@EnableRedisHttpSession
@EnableFeignClients
public class CmsApplication {

	public static void main(String[] args){
		SpringApplication.run(CmsApplication.class, args);
	}
}

第四步:在需要调用的地方调用刚刚写的接口

@ResponseBody
    @RequestMapping("/saveQuote")
    public String saveQuoteInfo(Quote quote){
        quote.setCreateTime(new Date());
        if (quote.getQuoteId() != null){
            return quoteService.updateQuote(quote);
        }
        addCustomerToCRM(quote);
        return quoteService.saveQuote(quote);
    }


    // 新加入的方法,专门用于调用刚刚写的放入到service层的接口
    private void addCustomerToCRM(Quote quote) {
        String customerName = Optional
                .ofNullable(quote.getCustomerName())
                .orElse("自然到访客户");
        try{
            JSONObject result = naturalVisitCustomerService.saveCustomer(customerName, quote.getCustomerPhone(), CmsConst.SITE, CmsConst.CONSUMER);
            log.info("自然到访客户接口 保存客户 结果 === {}",result.toString());
        }catch (Exception e){
            log.error("自然到访客户接口 保存客户 异常 === {}",e.getMessage());
        }
    }

参考文献

spring boot 访问外部http请求

标签:http,String,url,spring,boot,接口,quote,public
From: https://blog.51cto.com/u_16128050/6342862

相关文章

  • 【老王读SpringMVC-5】Controller method 是如何执行的?
    通过前面对Controllermethod参数绑定的分析,我们知道,被@RequestMapping标记handlermethod的执行是通过调用RequestMappingHandlerAdapter#handle()。RequestMappingHandlerAdapter#handle()具体的调用过程如下:参数解析、handlermethod的执行和对返回值的处理,最终......
  • c#中用System.Diagnostics.Process.Start(Path.GetFullPath(“vlc.exe.lnk“), url);用
    vlc.exe.lnk双击这个文件,能正常打开vlc,但是用System.Diagnostics.Process.Start(Path.GetFullPath("vlc.exe.lnk"),url);没有任何反应。根据常理,不应该出现这个问题。但是现实就是这么魔幻,偏偏有这个问题。根据上面图,根据快捷方式是可以获取到vlc可执行文件的路径的,然后在网上搜索......
  • spring-boot配置文件中server.context-path不起作用的解决方案
    背景:server.context-path不起作用简单说springboot项目路径默认是ip:port进入项目,通过在application配置文件添加server.context-path属性,可自定义上下文,如ip:port/server.context-path而springboot2.0之后,上下文的配置改为了server.servlet.context-path。  如果还是不懂可......
  • c#中用System.Diagnostics.Process.Start(Path.GetFullPath(“vlc.exe.lnk“), url);用
    vlc.exe.lnk双击这个文件,能正常打开vlc,但是用System.Diagnostics.Process.Start(Path.GetFullPath("vlc.exe.lnk"),url);没有任何反应。根据常理,不应该出现这个问题。但是现实就是这么魔幻,偏偏有这个问题。根据上面图,根据快捷方式是可以获取到vlc可执行文件的路径的,然后在网上......
  • springboot常用注解
    SpringBoot是基于Spring框架的部分组件进行封装,所以大部分注解都是Spring框架中常用的注解。下面是SpringBoot中常用的注解:-----------------------------------------------------------------------1、@SpringBootApplication:这个注解是SpringBoot应用的入口标志,......
  • C# http请求
     ///<summary>    ///GET方式发送得结果    ///</summary>    ///<paramname="url">请求的url</param>    publicstaticstringDoGetRequestSendData(stringurl)    {      HttpWebRequesthwRequest=null;  ......
  • springcloud 第一个项目遇到的报错
    在创建模块springcloud-provider-8001的时候出现两个错误,一个是CLASSNOTFOUND,另一个是错误的类文件:/D:/apache-maven-3.8.8/maven-repo/org/springframework/spring-beans/6.0.4/spring-beans-6.0.4.jar!/org/springframework/beans/factory/annotation/Autowired.class   ......
  • 【服务治理】基于SpringCloudAlibaba微服务组件的灰度发布设计(二)
    一.背景在上文中,灰度发布遇到了些问题,例如:1.动态修改Nacos配置中心的元数据信息,如何同步到Nacos注册中心对应服务的列表中2.管理后台业务调用其它服务灰度实例时的路由规则二.解决方案//TODO ......
  • 微服务框架SpringCloud-2-服务拆分及远程调用-2.2服务远程调用
    微服务框架SpringCloud2服务拆分及远程调用2.2服务远程调用2.2.1根据订单id查询订单功能需求:根据订单id查询订单的同时,把订单所属的用户信息一起返回现在是不能做到的, 可以看到现在user是个null【所以没有达到需求】现在的情况是下面的样子  但是需求想要的......
  • python嵌入HTTP代理代码示例
    以下是使用Python嵌入HTTP代理的示例代码:```pythonimporturllib.request#设置代理服务器地址和端口号proxy_handler=urllib.request.ProxyHandler({'http':'http://proxy.example.com:8080'})#创建opener对象opener=urllib.request.build_opener(proxy_handler)#使用opene......