Feign Client 编译失败,它把 BindingResult 作为第二个 Body 参数 [英] Feign Client fails to compile, it treats BindingResult as a second Body parameter
问题描述
我正在学习使用 Spring 的 Feign Client,所以我构建了两个简单的项目(serviceA 和 serviceB)来测试它.我有以下代码:
I'm learning to use Spring's Feign Client, so I've built two simple projects (serviceA and serviceB) to test it out. I have the following code:
serviceA 休息接口:
serviceA rest interface:
@RequestMapping("/users")
public interface UserRest {
@PostMapping
public ResponseEntity<User> createUser(@Valid @RequestBody User user, BindingResult br);
}
serviceA REST 接口实现:
serviceA rest interface implementation:
@RestController
public class UserController implements UserRest {
@Override
@PostMapping
public ResponseEntity<User> createUser(@Valid @RequestBody User user, BindingResult br) {
// validate user
// persist user
return ResponseEntity.ok(user);
}
}
serviceA feign client 声明:
serviceA feign client declaration:
@FeignClient(value = "serviceA", decode404 = true)
public interface UserFeignClient extends UserRest {}
现在,当我将 UserFeignClient
的实例自动装配到我的 serviceB 中时,当我的 REST 方法采用单个参数时,我可以很好地使用它.但是,当我尝试使用上述 BindingResult
验证参数时,出现以下异常:
Now when I autowire an instance of UserFeignClient
into my serviceB, I can use it just fine when my REST methods take a single parameter. However, when I then try to validate the parameter using BindingResult
like above, I get the following exception:
java.lang.IllegalStateException: Method has too many Body parameters: public abstract org.springframework.http.ResponseEntity com.mypackage.servicea.api.UserRest.createUser(org.apache.catalina.User,org.springframework.validation.BindingResult)
为什么 Feign 认为 BindingResult 是第二个 Body 实体?有什么办法可以解决这个问题吗?
Why does Feign think the BindingResult is a second Body entity? Is there any way to fix this?
推荐答案
一般认为
public ResponseEntity createUser(@Valid @RequestBody User user,绑定结果 br);
public ResponseEntity createUser(@Valid @RequestBody User user, BindingResult br);
作为
public ResponseEntity createUser(@Valid @RequestBody 用户用户,@RequestBody BindingResult br);
public ResponseEntity createUser(@Valid @RequestBody User user, @RequestBody BindingResult br);
OpenFeign 不接受两个 @RequestBody.
OpenFeign doesn’t accept two @RequestBody.
您可以在 feing/Contract.java 文件.
最好的办法是将 Feign 客户端与界面分离.
The best thing is to separate the Feign client from the interface.
这篇关于Feign Client 编译失败,它把 BindingResult 作为第二个 Body 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!