1

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?

DraegerMTN
  • 1,001
  • 2
  • 12
  • 21
  • Did you find a solution for this? – Kristijan Iliev Sep 01 '20 at 10:17
  • 1
    No, I'm pretty sure feign does not support methods with a BindingResult parameter, or at least it didn't back when I was trying it. In the end I had to remove BindingResult and create a custom validator (https://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/html/validation.html#validator) and then bind it to my controller (https://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/html/validation.html#validation-mvc-configuring) – DraegerMTN Sep 02 '20 at 17:48

1 Answers1

0

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.

Pavan Kumar Jorrigala
  • 3,085
  • 16
  • 27