0

I've got REST FeignClient defined in my application:

@FeignClient(name = "gateway", configuration = FeignAuthConfig.class)
public interface AccountsClient extends Accounts {

}

I share endpoint interface between server and client:

@RequestMapping(API_PATH)
public interface Accounts {


    @PostMapping(path = "/register",
            produces = APPLICATION_JSON_VALUE,
            consumes = APPLICATION_JSON_VALUE)
    ResponseEntity<?> registerAccount(@RequestBody ManagedPassUserVM managedUserDTO)
            throws EmailAlreadyInUseException, UsernameAlreadyInUseException, URISyntaxException;

}

Everythng works fine except that my FeignClient definition in my client application also got registered as independent REST endpoint.

At the moment I try to prevent this behavior using filter which returns 404 status code for FeignClinet client mappings in my client application. However this workeraund seems very inelegant.

Is there another way how to prevent feign clients registering as separate REST endpoints?

Ondrej Bozek
  • 10,987
  • 7
  • 54
  • 70

2 Answers2

1

It is a known limitation of Spring Cloud's feign support. By adding @RequestMapping to the interface, Spring MVC (not Spring Cloud) assumes you want as an endpoint. @RequestMapping on Feign interfaces is not currently supported.

spencergibb
  • 24,471
  • 6
  • 69
  • 75
  • It seems like a bug in SpringMvc. I also tried to omit my FeignClients from component scaning and it didn't work. – Ondrej Bozek Jan 18 '17 at 18:27
  • Pretty sure it's been a feature for a fairly long time. It is a shortcut, so you don't have to put both `@RequestMapping` and `@Component`. – spencergibb Jan 18 '17 at 18:29
  • It seems that they forgot to mention this behavior in documentation or javadoc. It also seems as quite unexpected behavior. – Ondrej Bozek Jan 19 '17 at 09:24
1

I've used workaround for this faulty Spring Framework behavior:

@Configuration
@ConditionalOnClass({Feign.class})
public class FeignMappingDefaultConfiguration {
    @Bean
    public WebMvcRegistrations feignWebRegistrations() {
        return new WebMvcRegistrationsAdapter() {
            @Override
            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
                return new FeignFilterRequestMappingHandlerMapping();
            }
        };
    }

    private static class FeignFilterRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
        @Override
        protected boolean isHandler(Class<?> beanType) {
            return super.isHandler(beanType) && (AnnotationUtils.findAnnotation(beanType, FeignClient.class) == null);
        }
    }
}

I found it in SpringCloud issue

Ondrej Bozek
  • 10,987
  • 7
  • 54
  • 70