问题
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