Spring Cloud 使用自定義存根下載器

2024-01-02 16:47 更新

您可以通過創(chuàng)建StubDownloaderBuilder接口的實(shí)現(xiàn)來自定義存根的下載方式,如以下示例所示:

package com.example;

class CustomStubDownloaderBuilder implements StubDownloaderBuilder {

	@Override
	public StubDownloader build(final StubRunnerOptions stubRunnerOptions) {
		return new StubDownloader() {
			@Override
			public Map.Entry<StubConfiguration, File> downloadAndUnpackStubJar(
					StubConfiguration config) {
				File unpackedStubs = retrieveStubs();
				return new AbstractMap.SimpleEntry<>(
						new StubConfiguration(config.getGroupId(), config.getArtifactId(), version,
								config.getClassifier()), unpackedStubs);
			}

			File retrieveStubs() {
			    // here goes your custom logic to provide a folder where all the stubs reside
			}
}

然后,可以將其注冊(cè)到spring.factories文件中,如以下示例所示:

# Example of a custom Stub Downloader Provider
org.springframework.cloud.contract.stubrunner.StubDownloaderBuilder=\
com.example.CustomStubDownloaderBuilder

現(xiàn)在,您可以選擇一個(gè)包含存根源的文件夾。

如果不提供任何實(shí)現(xiàn),則使用默認(rèn)設(shè)置(掃描類路徑)。如果提供stubsMode = StubRunnerProperties.StubsMode.LOCAL, stubsMode = StubRunnerProperties.StubsMode.REMOTE,則將使用Aether實(shí)現(xiàn)。如果提供多個(gè)??,則將使用列表中的第一個(gè)。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)