springboot 4.0.0
- pom.xml 引入openfeign 2.0.2
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign</artifactId>
<version>2.0.2.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--外部http请求 FeignClient-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<repositories>
<!--外部http请求 FeignClient-->
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
- 启动类 添加注解@EnableFeignClients
@SpringBootApplication
@MapperScan("com.sichuang.repository.dao")//将项目中对应的mapper类的路径加进来就可以了
@ServletComponentScan
@EnableFeignClients
public class RepositoryApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(RepositoryApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// TODO Auto-generated method stub
// return super.configure(builder);
return builder.sources(RepositoryApplication.class);
}
}
- 外部接口类。调用方式同service
- @RequestParam 参数注解
- produces = MediaType.APPLICATION_JSON_UTF8_VALUE 返回json参数
package com.sichuang.repository.api;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
/**
* 对接接口
*
* @author xs
* @date 2018/10/8 9:08
*/
@FeignClient(url = "${url}", name = "Ewaytec2001API")
public interface Ewaytec2001API {
/**
*/
@GetMapping(value = "${url}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
String getEmployeeInfo(@RequestParam("id") int id, @RequestParam("sign") String sign);
}