在使用GET请求时使用了rest风格方式,结果报错500
- Feign客户端代码
![](https://img2022.cnblogs.com/blog/892740/202206/892740-20220622021738702-1896734080.png)
- 请求路由
![](https://img2022.cnblogs.com/blog/892740/202206/892740-20220622021759594-451927393.png)
服务端路由
![](https://img2022.cnblogs.com/blog/892740/202206/892740-20220622021820392-1179736014.png)
使用postman 请求
![](https://img2022.cnblogs.com/blog/892740/202206/892740-20220622021841742-938657948.png)
报错信息
![](https://img2022.cnblogs.com/blog/892740/202206/892740-20220622021902694-1314437265.png)
刚开始还以为路由写错了,最后发现是需要把路径上的id参数取出来传入调用的方法中,修改后代码
package com.fengyun.medical.customersconsumerservice.openfeign;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import reactivefeign.spring.config.ReactiveFeignClient;
import reactor.core.publisher.Mono;
@ReactiveFeignClient(name="medical-product-service")
public interface HandBookFeignService {
@GetMapping(value = "/echo/{string}")
Mono<String> echo(@PathVariable String string);
@GetMapping(value = "/city")
Mono<String> getCity();
@GetMapping(value = "/ethnic")
Mono<String> getEthnic();
@GetMapping(value = "/relational")
Mono<String> getRelational();
@GetMapping(value = "/title")
Mono<String> getTitle();
@PostMapping(value = "/doctors")
Mono<String> register();
@GetMapping(value = "/doctors/{id}")
Mono<String> getDoctorById(@PathVariable Integer id);
}
package com.fengyun.medical.customersconsumerservice.routers;
import com.fengyun.medical.customersconsumerservice.openfeign.HandBookFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
//@Configuration(proxyBeanMethods = false)
public class HandBookRouter {
@Autowired
HandBookFeignService feignService;
@Value("${spring.application.name}")
private String appName;
@Bean
public RouterFunction<ServerResponse> router(){
return RouterFunctions.route()
.GET("/echo/app-name", accept(MediaType.APPLICATION_JSON),
request -> ServerResponse.ok().body(feignService.echo(appName), String.class))
.GET("/city", accept(MediaType.APPLICATION_JSON),
request -> ServerResponse.ok().body(feignService.getCity(), String.class))
.GET("/relational", accept(MediaType.APPLICATION_JSON),
request -> ServerResponse.ok().body(feignService.getRelational(), String.class))
.GET("/ethnic", accept(MediaType.APPLICATION_JSON),
request -> ServerResponse.ok().body(feignService.getEthnic(), String.class))
.GET("/title", accept(MediaType.APPLICATION_JSON),
request -> ServerResponse.ok().body(feignService.getTitle(), String.class))
.build();
}
@Bean
public RouterFunction<ServerResponse> doctorRouter() {
return RouterFunctions.route()
.GET("/doctor/{id}", accept(MediaType.APPLICATION_JSON),
request -> ServerResponse.ok()
.body(feignService.getDoctorById(Integer.valueOf(request.pathVariable("id"))), String.class))
// .path("/doctors", b1 -> b1
// .nest(accept(MediaType.APPLICATION_JSON), b2 -> b2
// .GET("/{id}", request -> ServerResponse.ok()
// .body(feignService.getDoctorById(), String.class))
// )
// .POST(request -> ServerResponse.ok().body(feignService.register(), String.class))
// )
.build();
}
}
找到consumer端路由:"/doctor/{id}"
调用Feign端提供的方法,这时需要把路径上的id取出来交给Feign
接下来Feign根据@ReactiveFeignClient(name="medical-product-service")
注解找到相应的服务,在注册中心找到服务所对应的url。
Feign再找到方法上的GetMapping
提供的映射地址@GetMapping(value = "/doctors/{id}")
Feign 通过方法内传递过来的id值与映射地址上的占位符id进行替换,错误就发生在这一步,因为Feign没有接收到id的值,所以无法与点位符上的id进行替换。