Spring Cloud 定制合同转换器
2024-01-02 16:17 更新
ContractConverter
接口使您可以注册自己的合同结构转换器的实现。以下代码清单显示了ContractConverter
接口:
package org.springframework.cloud.contract.spec /** * Converter to be used to convert FROM {@link File} TO {@link Contract} * and from {@link Contract} to {@code T} * * @param <T > - type to which we want to convert the contract * * @author Marcin Grzejszczak * @since 1.1.0 */ interface ContractConverter<T> extends ContractStorer<T> { /** * Should this file be accepted by the converter. Can use the file extension * to check if the conversion is possible. * * @param file - file to be considered for conversion * @return - {@code true} if the given implementation can convert the file */ boolean isAccepted(File file) /** * Converts the given {@link File} to its {@link Contract} representation * * @param file - file to convert * @return - {@link Contract} representation of the file */ Collection<Contract> convertFrom(File file) /** * Converts the given {@link Contract} to a {@link T} representation * * @param contract - the parsed contract * @return - {@link T} the type to which we do the conversion */ T convertTo(Collection<Contract> contract) }
您的实现必须定义启动转换的条件。另外,您必须定义如何在两个方向上执行该转换。
创建实施后,必须创建一个
/META-INF/spring.factories
文件,在其中提供实施的完全限定名称。
以下示例显示了典型的spring.factories
文件:
org.springframework.cloud.contract.spec.ContractConverter=\ org.springframework.cloud.contract.verifier.converter.YamlContractConverter
以上内容是否对您有帮助:
更多建议: