Feign Client fails to compile, it treats BindingResult as a second Body parameter

走远了吗. 提交于 2021-01-28 19:33:30

问题


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 rest interface:

@RequestMapping("/users")
public interface UserRest {
    @PostMapping
    public ResponseEntity<User> createUser(@Valid @RequestBody User user, BindingResult br);
}

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 declaration:

@FeignClient(value = "serviceA", decode404 = true)
public interface UserFeignClient extends UserRest {}

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)

Why does Feign think the BindingResult is a second Body entity? Is there any way to fix this?


回答1:


Generally, it considers

public ResponseEntity createUser(@Valid @RequestBody User user, BindingResult br);

as

public ResponseEntity createUser(@Valid @RequestBody User user, @RequestBody BindingResult br);

OpenFeign doesn’t accept two @RequestBody.

You can debug this in feing/Contract.java file.

The best thing is to separate the Feign client from the interface.



来源:https://stackoverflow.com/questions/59110131/feign-client-fails-to-compile-it-treats-bindingresult-as-a-second-body-paramete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!