|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.docs.integration.resthttpinterface.customresolver; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.springframework.core.MethodParameter; |
| 22 | +import org.springframework.web.client.RestClient; |
| 23 | +import org.springframework.web.client.support.RestClientAdapter; |
| 24 | +import org.springframework.web.service.annotation.GetExchange; |
| 25 | +import org.springframework.web.service.invoker.HttpRequestValues; |
| 26 | +import org.springframework.web.service.invoker.HttpServiceArgumentResolver; |
| 27 | +import org.springframework.web.service.invoker.HttpServiceProxyFactory; |
| 28 | + |
| 29 | +public class CustomHttpServiceArgumentResolver { |
| 30 | + |
| 31 | + // tag::httpinterface[] |
| 32 | + interface RepositoryService { |
| 33 | + |
| 34 | + @GetExchange("/repos/search") |
| 35 | + List<Repository> searchRepository(Search search); |
| 36 | + |
| 37 | + } |
| 38 | + // end::httpinterface[] |
| 39 | + |
| 40 | + class Sample { |
| 41 | + |
| 42 | + void sample() { |
| 43 | + // tag::usage[] |
| 44 | + RestClient restClient = RestClient.builder().baseUrl("https://api.github.com/").build(); |
| 45 | + RestClientAdapter adapter = RestClientAdapter.create(restClient); |
| 46 | + HttpServiceProxyFactory factory = HttpServiceProxyFactory |
| 47 | + .builderFor(adapter) |
| 48 | + .customArgumentResolver(new SearchQueryArgumentResolver()) |
| 49 | + .build(); |
| 50 | + RepositoryService repositoryService = factory.createClient(RepositoryService.class); |
| 51 | + |
| 52 | + Search search = Search.create() |
| 53 | + .owner("spring-projects") |
| 54 | + .language("java") |
| 55 | + .query("rest") |
| 56 | + .build(); |
| 57 | + List<Repository> repositories = repositoryService.searchRepository(search); |
| 58 | + // end::usage[] |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + // tag::argumentresolver[] |
| 64 | + static class SearchQueryArgumentResolver implements HttpServiceArgumentResolver { |
| 65 | + @Override |
| 66 | + public boolean resolve(Object argument, MethodParameter parameter, HttpRequestValues.Builder requestValues) { |
| 67 | + if (parameter.getParameterType().equals(Search.class)) { |
| 68 | + Search search = (Search) argument; |
| 69 | + requestValues.addRequestParameter("owner", search.owner()); |
| 70 | + requestValues.addRequestParameter("language", search.language()); |
| 71 | + requestValues.addRequestParameter("query", search.query()); |
| 72 | + return true; |
| 73 | + } |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + // end::argumentresolver[] |
| 78 | + |
| 79 | + |
| 80 | + record Search (String query, String owner, String language) { |
| 81 | + |
| 82 | + static Builder create() { |
| 83 | + return new Builder(); |
| 84 | + } |
| 85 | + |
| 86 | + static class Builder { |
| 87 | + |
| 88 | + Builder query(String query) { return this;} |
| 89 | + |
| 90 | + Builder owner(String owner) { return this;} |
| 91 | + |
| 92 | + Builder language(String language) { return this;} |
| 93 | + |
| 94 | + Search build() { |
| 95 | + return new Search(null, null, null); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + record Repository(String name) { |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments