feign
feign copied to clipboard
@QueryMap Could not fill in parameters
in my case:
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
compile 'org.springframework.boot:spring-boot-starter-web:1.5.22.RELEASE'
compile('io.github.openfeign:feign-spring4:11.2')
compile('io.github.openfeign:feign-gson:11.2')
compile('io.github.openfeign:feign-httpclient:11.2')
compile("org.apache.httpcomponents:httpcore-nio:4.4.10")
compile("org.apache.httpcomponents:httpasyncclient:4.1.4")
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.8")
MyBean
private Integer age;
private String name;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
private LocalDateTime birthDay;
myfeignConfig
/**
* @author bryce
* 2021/5/9 16:06
*/
@Configuration
public class FeignConfig {
@Bean
public MyFeign myFeign() {
Gson gson = new GsonBuilder().setPrettyPrinting()
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
.registerTypeAdapter(LocalDate.class, new LocalDateAdapter())
.create();
GsonEncoder encode = new GsonEncoder(gson);
GsonDecoder decode = new GsonDecoder(gson);
return Feign.builder()
.encoder(encode)
.decoder(decode)
.client(new ApacheHttpClient(getHttpClient()))
.contract(new SpringContract())
.target(MyFeign.class, "http://localhost:8090");
}
private CloseableHttpClient getHttpClient() {
CloseableHttpClient httpClient = null;
try {
// To trust self-signed certificates
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
} catch (Exception e) {
e.fillInStackTrace();
}
return httpClient;
}
}
public final class LocalDateAdapter implements JsonSerializer<LocalDate>, JsonDeserializer<LocalDate> {
@Override
public JsonElement serialize(LocalDate date, Type typeOfSrc, JsonSerializationContext context) {
//return new JsonPrimitive(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
return new JsonPrimitive(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
@Override
public LocalDate deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
String timestamp = element.getAsJsonPrimitive()
.getAsString();
//return LocalDate.parse(timestamp, DateTimeFormatter.ISO_LOCAL_DATE);
return LocalDate.parse(timestamp, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
}
public final class LocalDateTimeAdapter implements JsonSerializer<LocalDateTime>, JsonDeserializer<LocalDateTime> {
@Override
public JsonElement serialize(LocalDateTime date, Type typeOfSrc, JsonSerializationContext context) {
//return new JsonPrimitive(date.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
return new JsonPrimitive(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
@Override
public LocalDateTime deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
String timestamp = element.getAsJsonPrimitive()
.getAsString();
// return LocalDateTime.parse(timestamp, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
return LocalDateTime.parse(timestamp, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
}
feign
@GetMapping(value = "/mybean")
MyBean myBean(@QueryMap MyBean myBean);
the service provider
@GetMapping(value = "/mybean")
public MyBean myBean( MyBean myBean) {
System.out.println("get the bean:"+myBean);
if (myBean == null) {
System.out.println("null");
return null;
}
if (myBean.getBirthDay()==null){
myBean.setBirthDay(LocalDateTime.now());
}
myBean.setBirthDay(myBean.getBirthDay()
.minusDays(1));
return myBean;
}
After sending the request using feign:
get the bean:MyBean{age=null, name='null', birthDay=null}
the param always null
QueryMap is for use with query strings. In your example it looks like you want @RequestBody
instead.