Skip to content

Instantly share code, notes, and snippets.

@rmfish
Created January 10, 2019 16:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmfish/0ed59a9af6c05157be2a60c9acea2a10 to your computer and use it in GitHub Desktop.
Save rmfish/0ed59a9af6c05157be2a60c9acea2a10 to your computer and use it in GitHub Desktop.
Feign support pojo object in get query method

I use openfeign with org.springframework.cloud:spring-cloud-starter-openfeign:2+

@Autowired(required = false)
private ConversionService feignConversionService;

@Bean
public Contract feignContract() {
  return new SpringMvcPojoObjectQueryContract(this.parameterProcessors, feignConversionService);
}
import feign.MethodMetadata;
import java.lang.annotation.Annotation;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import org.springframework.core.convert.ConversionService;
import org.springframework.web.bind.annotation.RequestBody;

/**
* Created by jiangyu on 2019-01-10 22:52.
*/
public class SpringMvcPojoObjectQueryContract extends SpringMvcContract {

public SpringMvcPojoObjectQueryContract(List<AnnotatedParameterProcessor> annotatedParameterProcessors,
    ConversionService conversionService) {
  super(annotatedParameterProcessors, conversionService);
}

@Override
protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[] annotations, int paramIndex) {
  boolean httpAnnotation = super.processAnnotationsOnParameter(data, annotations, paramIndex);
  //在springMvc中如果是Get请求且参数中是对象 没有声明为@RequestBody 则默认为Param
  if (!httpAnnotation && StringUtils.equalsIgnoreCase(data.template().method(), "GET")) {
    for (Annotation parameterAnnotation : annotations) {
      if (!(parameterAnnotation instanceof RequestBody)) {
        return false;
      }
    }
    data.queryMapIndex(paramIndex);
    return true;
  }
  return httpAnnotation;
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment