feign.Util类的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(289)

本文整理了Java中feign.Util类的一些代码示例,展示了Util类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Util类的具体详情如下:
包路径:feign.Util
类名称:Util

Util介绍

[英]Utilities, typically copied in from guava, so as to avoid dependency conflicts.
[中]实用程序,通常从guava中复制,以避免依赖冲突。

代码示例

代码示例来源:origin: spring-cloud-incubator/spring-cloud-alibaba

  1. SentinelInvocationHandler(Target<?> target, Map<Method, MethodHandler> dispatch) {
  2. this.target = checkNotNull(target, "target");
  3. this.dispatch = checkNotNull(dispatch, "dispatch");
  4. }

代码示例来源:origin: org.springframework.cloud/spring-cloud-openfeign-core

  1. private void parseConsumes(MethodMetadata md, Method method,
  2. RequestMapping annotation) {
  3. String[] serverConsumes = annotation.consumes();
  4. String clientProduces = serverConsumes.length == 0 ? null
  5. : emptyToNull(serverConsumes[0]);
  6. if (clientProduces != null) {
  7. md.template().header(CONTENT_TYPE, clientProduces);
  8. }
  9. }

代码示例来源:origin: spring-cloud/spring-cloud-openfeign

  1. @Override
  2. public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) {
  3. int parameterIndex = context.getParameterIndex();
  4. Class<?> parameterType = method.getParameterTypes()[parameterIndex];
  5. MethodMetadata data = context.getMethodMetadata();
  6. if (Map.class.isAssignableFrom(parameterType)) {
  7. checkState(data.headerMapIndex() == null, "Header map can only be present once.");
  8. data.headerMapIndex(parameterIndex);
  9. return true;
  10. }
  11. String name = ANNOTATION.cast(annotation).value();
  12. checkState(emptyToNull(name) != null,
  13. "RequestHeader.value() was empty on parameter %s", parameterIndex);
  14. context.setParameterName(name);
  15. Collection<String> header = context.setTemplateParameter(name, data.template().headers().get(name));
  16. data.template().header(name, header);
  17. return true;
  18. }
  19. }

代码示例来源:origin: com.netflix.feign/feign-jaxrs

  1. private void handleProducesAnnotation(MethodMetadata data, Produces produces, String name) {
  2. String[] serverProduces = produces.value();
  3. String clientAccepts = serverProduces.length == 0 ? null : emptyToNull(serverProduces[0]);
  4. checkState(clientAccepts != null, "Produces.value() was empty on %s", name);
  5. data.template().header(ACCEPT, (String) null); // remove any previous produces
  6. data.template().header(ACCEPT, clientAccepts);
  7. }

代码示例来源:origin: spring-cloud/spring-cloud-openfeign

  1. @Override
  2. public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) {
  3. String name = ANNOTATION.cast(annotation).value();
  4. checkState(emptyToNull(name) != null,
  5. "PathVariable annotation was empty on param %s.", context.getParameterIndex());
  6. context.setParameterName(name);
  7. MethodMetadata data = context.getMethodMetadata();
  8. String varName = '{' + name + '}';
  9. if (!data.template().url().contains(varName)
  10. && !searchMapValues(data.template().queries(), varName)
  11. && !searchMapValues(data.template().headers(), varName)) {
  12. data.formParams().add(name);
  13. }
  14. return true;
  15. }

代码示例来源:origin: kptfh/feign-reactive

  1. public PublisherClientMethodHandler(Target target,
  2. MethodMetadata methodMetadata,
  3. PublisherHttpClient publisherClient) {
  4. this.target = checkNotNull(target, "target must be not null");
  5. this.methodMetadata = checkNotNull(methodMetadata,
  6. "methodMetadata must be not null");
  7. this.publisherClient = checkNotNull(publisherClient, "client must be not null");
  8. this.pathExpander = buildExpandFunction(methodMetadata.template().url());
  9. this.headerExpanders = buildExpanders(methodMetadata.template().headers());
  10. this.queriesAll = new HashMap<>(methodMetadata.template().queries());
  11. if (methodMetadata.formParams() != null) {
  12. methodMetadata.formParams()
  13. .forEach(param -> add(queriesAll, param, "{" + param + "}"));
  14. }
  15. this.queryExpanders = buildExpanders(queriesAll);
  16. }

代码示例来源:origin: com.palantir.remoting3/jaxrs-clients

  1. @Override
  2. public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
  3. if (bodyType.equals(InputStream.class)) {
  4. try {
  5. template.body(Util.toByteArray((InputStream) object), StandardCharsets.UTF_8);
  6. } catch (IOException e) {
  7. throw new RuntimeException(e);
  8. }
  9. } else {
  10. delegate.encode(object, bodyType, template);
  11. }
  12. }
  13. }

代码示例来源:origin: io.github.reactivefeign/feign-reactive-core

  1. private ReactiveClientMethodHandler(Target target, MethodMetadata methodMetadata,
  2. ReactiveHttpClient reactiveClient) {
  3. this.target = checkNotNull(target, "target must be not null");
  4. this.methodMetadata = checkNotNull(methodMetadata,
  5. "methodMetadata must be not null");
  6. this.reactiveClient = checkNotNull(reactiveClient, "client must be not null");
  7. this.defaultUriBuilderFactory = new DefaultUriBuilderFactory(target.url());
  8. Stream<AbstractMap.SimpleImmutableEntry<String, String>> simpleImmutableEntryStream = methodMetadata
  9. .template().headers().entrySet().stream()
  10. .flatMap(e -> e.getValue().stream()
  11. .map(v -> new AbstractMap.SimpleImmutableEntry<>(e.getKey(), v)));
  12. this.headerExpanders = simpleImmutableEntryStream.collect(groupingBy(
  13. entry -> entry.getKey(),
  14. mapping(entry -> buildExpandHeaderFunction(entry.getValue()), toList())));
  15. }

代码示例来源:origin: kptfh/feign-reactive

  1. public static Type getBodyActualType(Type bodyType) {
  2. return ofNullable(bodyType).map(type -> {
  3. if (type instanceof ParameterizedType) {
  4. Class<?> bodyClass = (Class<?>) ((ParameterizedType) type).getRawType();
  5. if (Publisher.class.isAssignableFrom(bodyClass)) {
  6. return resolveLastTypeParameter(bodyType, bodyClass);
  7. }
  8. else {
  9. return type;
  10. }
  11. }
  12. else {
  13. return type;
  14. }
  15. }).orElse(null);
  16. }

代码示例来源:origin: com.marvinformatics.feign/feign-mock

  1. public MockClient ok(RequestKey requestKey, InputStream responseBody) throws IOException {
  2. return ok(requestKey, Util.toByteArray(responseBody));
  3. }

代码示例来源:origin: com.netflix.feign/feign-core

  1. @Override
  2. protected void processAnnotationOnClass(MethodMetadata data, Class<?> targetType) {
  3. if (targetType.isAnnotationPresent(Headers.class)) {
  4. String[] headersOnType = targetType.getAnnotation(Headers.class).value();
  5. checkState(headersOnType.length > 0, "Headers annotation was empty on type %s.",
  6. targetType.getName());
  7. Map<String, Collection<String>> headers = toMap(headersOnType);
  8. headers.putAll(data.template().headers());
  9. data.template().headers(null); // to clear
  10. data.template().headers(headers);
  11. }
  12. }

代码示例来源:origin: OpenFeign/feign-vertx

  1. @Override
  2. public List<MethodMetadata> parseAndValidatateMetadata(final Class<?> targetType) {
  3. checkNotNull(targetType, "Argument targetType must be not null");
  4. final List<MethodMetadata> metadatas = delegate.parseAndValidatateMetadata(targetType);
  5. for (final MethodMetadata metadata : metadatas) {
  6. final Type type = metadata.returnType();
  7. if (type instanceof ParameterizedType
  8. && ((ParameterizedType) type).getRawType().equals(Future.class)) {
  9. final Type actualType = resolveLastTypeParameter(type, Future.class);
  10. metadata.returnType(actualType);
  11. } else {
  12. throw new IllegalStateException(String.format(
  13. "Method %s of contract %s doesn't returns io.vertx.core.Future",
  14. metadata.configKey(), targetType.getSimpleName()));
  15. }
  16. }
  17. return metadatas;
  18. }
  19. }

代码示例来源:origin: com.netflix.feign/feign-core

  1. public HardCodedTarget(Class<T> type, String name, String url) {
  2. this.type = checkNotNull(type, "type");
  3. this.name = checkNotNull(emptyToNull(name), "name");
  4. this.url = checkNotNull(emptyToNull(url), "url");
  5. }

代码示例来源:origin: org.springframework.cloud/spring-cloud-openfeign-core

  1. private void checkOne(Method method, Object[] values, String fieldName) {
  2. checkState(values != null && values.length == 1,
  3. "Method %s can only contain 1 %s field. Found: %s", method.getName(),
  4. fieldName, values == null ? null : Arrays.asList(values));
  5. }

代码示例来源:origin: ppdai-incubator/raptor

  1. @Override
  2. protected void processAnnotationOnClass(MethodMetadata data, Class<?> clz) {
  3. if (clz.getInterfaces().length == 0) {
  4. RequestMapping classAnnotation = findMergedAnnotation(clz,
  5. RequestMapping.class);
  6. if (classAnnotation != null) {
  7. // Prepend path from class annotation if specified
  8. if (classAnnotation.value().length > 0) {
  9. String pathValue = emptyToNull(classAnnotation.value()[0]);
  10. pathValue = resolve(pathValue);
  11. if (!pathValue.startsWith("/")) {
  12. pathValue = "/" + pathValue;
  13. }
  14. data.template().insert(0, pathValue);
  15. }
  16. }
  17. }
  18. }

代码示例来源:origin: OpenFeign/feign-vertx

  1. @Override
  2. @SuppressWarnings("unchecked")
  3. public <T> T newInstance(final Target<T> target) {
  4. checkNotNull(target, "Argument target must be not null");
  5. final Map<String, MethodHandler> nameToHandler = targetToHandlersByName.apply(target);
  6. final Map<Method, MethodHandler> methodToHandler = new HashMap<>();
  7. final List<DefaultMethodHandler> defaultMethodHandlers = new ArrayList<>();
  8. for (final Method method : target.type().getMethods()) {
  9. if (isDefault(method)) {
  10. final DefaultMethodHandler handler = new DefaultMethodHandler(method);
  11. defaultMethodHandlers.add(handler);
  12. methodToHandler.put(method, handler);
  13. } else {
  14. methodToHandler.put(method, nameToHandler.get(Feign.configKey(target.type(), method)));
  15. }
  16. }
  17. final InvocationHandler handler = factory.create(target, methodToHandler);
  18. final T proxy = (T) Proxy.newProxyInstance(
  19. target.type().getClassLoader(),
  20. new Class<?>[] { target.type() },
  21. handler);
  22. for (final DefaultMethodHandler defaultMethodHandler : defaultMethodHandlers) {
  23. defaultMethodHandler.bindTo(proxy);
  24. }
  25. return proxy;
  26. }

代码示例来源:origin: com.netflix.feign/feign-core

  1. public RequestTemplate method(String method) {
  2. this.method = checkNotNull(method, "method");
  3. checkArgument(method.matches("^[A-Z]+$"), "Invalid HTTP Method: %s", method);
  4. return this;
  5. }

代码示例来源:origin: com.netflix.feign/feign-core

  1. private RequestTemplate doQuery(boolean encoded, String name, String... values) {
  2. checkNotNull(name, "name");
  3. String paramName = encoded ? name : encodeIfNotVariable(name);
  4. queries.remove(paramName);
  5. if (values != null && values.length > 0 && values[0] != null) {
  6. ArrayList<String> paramValues = new ArrayList<String>();
  7. for (String value : values) {
  8. paramValues.add(encoded ? value : encodeIfNotVariable(value));
  9. }
  10. this.queries.put(paramName, paramValues);
  11. }
  12. return this;
  13. }

代码示例来源:origin: com.netflix.feign/feign-core

  1. @Override
  2. public Object decode(Response response, Type type) throws IOException {
  3. if (response.status() == 404) return Util.emptyValueOf(type);
  4. if (response.body() == null) return null;
  5. if (byte[].class.equals(type)) {
  6. return Util.toByteArray(response.body().asInputStream());
  7. }
  8. return super.decode(response, type);
  9. }
  10. }

代码示例来源:origin: com.netflix.feign/feign-core

  1. @Override
  2. public List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType) {
  3. checkState(targetType.getTypeParameters().length == 0, "Parameterized types unsupported: %s",
  4. targetType.getSimpleName());
  5. checkState(targetType.getInterfaces().length <= 1, "Only single inheritance supported: %s",
  6. targetType.getSimpleName());
  7. if (targetType.getInterfaces().length == 1) {
  8. checkState(targetType.getInterfaces()[0].getInterfaces().length == 0,
  9. "Only single-level inheritance supported: %s",
  10. targetType.getSimpleName());
  11. }
  12. Map<String, MethodMetadata> result = new LinkedHashMap<String, MethodMetadata>();
  13. for (Method method : targetType.getMethods()) {
  14. if (method.getDeclaringClass() == Object.class ||
  15. (method.getModifiers() & Modifier.STATIC) != 0 ||
  16. Util.isDefault(method)) {
  17. continue;
  18. }
  19. MethodMetadata metadata = parseAndValidateMetadata(targetType, method);
  20. checkState(!result.containsKey(metadata.configKey()), "Overrides unsupported: %s",
  21. metadata.configKey());
  22. result.put(metadata.configKey(), metadata);
  23. }
  24. return new ArrayList<MethodMetadata>(result.values());
  25. }

相关文章