如何自定义@FeignClient Expander来转换参数?

时间:2016-02-25 07:36:06

标签: spring spring-cloud netflix-feign

假设默认扩展器转换参数:

final class ToStringExpander implements Expander {

    @Override
    public String expand(Object value) {
      return value.toString();
    }
  }

我想自定义它以将用户转换为支持GET param,就像这样

@FeignClient("xx")
interface UserService{

   @RequestMapping(value="/users",method=GET)
   public List<User> findBy(@ModelAttribute User user);
}

userService.findBy(user);

我该怎么办?

3 个答案:

答案 0 :(得分:5)

首先,您必须编写像ToJsonExpander这样的扩展器:

public class ToJsonExpander implements Param.Expander {

  private static ObjectMapper objectMapper = new ObjectMapper();

  public String expand(Object value) {
    try {
      return objectMapper.writeValueAsString(value);
    } catch (JsonProcessingException e) {
      throw new ExpanderException(e);
    }
  }
}

其次,编写一个{JENArgumentParameterProcessor AnnotatedParameterProcessor来为你的处理器添加扩展器。

public class JsonArgumentParameterProcessor implements AnnotatedParameterProcessor {

  private static final Class<JsonArgument> ANNOTATION = JsonArgument.class;

  public Class<? extends Annotation> getAnnotationType() {
    return ANNOTATION;
  }

  public boolean processArgument(AnnotatedParameterContext context, Annotation annotation) {
    MethodMetadata data = context.getMethodMetadata();
    String name = ANNOTATION.cast(annotation).value();
    String method = data.template().method();

    Util.checkState(Util.emptyToNull(name) != null,
        "JsonArgument.value() was empty on parameter %s", context.getParameterIndex());

    context.setParameterName(name);

    if (method != null && (HttpMethod.POST.matches(method) || HttpMethod.PUT.matches(method) || HttpMethod.DELETE.matches(method))) {
      data.formParams().add(name);
    } else {
      `data.indexToExpanderClass().put(context.getParameterIndex(), ToJsonExpander.class);`
      Collection<String> query = context.setTemplateParameter(name, data.template().queries().get(name));
      data.template().query(name, query);
    }

    return true;
  }
}

第三,将其添加到Feign配置。

  @Bean
  public Contract feignContract(){
    List<AnnotatedParameterProcessor> processors = new ArrayList<>();
    processors.add(new JsonArgumentParameterProcessor());
    processors.add(new PathVariableParameterProcessor());
    processors.add(new RequestHeaderParameterProcessor());
    processors.add(new RequestParamParameterProcessor());
    return new SpringMvcContract(processors);
  }

现在,您可以使用@JsonArgument发送模型参数,如:

public void saveV10(@JsonArgument("session") Session session);

答案 1 :(得分:2)

我不知道@ModelAttribute的作用,但我正在寻找一种转换@RequestParam值的方法。

import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import org.springframework.cloud.netflix.feign.FeignFormatterRegistrar;
import org.springframework.format.FormatterRegistry;
import org.springframework.stereotype.Component;

import static com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat.E164;

@Component
public class PhoneNumberFeignFormatterRegistrar implements FeignFormatterRegistrar {
    private final PhoneNumberUtil phoneNumberUtil;

    public PhoneNumberFeignFormatterRegistrar(PhoneNumberUtil phoneNumberUtil) {
        this.phoneNumberUtil = phoneNumberUtil;
    }

    @Override
    public void registerFormatters(FormatterRegistry registry) {
        registry.addConverter(Phonenumber.PhoneNumber.class, String.class, source -> phoneNumberUtil.format(source, E164));
    }
}

现在像下面这样的作品

import com.google.i18n.phonenumbers.Phonenumber;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.hateoas.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient("data-service")
public interface DataClient {
    @RequestMapping(method = RequestMethod.GET, value = "/phoneNumbers/search/findByPhoneNumber")
    Resource<PhoneNumberRecord> getPhoneNumber(@RequestParam("phoneNumber") Phonenumber.PhoneNumber phoneNumber);
}

答案 2 :(得分:1)

open feign issuespring doc说:

  

OpenFeign @QueryMap批注支持将POJO用作GET参数映射。

     

Spring Cloud OpenFeign提供了等效的@SpringQueryMap批注,该批注用于从2.1.0版本开始将POJO或Map参数注释为查询参数映射。

您可以像这样使用它:

DataSet
    @GetMapping("user")
    String getUser(@SpringQueryMap User user);