我的冒牌客户定义如下:
@FeignClient(name = "${feign.name}",url = "${feign.url}",
configuration = {DateFormatConfiguration.class})
public interface MyFeignClient {
@GetMapping(value = "/test")
ResponseEntity<MyResponse> getResponse(@RequestParam(value = "date") Date date);
}其中:
class DateFormatConfiguration {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
@Bean
public FeignFormatterRegistrar dateFeignFormatterRegistrar() {
return formatterRegistry -> formatterRegistry.addFormatter(new Formatter<Date>() {
@Override
public Date parse(String text, Locale locale) throws ParseException {
return df.parse(text);
}
@Override
public String print(Date object, Locale locale) {
return df.format(object);
}
});
}
}然而,当我运行这个测试时:
@Test
public void test(){
Date date= new GregorianCalendar(2000, 12, 31).getTime();
myFeignClient.getResponse(date);
}请求以这种格式发送:
https:xxx/test?date=Wed%20Jan%2031%2000%3A00%3A00%20EST%202001
->获取
我想要的是:
-->获取https:xxx/test?date=2000-12-31
根据我的需要日期是格式化的。
我也尝试过这个解决方案,但两者都不起作用:
class DateFormatConfiguration {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
@Bean
public JacksonEncoder feignEncoder() {
return new JacksonEncoder(customObjectMapper());
}
@Bean
public JacksonDecoder feignDecoder() {
return new JacksonDecoder(customObjectMapper());
}
private ObjectMapper customObjectMapper(){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(df);
return objectMapper;
}
}有什么想法吗?
发布于 2020-12-03 14:48:34
您应该考虑尝试用以下内容替换所需的行:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date= LocalDate.ofInstant(new GregorianCalendar(2000, 12, 31).getTime().toInstant(), ZoneId.of(TimeZone.getDefault().getID()));
String dateFormatted = date.format(dtf);https://stackoverflow.com/questions/65118245
复制相似问题