【问题标题】:How to set representation for specific format on Json Serializer used by MicroProfile Rest Client?如何在 MicroProfile Rest Client 使用的 Json Serializer 上设置特定格式的表示?
【发布时间】:2021-05-25 20:51:35
【问题描述】:

如何在 MicroProfile Rest Client 使用的 Json Serializer 上设置特定格式的表示?

我有一项服务需要输入年月数据,格式为 ISO 8601“YYYY-MM”。服务器端工作正常,响应格式正确,但实现是 Resteasy。

我使用的是 MicroProfile Rest Client,实体属性定义为java.util.YearMonth

当我希望它是格式化字符串时,请求使用序列化的 JSON 年月属性表示为对象。

已尝试使用JsonFormatJsonbDateFormat@Schema 注释属性,结果相同。

还尝试添加适用于服务器端的 Jackson Provider (ContextResolver),但在客户端没有任何变化。

睾丸注释的代码sn-p:

import com.fasterxml.jackson.annotation.JsonFormat;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import javax.json.bind.annotation.JsonbDateFormat;

@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM")
@JsonbDateFormat(value = "yyyy-MM")
// Next one I'm guessing. Can't find docs for this.
@Schema(type = SchemaType.STRING, implementation = YearMonth.class, pattern = "yyyy-MM")
private YearMonth referencia;

环境:

  • 野蝇 21
  • Java SE 11
  • microprofile-rest-client-api:2.0
  • microprofile-openapi-api:2.0

【问题讨论】:

    标签: java json rest microprofile


    【解决方案1】:

    1. 首先,您需要启用 Jackson,因为 RESTEasy 默认为 JSONB。为此,您需要添加此系统属性

    • resteasy.preferJacksonOverJsonB = true

    使用-Dproperty=value 命令行语法或其他任何方式添加系统属性作为您的偏好。在我们的例子中,我们使用 WildFly 配置(例如standalone.xml):

    <system-properties>
        <property name="resteasy.preferJacksonOverJsonB" value="true"/>
    </system-properties>
    
    

    2. 然后您可以将 Jackson 配置提供程序 添加到您的 REST 客户端界面:

    import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
    import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
    
    @RegisterProvider(JacksonConfiguration.class)
    @RegisterRestClient
    public interface RestTestService {
        // methods...
    }
    

    现在,Java 8 日期时间 API 将被正确序列化,如果需要,您可以使用 @JsonFormat 添加自定义格式。


    另外:这里是 Jackson 配置提供程序的示例,也可以在客户端和服务器端使用:

    import javax.ws.rs.ext.ContextResolver;
    import javax.ws.rs.ext.Provider;
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
    
    @Provider
    public class JacksonConfiguration implements ContextResolver<ObjectMapper> {
        private final ObjectMapper mapper;
    
        public JacksonConfiguration() {
            mapper = new ObjectMapper();
            mapper.registerModule(new JavaTimeModule());
            mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        }
    
        @Override
        public ObjectMapper getContext(Class<?> type) {
            return mapper;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 2022-12-17
      • 2023-03-10
      相关资源
      最近更新 更多