Skip to content

Commit 404f69f

Browse files
committed
Update README.md.
1 parent 232d8b4 commit 404f69f

File tree

11 files changed

+36
-57
lines changed

11 files changed

+36
-57
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ The `RequestMethod` annotation is used to associate a service method with an HTT
4848
Multiple methods may be associated with the same verb and path. `WebService` selects the best method to execute based on the provided argument values. For example, the following service class implements some simple addition operations:
4949

5050
```java
51-
@WebServlet(urlPatterns={"/math/*"})
51+
@WebServlet(urlPatterns={"/math/*"}, loadOnStartup = 1)
5252
public class MathService extends WebService {
5353
@RequestMethod("GET")
5454
@ResourcePath("sum")
@@ -109,7 +109,7 @@ Missing or `null` values are automatically converted to `0` or `false` for primi
109109
`URL` and `List<URL>` arguments represent file uploads. They may be used only with `POST` requests submitted using the multi-part form data encoding. For example:
110110

111111
```java
112-
@WebServlet(urlPatterns={"/upload/*"})
112+
@WebServlet(urlPatterns={"/upload/*"}, loadOnStartup = 1)
113113
@MultipartConfig
114114
public class FileUploadService extends WebService {
115115
@RequestMethod("POST")
@@ -263,11 +263,9 @@ Methods are grouped by resource path. Parameter and return types are encoded as
263263
Implementations can provide additional information about service types and operations using the `Description` annotation. For example:
264264

265265
```java
266-
@WebServlet(urlPatterns={"/math/*"})
266+
@WebServlet(urlPatterns={"/math/*"}, loadOnStartup = 1)
267267
@Description("Math example service.")
268268
public class MathService extends WebService {
269-
private static final long serialVersionUID = 0;
270-
271269
@RequestMethod("GET")
272270
@ResourcePath("sum")
273271
@Description("Calculates the sum of two numbers.")
@@ -301,7 +299,7 @@ public <T> T invoke() throws IOException { ... }
301299
public <T> T invoke(ResponseHandler<T> responseHandler) throws IOException { ... }
302300
```
303301

304-
The first version automatically deserializes a successful server response using `JSONDecoder`, which is discussed in more detail below. The second allows a caller to provide a custom response handler. `ResponseHandler` is a functional interface that is defined as follows:
302+
The first version automatically deserializes a successful server response using `JSONDecoder`, which is discussed in more detail later. The second allows a caller to provide a custom response handler. `ResponseHandler` is a functional interface that is defined as follows:
305303

306304
```java
307305
public interface ResponseHandler<T> {
@@ -314,22 +312,22 @@ If a service returns an error response, a `WebServiceException` will be thrown.
314312
The following code snippet demonstrates how `WebServiceProxy` might be used to access the operations of the simple math service discussed earlier. `listOf()`, `mapOf()`, and `entry()` are static utility methods provided by the `org.httprpc.util.Collections` class that can be used to declaratively create immutable collection instances:
315313

316314
```java
317-
WebServiceProxy webServiceProxy = new WebServiceProxy("GET", new URL("http://localhost:8080/httprpc-server/math/sum"));
315+
WebServiceProxy webServiceProxy = new WebServiceProxy("GET", new URL(serverURL, "math/sum"));
318316

319317
// GET /math/sum?a=2&b=4
320318
webServiceProxy.setArguments(mapOf(
321319
entry("a", 4),
322320
entry("b", 2)
323321
));
324322

325-
System.out.println(webServiceProxy.invoke()); // 6.0
323+
System.out.println((Number)webServiceProxy.invoke()); // 6.0
326324

327325
// GET /math/sum?values=1&values=2&values=3
328326
webServiceProxy.setArguments(mapOf(
329327
entry("values", listOf(1, 2, 3))
330328
));
331329

332-
System.out.println(webServiceProxy.invoke()); // 6.0
330+
System.out.println((Number)webServiceProxy.invoke()); // 6.0
333331
```
334332

335333
### Typed Access
@@ -364,7 +362,7 @@ public interface MathService {
364362
This code uses the `adapt()` method to create an instance of `MathService`, then invokes the `getSum()` method on the returned instance. The results are identical to the previous example:
365363

366364
```java
367-
MathService mathService = WebServiceProxy.adapt(new URL("http://localhost:8080/httprpc-test/math/"), MathService.class);
365+
MathService mathService = WebServiceProxy.adapt(new URL(serverURL, "math/"), MathService.class);
368366

369367
// GET /math/sum?a=2&b=4
370368
System.out.println(mathService.getSum(4, 2)); // 6.0
@@ -488,7 +486,7 @@ CSVDecoder csvDecoder = new CSVDecoder();
488486

489487
Iterable<Map<String, String>> months = csvDecoder.read(inputStream);
490488

491-
for (Map<String, Object> month : months) {
489+
for (Map<String, String> month : months) {
492490
System.out.println(String.format("%s has %d days", month.get("name"), month.get("days")));
493491
}
494492
```
@@ -503,7 +501,7 @@ public TemplateEncoder(URL url) { ... }
503501
public TemplateEncoder(URL url, Charset charset) { ... }
504502
```
505503

506-
The first argument specifies the URL of the template document (for example, as a resource on the application's classpath). The escape modifier corresponding to the document's extension (if any) will be applied by default. The optional second argument represents the character encoding used by the template document. If unspecified, UTF-8 is assumed.
504+
The first argument specifies the URL of the template document (typically as a resource on the application's classpath). The escape modifier corresponding to the document's extension (if any) will be applied by default. The optional second argument represents the character encoding used by the template document. If unspecified, UTF-8 is assumed.
507505

508506
Templates are applied using one of the following methods:
509507

@@ -529,7 +527,7 @@ Map<String, Object> map = mapOf(
529527

530528
TemplateEncoder templateEncoder = new TemplateEncoder(getClass().getResource("example.txt"));
531529

532-
templateEncoder.write(map, outputStream);
530+
templateEncoder.write(map, System.out);
533531
```
534532

535533
### Custom Modifiers
@@ -587,6 +585,7 @@ public class TreeNode {
587585
public void setChildren(List<TreeNode> children) {
588586
this.children = children;
589587
}
588+
}
590589
```
591590

592591
This class could be used to create a simple hierarchy as shown below:
@@ -617,7 +616,7 @@ JSONEncoder jsonEncoder = new JSONEncoder();
617616
jsonEncoder.write(new BeanAdapter(root), System.out);
618617
```
619618

620-
producing the following result:
619+
producing output similar to the following:
621620

622621
```json
623622
{
@@ -645,6 +644,8 @@ producing the following result:
645644
}
646645
```
647646

647+
Note that the order in which properties are traversed is not guaranteed to match the order in which the accessor methods are declared in a class. This is because the JDK itself makes no guarantees about the order in which methods are returned by `Class#getMethods()`, which is used internally by `BeanAdapter` to identify accessor methods.
648+
648649
### Excluding Values
649650
Any property tagged with the `Ignore` annotation will be excluded from the map. For example:
650651

@@ -686,11 +687,15 @@ public interface TreeNode {
686687
the `adapt()` method can be used to model the JSON data from the previous section as a collection of `TreeNode` values:
687688

688689
```java
690+
JSONDecoder jsonDecoder = new JSONDecoder();
691+
692+
Map<String, Object> map = jsonDecoder.read(inputStream);
693+
689694
TreeNode root = BeanAdapter.adapt(map, TreeNode.class);
690695

691-
root.getName(); // "Seasons"
692-
root.getChildren().get(0).getName(); // "Winter"
693-
root.getChildren().get(0).getChildren().get(0).getName(); // "January"
696+
System.out.println(root.getName()); // "Seasons"
697+
System.out.println(root.getChildren().get(0).getName()); // "Winter"
698+
System.out.println(root.getChildren().get(0).getChildren().get(0).getName()); // "January"
694699
```
695700

696701
### Custom Property Keys
@@ -715,7 +720,7 @@ public class Person {
715720

716721
Similarly, when a proxy instance of this interface is created by the `adapt()` method, `getFirstName()` will return the value associated with "first_name" in the underlying map, not "firstName":
717722

718-
```
723+
```java
719724
public interface Person {
720725
@Key("first_name")
721726
String getFirstName();
@@ -737,7 +742,7 @@ For example, the following code could be used to serialize the results of a data
737742
try (ResultSetAdapter resultSetAdapter = new ResultSetAdapter(statement.executeQuery())) {
738743
JSONEncoder jsonEncoder = new JSONEncoder();
739744

740-
jsonEncoder.write(resultSetAdapter, outputStream);
745+
jsonEncoder.write(resultSetAdapter, System.out);
741746
}
742747
```
743748

@@ -812,9 +817,9 @@ For example, the following markup might be used to represent the status of a ban
812817
This code could be used to display the account holder's name:
813818

814819
```java
815-
ElementAdapter rootAdapter = new ElementAdapter(document.getDocumentElement());
820+
ElementAdapter accountAdapter = new ElementAdapter(document.getDocumentElement());
816821

817-
Map<String, Object> holder = (Map<String, Object>)rootAdapter.get("holder");
822+
Map<String, Object> holder = (Map<String, Object>)accountAdapter.get("holder");
818823

819824
System.out.println(String.format("%s, %s", holder.get("lastName"), holder.get("firstName")));
820825
```
@@ -828,9 +833,9 @@ System.out.println(accountAdapter.get("@id")); // "101"
828833
A list of sub-elements can be obtained by appending an asterisk to the element name:
829834

830835
```java
831-
Map<String, Object> transactions = (Map<String, Object>)rootAdapter.get("transactions");
836+
Map<String, Object> transactions = (Map<String, Object>)accountAdapter.get("transactions");
832837

833-
List<Map<String, Object>> credits = transactions.get("credit*");
838+
List<Map<String, Object>> credits = (List<Map<String, Object>>)transactions.get("credit*");
834839

835840
for (Map<String, Object> credit : credits) {
836841
...
@@ -852,7 +857,7 @@ The `StreamAdapter` class provides access to the contents of a stream via the `I
852857

853858
JSONEncoder jsonEncoder = new JSONEncoder(true);
854859

855-
jsonEncoder.write(new StreamAdapter<>(values.stream().map(element -> element * 2)), writer);
860+
jsonEncoder.write(new StreamAdapter<>(values.stream().map(element -> element * 2)), System.out);
856861
```
857862

858863
`StreamAdapter` also implements `AutoCloseable` and ensures that the underlying stream is closed when the adapter is closed.
@@ -876,8 +881,6 @@ In addition to Java, HTTP-RPC web services can be implemented using the [Kotlin]
876881
@Description("System info service.")
877882
class SystemInfoService : WebService() {
878883
class SystemInfo(
879-
val hostName: String,
880-
val hostAddress: String,
881884
val availableProcessors: Int,
882885
val freeMemory: Long,
883886
val totalMemory: Long
@@ -886,12 +889,9 @@ class SystemInfoService : WebService() {
886889
@RequestMethod("GET")
887890
@Description("Returns system info.")
888891
fun getSystemInfo(): SystemInfo {
889-
val localHost = InetAddress.getLocalHost()
890892
val runtime = Runtime.getRuntime()
891893

892894
return SystemInfo(
893-
localHost.hostName,
894-
localHost.hostAddress,
895895
runtime.availableProcessors(),
896896
runtime.freeMemory(),
897897
runtime.totalMemory()
@@ -904,11 +904,9 @@ A response produced by the service might look like this:
904904

905905
```json
906906
{
907-
"hostName": "vm.local",
908-
"hostAddress": "192.168.1.12",
909-
"availableProcessors": 4,
910-
"freeMemory": 222234120,
911-
"totalMemory": 257949696
907+
"availableProcessors": 16,
908+
"freeMemory": 85845656,
909+
"totalMemory": 134217728
912910
}
913911
```
914912

httprpc-client/src/main/java/org/httprpc/util/Collections.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import java.util.AbstractMap;
1818
import java.util.Arrays;
19-
import java.util.HashMap;
19+
import java.util.LinkedHashMap;
2020
import java.util.List;
2121
import java.util.Map;
2222

@@ -61,7 +61,7 @@ public static <E> List<E> listOf(E... elements) {
6161
*/
6262
@SafeVarargs
6363
public static <K, V> Map<K, V> mapOf(Map.Entry<K, V>... entries) {
64-
HashMap<K, V> map = new HashMap<>();
64+
LinkedHashMap<K, V> map = new LinkedHashMap<>();
6565

6666
for (Map.Entry<K, V> entry : entries) {
6767
map.put(entry.getKey(), entry.getValue());

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
@WebServlet(urlPatterns={"/catalog/*"}, loadOnStartup=1)
2828
@Description("Simulates a product catalog.")
2929
public class CatalogService extends WebService {
30-
private static final long serialVersionUID = 0;
31-
3230
public static class Item {
3331
private String description;
3432
private double price;

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
@WebServlet(urlPatterns={"/echo/*"}, loadOnStartup=1)
2727
@Description("Echoes a string value by writing a custom response.")
2828
public class EchoService extends WebService {
29-
private static final long serialVersionUID = 0;
30-
3129
@RequestMethod("GET")
3230
@Description("Echoes a string value to the servlet response stream.")
3331
public void echo(

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
@MultipartConfig
3333
@Description("File upload example service.")
3434
public class FileUploadService extends WebService {
35-
private static final long serialVersionUID = 0;
36-
3735
@RequestMethod("POST")
3836
@Description("Uploads a single file.")
3937
public long upload(

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
@WebServlet(urlPatterns={"/math/*"}, loadOnStartup=1)
2626
@Description("Math example service.")
2727
public class MathService extends WebService {
28-
private static final long serialVersionUID = 0;
29-
3028
@RequestMethod("GET")
3129
@ResourcePath("sum")
3230
@Description("Calculates the sum of two numbers.")

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747
*/
4848
@WebServlet(urlPatterns={"/pets/*"}, loadOnStartup=1)
4949
public class PetService extends WebService {
50-
private static final long serialVersionUID = 0;
51-
5250
private DataSource dataSource = null;
5351

5452
public interface Pet {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
@WebServlet(urlPatterns={"/test/*"}, loadOnStartup=1)
4747
@MultipartConfig
4848
public class TestService extends WebService {
49-
private static final long serialVersionUID = 0;
50-
5149
@RequestMethod("GET")
5250
public Map<String, ?> testGet(String string, List<String> strings, int number, boolean flag,
5351
Date date, LocalDate localDate, LocalTime localTime, LocalDateTime localDateTime) {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.httprpc.WebService;
2020

2121
import javax.servlet.annotation.WebServlet;
22-
import java.util.Arrays;
2322

2423
import static org.httprpc.util.Collections.listOf;
2524

@@ -29,8 +28,6 @@
2928
@WebServlet(urlPatterns={"/tree/*"}, loadOnStartup=1)
3029
@Description("Tree service.")
3130
public class TreeService extends WebService {
32-
private static final long serialVersionUID = 0;
33-
3431
@RequestMethod("GET")
3532
@Description("Returns an example tree structure.")
3633
public TreeNode getTree() {

0 commit comments

Comments
 (0)