创建工程HelloServiceApi,目录结构如下:
其中entity.User和service.HelloService定义如下:
package com.didispace.helloserviceapi.service;
import com.didispace.helloserviceapi.entity.User;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/refactor")
public interface HelloService {
// 注意:以下三个方法,必须显示声明参数名称,否则Feign会抛出IIIegalStateException
@GetMapping("/hello4")
String hello(@RequestParam("name") String name);
@GetMapping("/hello5")
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@PostMapping("/hello6")
String hello(@RequestBody User user);
}
package com.didispace.helloserviceapi.entity;
public class User {
private String name;
private Integer id;
public User() {
}
public User(String name, Integer id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
打包:mvn install -Dmaven.test.skip=true
然后,引入本地maven仓库:mvn install:install-file -DgroupId=com.didispace -DartifactId=hello-service-api -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -Dfile=/Users/r/IdeaProjects/sc/hello-service-api/target/hello-service-api-0.0.1-SNAPSHOT.jar
然后,在HelloService引入如下:注意groupId, artifactId, version分别对应上面的命令中的值。
<dependency>
<groupId>com.didispace</groupId>
<artifactId>hello-service-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
但是,却报错:cannot resolve symbol "didispace",
错误原因是因为打包时配置错误,必须加入<configuration><skip>true</skip></configuration>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
重新打包,并且引入本地仓库,就正确如下:
接着,定义RefactorHelloController实现HelloServiceApi.HelloService
package com.sh.sctest.controller;
import com.didispace.helloserviceapi.entity.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RefactorHelloController implements com.didispace.helloserviceapi.service.HelloService {
@Override
public String hello(@RequestParam("name") String name) {
return "hello " + name;
}
@Override
public User hello(@RequestHeader("name") String name,@RequestHeader("age") Integer age) {
return new User(name, age);
}
@Override
public String hello(@RequestBody User user) {
return user.toString();
}
}
然后,改造FeginConsumer,引入新的HelloServiceApi:
<dependency>
<groupId>com.didispace</groupId>
<artifactId>hello-service-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
定义新的接口,并指明@FeignClient(value = "Hello-Service")
package com.sc.feignconsumer.service;
import com.didispace.helloserviceapi.service.HelloService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
@Component
@FeignClient(value = "Hello-Service")
public interface RefactorHelloService extends HelloService {
}
在FeignConsumerController中新增接口:
@Autowired
private RefactorHelloService refactorHelloService;
@GetMapping("/feign-consumer3")
public String helloConsumer3() {
StringBuilder sb = new StringBuilder();
sb.append(this.refactorHelloService.hello("daluo")).append("\n");
sb.append(this.refactorHelloService.hello("spring", 30)).append("\n");
sb.append(this.refactorHelloService.hello(new com.didispace.helloserviceapi.entity.User("daluo", 20))).append("\n");
return sb.toString();
}
最后效果如下:
所以,我们并没有在HelloService以及FeginConsumer定义entity.User,也没有在HelloService以及FeginConsumer显示声明RestFul路径/refactor,一切定义都交给HelloServiceApi !