1

I have a microservice and want to call a rest API thanks to FeignClient. I have used this:

@FeignClient( url = "${sample.url}")

But it doesn't work.

When I run my application, I get error:

Either 'name' or 'value' must be provided in @FeignClient

note that it is not a service which is registered in Eureka, It's an external rest API. I have added a sample name, then I got error Service id not legal hostname. My spring boot version is 2.7.3. It's ok for calling API from another microservice which is registered in Eureka.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

According to the documentation of FeignClient for value():

/**
 * The name of the service with optional protocol prefix. Synonym for {@link #name()
 * name}. A name must be specified for all clients, whether or not a url is provided.
 * Can be specified as property key, eg: ${propertyKey}.
 * @return the name of the service with optional protocol prefix
 */
@AliasFor("name")
String value() default "";

and name() :

/**
 * @return The service id with optional protocol prefix. Synonym for {@link #value()
 * value}.
 */
@AliasFor("value")
String name() default "";

So along with url you should provide either a name or value for your feign client

Also, if you get an error java.lang.IllegalStateException: Service id not legal hostname while using feign client, the cause may be is that feign client does not accept service name with under score. Just check this. The rules for naming are not documented, this point is just from my experience.

Also, you can read the reference where pointed:

Previously, using the url attribute, did not require the name attribute. Using name is now required.

kerbermeister
  • 2,985
  • 3
  • 11
  • 30
  • I thought it related to services that are registered in eureka. the Name will process and should follow the format that is acceptable for the URL. I have checked the URL class in Java and the getName method in feign. – Hamid Farid Feb 02 '23 at 19:30
  • so, you're still facing the problem? – kerbermeister Feb 02 '23 at 19:31
  • It is solved. @kerbermeister – Hamid Farid Feb 04 '23 at 10:00
  • so upvote, accept the answer if it has solved your problem, or provide your solution that helped you, so that community would know how to solve this kind of problem if someone faced the same – kerbermeister Feb 04 '23 at 10:08
  • plz, describe in your answer that name property should follow which pattern. "not accepting service name with underscore" is not enough. but it's a good hint. then I will accept your answer. Thanks. @kerbermeister – Hamid Farid Feb 04 '23 at 19:56
  • @HamidFarid actually it is not documented, it is just from my experince. I edited my answer – kerbermeister Feb 04 '23 at 22:38