Skip to content

Commit 773b014

Browse files
committed
Simplify path construction in web service proxy.
1 parent 216ff72 commit 773b014

File tree

4 files changed

+26
-74
lines changed

4 files changed

+26
-74
lines changed

httprpc-client/src/main/java/org/httprpc/WebServiceProxy.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -747,14 +747,17 @@ public static WebServiceProxy get(URL url) {
747747
* @param path
748748
* The path to the resource, relative to the base URL.
749749
*
750+
* @param args
751+
* Path format specifier arguments.
752+
*
750753
* @throws MalformedURLException
751754
* If a URL cannot be constructed from the base URL and path.
752755
*
753756
* @return
754757
* The new web service proxy.
755758
*/
756-
public static WebServiceProxy get(URL baseURL, String path) throws MalformedURLException {
757-
return get(new URL(baseURL, path));
759+
public static WebServiceProxy get(URL baseURL, String path, Object... args) throws MalformedURLException {
760+
return get(new URL(baseURL, String.format(path, args)));
758761
}
759762

760763
/**
@@ -779,14 +782,17 @@ public static WebServiceProxy post(URL url) {
779782
* @param path
780783
* The path to the resource, relative to the base URL.
781784
*
785+
* @param args
786+
* Path format specifier arguments.
787+
*
782788
* @throws MalformedURLException
783789
* If a URL cannot be constructed from the base URL and path.
784790
*
785791
* @return
786792
* The new web service proxy.
787793
*/
788-
public static WebServiceProxy post(URL baseURL, String path) throws MalformedURLException {
789-
return post(new URL(baseURL, path));
794+
public static WebServiceProxy post(URL baseURL, String path, Object... args) throws MalformedURLException {
795+
return post(new URL(baseURL, String.format(path, args)));
790796
}
791797

792798
/**
@@ -811,14 +817,17 @@ public static WebServiceProxy put(URL url) {
811817
* @param path
812818
* The path to the resource, relative to the base URL.
813819
*
820+
* @param args
821+
* Path format specifier arguments.
822+
*
814823
* @throws MalformedURLException
815824
* If a URL cannot be constructed from the base URL and path.
816825
*
817826
* @return
818827
* The new web service proxy.
819828
*/
820-
public static WebServiceProxy put(URL baseURL, String path) throws MalformedURLException {
821-
return put(new URL(baseURL, path));
829+
public static WebServiceProxy put(URL baseURL, String path, Object... args) throws MalformedURLException {
830+
return put(new URL(baseURL, String.format(path, args)));
822831
}
823832

824833
/**
@@ -843,14 +852,17 @@ public static WebServiceProxy delete(URL url) {
843852
* @param path
844853
* The path to the resource, relative to the base URL.
845854
*
855+
* @param args
856+
* Path format specifier arguments.
857+
*
846858
* @throws MalformedURLException
847859
* If a URL cannot be constructed from the base URL and path.
848860
*
849861
* @return
850862
* The new web service proxy.
851863
*/
852-
public static WebServiceProxy delete(URL baseURL, String path) throws MalformedURLException {
853-
return delete(new URL(baseURL, path));
864+
public static WebServiceProxy delete(URL baseURL, String path, Object... args) throws MalformedURLException {
865+
return delete(new URL(baseURL, String.format(path, args)));
854866
}
855867
}
856868

httprpc-test/src/main/java/org/httprpc/test/EchoService.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

httprpc-test/src/main/webapp/index.jsp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,12 @@
77

88
<body>
99

10-
<a href="${pageContext.request.contextPath}/math?api">Math API</a><br/>
11-
<br/>
12-
<a href="${pageContext.request.contextPath}/math/sum?a=2&b=4">Sum</a><br/>
13-
<a href="${pageContext.request.contextPath}/math/sum?values=1&values=2&values=3">Sum Values</a><br/>
14-
15-
<hr/>
16-
17-
<a href="${pageContext.request.contextPath}/echo?api">Echo API</a><br/>
18-
<br/>
19-
<a href="${pageContext.request.contextPath}/echo?value=héllo">Echo</a><br/>
20-
21-
<hr/>
22-
23-
<a href="${pageContext.request.contextPath}/catalog?api">Catalog API</a><br/>
10+
<a href="${pageContext.request.contextPath}/test?api">Test API</a><br/>
2411
<a href="${pageContext.request.contextPath}/upload?api">Upload API</a><br/>
12+
<a href="${pageContext.request.contextPath}/math?api">Math API</a><br/>
2513
<a href="${pageContext.request.contextPath}/tree?api">Tree API</a><br/>
26-
<a href="${pageContext.request.contextPath}/test?api">Test API</a><br/>
14+
<a href="${pageContext.request.contextPath}/catalog?api">Catalog API</a><br/>
15+
<a href="${pageContext.request.contextPath}/employees?api">Employees API</a><br/>
2716

2817
<hr/>
2918

@@ -41,9 +30,5 @@
4130
<br/>
4231
<a href="${pageContext.request.contextPath}/pets/average-age">Average Age</a><br/>
4332

44-
<hr/>
45-
46-
<a href="${pageContext.request.contextPath}/employees?api">Employees API</a><br/>
47-
4833
</body>
4934
</html>

httprpc-test/src/test/java/org/httprpc/WebServiceProxyTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,12 @@ public void testGet() throws IOException {
192192

193193
@Test
194194
public void testGetKeys() throws IOException {
195-
Map<String, ?> result = WebServiceProxy.get(baseURL, String.format("test/a/%d/b/%s/c/%d/d/%s",
195+
Map<String, ?> result = WebServiceProxy.get(baseURL, "test/a/%d/b/%s/c/%d/d/%s",
196196
123,
197197
URLEncoder.encode("héllo", "UTF-8"),
198198
456,
199199
URLEncoder.encode("göodbye", "UTF-8")
200-
)).invoke();
200+
).invoke();
201201

202202
assertEquals(mapOf(
203203
entry("list", listOf("123", "héllo", "456", "göodbye")),

0 commit comments

Comments
 (0)