Springboot 使用Feign调用微信http/https链接

采用Feign调用外部http/https链接,比传统httpclient方法要简单,废话不多了,直接上代码

本文是调用微信   逆地址解析接口文档

https://apis.map.qq.com/ws/geocoder/v1?location=40.100834,117.089607&key=你自己的key&get_poi=1

其中key的值需要自己申请,申请地址 https://lbs.qq.com/

1、pom.xml

     注意:不能使用最新的springboot版本2.4.0 ,要使用2.2.11release版本(要与Eurka版本一样)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.11.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.2.3</version>
            <classifier>jdk15</classifier>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、application.yml

wx:
  url: https://apis.map.qq.com
server:
  port: 9090

3、DemoApplication,要添加 @EnableFeignClients注解

@SpringBootApplication
@EnableFeignClients
public class DemoApplication {

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

}

4、WxService

package com.example.demo.service;

import net.sf.json.JSONObject;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author zqing
 * @description: TODO
 * @date: 2020/12/7 10:28
 */

@FeignClient(url="${wx.url}",name="wxServer")
public interface WxService {
   /**
    * 完整地址是:https://apis.map.qq.com/ws/geocoder/v1?location=40.100834,117.089607&key=4UXBZ-3YFE4-XXXXXXXXXX-XXXXX-D5BFV&get_poi=1
    * @param location
    * @param key
    * @param get_poi
    * @return
    */

   @RequestMapping(value = "/ws/geocoder/v1")
   public JSONObject getInfo(@RequestParam String location,
                             @RequestParam String key,
                             @RequestParam String get_poi);


}

5、Wxcontroller

package com.example.demo.controller;

import com.example.demo.service.WxService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zqing
 * @description: TODO
 * @date: 2020/12/7 10:13
 */
@RestController
@CrossOrigin
public class WxController {
    
    @Autowired
    private WxService wxService;
    
    @RequestMapping("/getData")
      public JSONObject getInfo(@RequestParam String location,
                                @RequestParam String key,
                                @RequestParam String get_poi){
         JSONObject json = wxService.getInfo(location,key,get_poi);
         return (JSONObject) json.get("result");
      }
}

6、直接访问http://localhost:9090/getData?location=40.100834,117.089607&key=你自己的key&get_poi=1

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Feign调用微服务时,如果想取消响应数据映射,直接以String类型返回,可以通过在Feign的配置类上加上`Decoder`和`Encoder`的Bean实现。具体操作如下: 1. 创建一个配置类,例如`FeignConfig`,并在该类上加上注解`@Configuration`。 2. 在配置类中定义一个`Decoder`类型的Bean,用于取消响应数据映射,直接以String类型返回。具体代码如下: ```java @Bean public Decoder feignDecoder() { return new Decoder() { @Override public Object decode(Response response, Type type) throws IOException, FeignException { if (type.equals(String.class)) { return Util.toString(response.body().asReader()); } else { return new JacksonDecoder().decode(response, type); } } }; } ``` 3. 在配置类中定义一个`Encoder`类型的Bean,用于将请求数据转换为String类型。具体代码如下: ```java @Bean public Encoder feignEncoder() { return new Encoder() { @Override public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException { if (bodyType.equals(String.class)) { template.body(object.toString(), Charset.defaultCharset()); } else { new JacksonEncoder().encode(object, bodyType, template); } } }; } ``` 4. 最后,在Feign客户端接口上加上注解`@FeignClient`,并指定配置类`FeignConfig`,即可实现取消响应数据映射,直接以String类型返回。例如: ```java @FeignClient(name = "microservice", configuration = FeignConfig.class) public interface MicroserviceClient { @GetMapping("/api/data") String getData(); } ``` 以上就是使用Feign调用微服务,取消响应数据映射,直接以String类型返回的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值