feign.Target.HardCodedTarget Java Examples

The following examples show how to use feign.Target.HardCodedTarget. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TargetTest.java    From feign with Apache License 2.0 6 votes vote down vote up
/**
 * Per <a href="https://github.com/Netflix/feign/issues/227">#227</a>, some may want to opt out of
 * percent encoding. Here's how.
 */
@Test
public void targetCanCreateCustomRequest() throws InterruptedException {
  server.enqueue(new MockResponse());

  String baseUrl = server.url("/default").toString();
  Target<TestQuery> custom = new HardCodedTarget<TestQuery>(TestQuery.class, baseUrl) {

    @Override
    public Request apply(RequestTemplate input) {
      Request urlEncoded = super.apply(input);
      return Request.create(
          urlEncoded.httpMethod(),
          urlEncoded.url().replace("%2F", "/"),
          urlEncoded.headers(),
          urlEncoded.body(), urlEncoded.charset());
    }
  };

  Feign.builder().target(custom).get("slash/foo", "slash/bar");

  assertThat(server.takeRequest()).hasPath("/default/slash/foo?query=slash/bar");
}
 
Example #2
Source File: FeignClientFactoryBean.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
protected <T> T loadBalance(Feign.Builder builder, FeignContext context,
		HardCodedTarget<T> target) {
	Client client = getOptional(context, Client.class);
	if (client != null) {
		builder.client(client);
		Targeter targeter = get(context, Targeter.class);
		return targeter.target(this, builder, context, target);
	}

	throw new IllegalStateException(
			"No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?");
}
 
Example #3
Source File: ExtTargeter.java    From onetwo with Apache License 2.0 5 votes vote down vote up
public FeignTargetContext(FeignClientFactoryBean feignClientfactory, Builder feign, FeignContext context,
		HardCodedTarget<T> hardeCodetarget, Targeter cloudTargeter) {
	super();
	this.feignClientfactory = feignClientfactory;
	this.feign = feign;
	this.context = context;
	this.hardeCodetarget = hardeCodetarget;
	this.cloudTargeter = cloudTargeter;
}
 
Example #4
Source File: ManuallyCreatedDelegateLoadBalancerFeignClientTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean
public AnnotatedFeignClient annotatedFeignClient(Client client, Decoder decoder,
		Encoder encoder, Contract contract) {
	return Feign.builder().client(client).encoder(encoder).decoder(decoder)
			.contract(contract).target(new HardCodedTarget<>(
					AnnotatedFeignClient.class, "foo", "http://foo"));
}
 
Example #5
Source File: DecoratorInvocationHandlerTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Throwable {
    target = new HardCodedTarget<TestService>(TestService.class,
        TestService.class.getSimpleName());
    testService = new TestServiceImpl();
    greetingMethod = testService.getClass().getDeclaredMethod("greeting");
    feignDecorator = new TestFeignDecorator();

    methodHandler = mock(MethodHandler.class);
    when(methodHandler.invoke(any())).thenReturn(testService.greeting());

    dispatch = new HashMap<>();
    dispatch.put(greetingMethod, methodHandler);

    testSubject = new DecoratorInvocationHandler(target, dispatch, feignDecorator);
}
 
Example #6
Source File: WhatShouldWeCacheBenchmarks.java    From feign with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup() {
  feignContract = new Contract.Default();
  cachedContact = new Contract() {
    private final List<MethodMetadata> cached =
        new Default().parseAndValidateMetadata(FeignTestInterface.class);

    public List<MethodMetadata> parseAndValidateMetadata(Class<?> declaring) {
      return cached;
    }
  };
  fakeClient = new Client() {
    public Response execute(Request request, Request.Options options) throws IOException {
      Map<String, Collection<String>> headers = new LinkedHashMap<String, Collection<String>>();
      return Response.builder()
          .body((byte[]) null)
          .status(200)
          .headers(headers)
          .reason("ok")
          .request(request)
          .build();
    }
  };
  cachedFakeFeign = Feign.builder().client(fakeClient).build();
  cachedFakeApi = cachedFakeFeign.newInstance(
      new HardCodedTarget<FeignTestInterface>(FeignTestInterface.class, "http://localhost"));
}
 
Example #7
Source File: WhatShouldWeCacheBenchmarks.java    From feign with Apache License 2.0 5 votes vote down vote up
/**
 * How fast re-parsing the annotated http api for each http request, without considering network?
 */
@Benchmark
public Response buildAndQuery_fake_cachedFeign() {
  return cachedFakeFeign.newInstance(
      new HardCodedTarget<FeignTestInterface>(FeignTestInterface.class, "http://localhost"))
      .query();
}
 
Example #8
Source File: HystrixBuilderTest.java    From feign with Apache License 2.0 5 votes vote down vote up
@Test
public void equalsHashCodeAndToStringWork() {
  final Target<TestInterface> t1 =
      new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8080");
  final Target<TestInterface> t2 =
      new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8888");
  final Target<OtherTestInterface> t3 =
      new HardCodedTarget<OtherTestInterface>(OtherTestInterface.class, "http://localhost:8080");
  final TestInterface i1 = target(t1);
  final TestInterface i2 = target(t1);
  final TestInterface i3 = target(t2);
  final OtherTestInterface i4 = target(t3);

  assertThat(i1)
      .isEqualTo(i2)
      .isNotEqualTo(i3)
      .isNotEqualTo(i4);

  assertThat(i1.hashCode())
      .isEqualTo(i2.hashCode())
      .isNotEqualTo(i3.hashCode())
      .isNotEqualTo(i4.hashCode());

  assertThat(i1.toString())
      .isEqualTo(i2.toString())
      .isNotEqualTo(i3.toString())
      .isNotEqualTo(i4.toString());

  assertThat(t1)
      .isNotEqualTo(i1);

  assertThat(t1.hashCode())
      .isEqualTo(i1.hashCode());

  assertThat(t1.toString())
      .isEqualTo(i1.toString());
}
 
Example #9
Source File: AsyncApacheHttp5ClientTest.java    From feign with Apache License 2.0 5 votes vote down vote up
@Test
public void equalsHashCodeAndToStringWork() {
  final Target<TestInterfaceAsync> t1 =
      new HardCodedTarget<TestInterfaceAsync>(TestInterfaceAsync.class,
          "http://localhost:8080");
  final Target<TestInterfaceAsync> t2 =
      new HardCodedTarget<TestInterfaceAsync>(TestInterfaceAsync.class,
          "http://localhost:8888");
  final Target<OtherTestInterfaceAsync> t3 =
      new HardCodedTarget<OtherTestInterfaceAsync>(OtherTestInterfaceAsync.class,
          "http://localhost:8080");
  final TestInterfaceAsync i1 = AsyncFeign.asyncBuilder().target(t1);
  final TestInterfaceAsync i2 = AsyncFeign.asyncBuilder().target(t1);
  final TestInterfaceAsync i3 = AsyncFeign.asyncBuilder().target(t2);
  final OtherTestInterfaceAsync i4 = AsyncFeign.asyncBuilder().target(t3);

  assertThat(i1).isEqualTo(i2).isNotEqualTo(i3).isNotEqualTo(i4);

  assertThat(i1.hashCode()).isEqualTo(i2.hashCode()).isNotEqualTo(i3.hashCode())
      .isNotEqualTo(i4.hashCode());

  assertThat(i1.toString()).isEqualTo(i2.toString()).isNotEqualTo(i3.toString())
      .isNotEqualTo(i4.toString());

  assertThat(t1).isNotEqualTo(i1);

  assertThat(t1.hashCode()).isEqualTo(i1.hashCode());

  assertThat(t1.toString()).isEqualTo(i1.toString());
}
 
Example #10
Source File: AsyncFeignTest.java    From feign with Apache License 2.0 5 votes vote down vote up
@Test
public void equalsHashCodeAndToStringWork() {
  Target<TestInterfaceAsync> t1 =
      new HardCodedTarget<TestInterfaceAsync>(TestInterfaceAsync.class,
          "http://localhost:8080");
  Target<TestInterfaceAsync> t2 =
      new HardCodedTarget<TestInterfaceAsync>(TestInterfaceAsync.class,
          "http://localhost:8888");
  Target<OtherTestInterfaceAsync> t3 =
      new HardCodedTarget<OtherTestInterfaceAsync>(OtherTestInterfaceAsync.class,
          "http://localhost:8080");
  TestInterfaceAsync i1 = AsyncFeign.asyncBuilder().target(t1);
  TestInterfaceAsync i2 = AsyncFeign.asyncBuilder().target(t1);
  TestInterfaceAsync i3 = AsyncFeign.asyncBuilder().target(t2);
  OtherTestInterfaceAsync i4 = AsyncFeign.asyncBuilder().target(t3);

  assertThat(i1).isEqualTo(i2).isNotEqualTo(i3).isNotEqualTo(i4);

  assertThat(i1.hashCode()).isEqualTo(i2.hashCode()).isNotEqualTo(i3.hashCode())
      .isNotEqualTo(i4.hashCode());

  assertThat(i1.toString()).isEqualTo(i2.toString()).isNotEqualTo(i3.toString())
      .isNotEqualTo(i4.toString());

  assertThat(t1).isNotEqualTo(i1);

  assertThat(t1.hashCode()).isEqualTo(i1.hashCode());

  assertThat(t1.toString()).isEqualTo(i1.toString());
}
 
Example #11
Source File: FeignUnderAsyncTest.java    From feign with Apache License 2.0 5 votes vote down vote up
@Test
public void equalsHashCodeAndToStringWork() {
  Target<TestInterface> t1 =
      new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8080");
  Target<TestInterface> t2 =
      new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8888");
  Target<OtherTestInterface> t3 =
      new HardCodedTarget<OtherTestInterface>(OtherTestInterface.class, "http://localhost:8080");
  TestInterface i1 = AsyncFeign.asyncBuilder().target(t1);
  TestInterface i2 = AsyncFeign.asyncBuilder().target(t1);
  TestInterface i3 = AsyncFeign.asyncBuilder().target(t2);
  OtherTestInterface i4 = AsyncFeign.asyncBuilder().target(t3);

  assertThat(i1)
      .isEqualTo(i2)
      .isNotEqualTo(i3)
      .isNotEqualTo(i4);

  assertThat(i1.hashCode())
      .isEqualTo(i2.hashCode())
      .isNotEqualTo(i3.hashCode())
      .isNotEqualTo(i4.hashCode());

  assertThat(i1.toString())
      .isEqualTo(i2.toString())
      .isNotEqualTo(i3.toString())
      .isNotEqualTo(i4.toString());

  assertThat(t1)
      .isNotEqualTo(i1);

  assertThat(t1.hashCode())
      .isEqualTo(i1.hashCode());

  assertThat(t1.toString())
      .isEqualTo(i1.toString());
}
 
Example #12
Source File: FeignTest.java    From feign with Apache License 2.0 5 votes vote down vote up
@Test
public void equalsHashCodeAndToStringWork() {
  Target<TestInterface> t1 =
      new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8080");
  Target<TestInterface> t2 =
      new HardCodedTarget<TestInterface>(TestInterface.class, "http://localhost:8888");
  Target<OtherTestInterface> t3 =
      new HardCodedTarget<OtherTestInterface>(OtherTestInterface.class, "http://localhost:8080");
  TestInterface i1 = Feign.builder().target(t1);
  TestInterface i2 = Feign.builder().target(t1);
  TestInterface i3 = Feign.builder().target(t2);
  OtherTestInterface i4 = Feign.builder().target(t3);

  assertThat(i1)
      .isEqualTo(i2)
      .isNotEqualTo(i3)
      .isNotEqualTo(i4);

  assertThat(i1.hashCode())
      .isEqualTo(i2.hashCode())
      .isNotEqualTo(i3.hashCode())
      .isNotEqualTo(i4.hashCode());

  assertThat(i1.toString())
      .isEqualTo(i2.toString())
      .isNotEqualTo(i3.toString())
      .isNotEqualTo(i4.toString());

  assertThat(t1)
      .isNotEqualTo(i1);

  assertThat(t1.hashCode())
      .isEqualTo(i1.hashCode());

  assertThat(t1.toString())
      .isEqualTo(i1.toString());
}
 
Example #13
Source File: AsyncFeign.java    From feign with Apache License 2.0 4 votes vote down vote up
public <T> T target(Class<T> apiType, String url) {
  return target(new HardCodedTarget<>(apiType, url));
}
 
Example #14
Source File: AsyncFeign.java    From feign with Apache License 2.0 4 votes vote down vote up
public <T> T target(Class<T> apiType, String url, C context) {
  return target(new HardCodedTarget<>(apiType, url), context);
}
 
Example #15
Source File: Feign.java    From feign with Apache License 2.0 4 votes vote down vote up
public <T> T target(Class<T> apiType, String url) {
  return target(new HardCodedTarget<T>(apiType, url));
}