aboutsummaryrefslogtreecommitdiff
path: root/exampleSite/content/docs/rx-jersey-server.md
blob: 105e581b36142b288a78e5d1010d12e474e8b5f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
## Jersey Server
Register `RxJerseyServerFeature` in `resourceConfig`
```java
resourceConfig.register(RxJerseyServerFeature.class);
```
Or with configuration
```java
RxJerseyServerFeature rxJerseyServerFeature = new RxJerseyServerFeature()
                .register(AuthInterceptor.class);

resourceConfig.register(rxJerseyServerFeature);
```

Update your resource adding rx return type:
```java
@Path("/")
public class HelloResource {

    @GET
    public Single<HelloEntity> getAsync() {
        return Single.just(new HelloEntity());
    }


    public static class HelloEntity {
        public String hello = "world";
    }
}
```

## Inteceptor
You can use RxJava enabled interceptors. Result of such interceptor will be ignored. Thrown or returned error would be redirected to jersey.

#### RxJava
```java
public class SimpleInterceptor implements ObservableRequestInterceptor<Void> {
    public Observable<Void> intercept(ContainerRequestContext requestContext) {
        return Observable.empty();
    }
}
```

#### RxJava 2
```java
public class SimpleInterceptor implements CompletableRequestInterceptor {
    public Completable intercept(ContainerRequestContext requestContext) {
        return Observable.complete();
    }
}
```


## Important notes
#### RxJava
 - It's recommended to use `rx.Single` as return type (Representing single response entity).
 - Multiple elements emitted in `Observable` will be treated as error.
 - Empty `Observable` or `null` value in `Observable` or `Single` will be treated as `204: No content`.
 - `Completable` will be executed and `204: No content` will be returned.

#### RxJava 2
 - It's recommended to use `io.reactivex.Maybe` which could be 0 or 1 item or an error.
 - Multiple elements emitted in `Observable` or `Flowable` will be treated as error.
 - Empty `Observable`/`Maybe` will be treated as `204: No content`.
 - `Completable` will be executed and `204: No content` will be returned.