如何在 Feign Client 中以特定格式序列化请求正文的日期?

问题描述 投票:0回答:2

我尝试使用 feign 客户端向第三方 api 发送请求。当我检查请求正文时,它如下所示:

{
  "requestTime": "2023-06-07T12:18:00.916+00:00"
}

但 api 只接受日期格式

yyyy-MM-dd'T'mm:HH:ss.SSSZ
,因此有效的请求正文将类似于以下内容:

{
  "requestTime": "2023-06-17T14:53:47.402Z"
}

Feign Client中如何配置日期格式的序列化?

我的代码:

@FeignClient(value = "myfeign", url = "https://myfeign.com/")
public interface MyFeignClient {

    @PostMapping(value = "/myfeign/", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
    MyResponse sendRequest(MyRequest request);
}

并且

MyRequest
是由 openapi-generator 生成的。

public class MyRequest {
   @JsonProperty("requestTime")
   @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
   private Date requestTime;
   // getter...
}

编辑:我下面的方法不起作用,因为请求对象仍然使用

"2023-06-07T12:18:00.916+00:00"
格式。

public class MyFeignConfig {

    @Bean
    public Encoder feignEncoder() {
        HttpMessageConverter<Object> jacksonConverter = new MappingJackson2HttpMessageConverter(objectMapper());

        HttpMessageConverters httpMessageConverters = new HttpMessageConverters(jacksonConverter);
        ObjectFactory<HttpMessageConverters> objectFactory = () -> httpMessageConverters;


        return new SpringEncoder(objectFactory);
    }

    private ObjectMapper objectMapper() {
        final String DATE_FORMAT = "yyyy-MM-dd'T'mm:HH:ss.SSSZ";
        SimpleDateFormat dateFormat = new SimpleDateFormat((DATE_FORMAT));
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(dateFormat);
    }

在客户端添加配置

@FeignClient(value = "myfeign", url = "https://myfeign.com/",  configuration = "MyFeignConfig.class")
public interface MyFeignClient {

    @PostMapping(value = "/myfeign/", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
    MyResponse sendRequest(MyRequest request);
}
java spring-boot serialization encoding spring-cloud-feign
2个回答
0
投票

在 Feign Client 中配置日期格式的序列化,可以按照以下步骤操作:

创建一个配置类,我们称之为FeignConfig,并使用@Configuration注解。

@Configuration
public class FeignConfig {
    // Configuration code will go here
}

在FeignConfig类中为ObjectMapper定义一个bean。该 bean 将负责配置日期格式序列化。

@Bean
public ObjectMapper objectMapper() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setDateFormat(dateFormat);
    
    return objectMapper;
}

在这里,我们设置所需的日期格式并确保它使用 UTC 时区。

在FeignConfig类中为Encoder创建一个bean。该bean将使用定制的ObjectMapper进行序列化。

@Bean
public Encoder feignEncoder() {
    HttpMessageConverter<Object> jacksonConverter = new MappingJackson2HttpMessageConverter(objectMapper());

    return new SpringEncoder(() -> new HttpMessageConverters(jacksonConverter));
}
Apply the FeignConfig to your Feign client by specifying the configuration attribute in the @FeignClient annotation.
@FeignClient(value = "myfeign", url = "https://myfeign.com/", configuration = FeignConfig.class)
public interface MyFeignClient {
    // Feign client methods
}

这样,Feign 客户端将使用自定义的具有指定日期格式的 ObjectMapper 进行序列化,确保在将请求发送到第三方 API 之前,请求正文的格式正确。


0
投票

您需要在格式字符串中使用单引号

Z
,如下所示

final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
© www.soinside.com 2019 - 2024. All rights reserved.