Add exampleSite
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
## Dropwizard configuration
|
||||
|
||||
Use provided `RxJerseyBundle`
|
||||
```java
|
||||
@Override
|
||||
public void initialize(Bootstrap<RxJerseyConfiguration> bootstrap) {
|
||||
bootstrap.addBundle(new RxJerseyBundle<RxJerseyConfiguration>()
|
||||
.setClientConfigurationProvider(config -> config.client)
|
||||
.register(HeaderInterceptor.class)
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
Alternatively you can directly configure and register Jersey feature
|
||||
```java
|
||||
public void run(RxJerseyConfiguration configuration, Environment environment) throws Exception {
|
||||
JerseyEnvironment jersey = environment.jersey();
|
||||
|
||||
Client client = new JerseyClientBuilder(environment)
|
||||
.using(configuration.client)
|
||||
.using(new GrizzlyConnectorProvider())
|
||||
.buildRx("Client", RxObservableInvoker.class);
|
||||
|
||||
RxJerseyServerFeature rxJerseyServerFeature = new RxJerseyServerFeature()
|
||||
.register(HeaderInterceptor.class);
|
||||
|
||||
RxJerseyClientFeature rxJerseyClientFeature = new RxJerseyClientFeature()
|
||||
.register(client);
|
||||
|
||||
jersey.register(rxJerseyServerFeature);
|
||||
jersey.register(rxJerseyClientFeature);
|
||||
}
|
||||
```
|
||||
|
||||
#### [See example](https://github.com/alex-shpak/rx-jersey/tree/master/example) for more information
|
||||
@@ -0,0 +1,20 @@
|
||||
## Jersey configuration
|
||||
### Simple configuration
|
||||
This will assume default configuration with no interceptor and Grizzly client
|
||||
```java
|
||||
resourceConfig.register(RxJerseyServerFeature.class);
|
||||
resourceConfig.register(RxJerseyClientFeature.class);
|
||||
```
|
||||
|
||||
### Detailed configuration
|
||||
This configuration will add async request interceptor and override default client
|
||||
```java
|
||||
RxJerseyServerFeature rxJerseyServerFeature = new RxJerseyServerFeature()
|
||||
.register(AuthRequestInterceptor.class);
|
||||
|
||||
RxJerseyClientFeature rxJerseyClientFeature = new RxJerseyClientFeature()
|
||||
.register(client); // Should be non-blocking client implementation
|
||||
|
||||
resourceConfig.register(rxJerseyServerFeature);
|
||||
resourceConfig.register(rxJerseyClientFeature);
|
||||
```
|
||||
@@ -0,0 +1,60 @@
|
||||
## RxJersey Proxy Client
|
||||
|
||||
Proxy client provides convenient way to call resources without constructing request. Also it allows to reuse resource interfaces between microservices.
|
||||
|
||||
In order to enable RxJava in proxy client register Jersey feature
|
||||
```java
|
||||
RxJerseyClientFeature rxJerseyClientFeature = new RxJerseyClientFeature()
|
||||
.register(client); //should be non-blocking client implementation
|
||||
resourceConfig.register(rxJerseyClientFeature);
|
||||
```
|
||||
Default client with Grizzly connector will be used if not provided
|
||||
|
||||
|
||||
## Remote resource injection
|
||||
You can inject proxy client with `@Remote` annotation, in addition you can inject `WebTarget` or `RxWebTarget`
|
||||
```java
|
||||
@Path("/example/")
|
||||
public class GithubResource {
|
||||
|
||||
@Remote("https://api.github.com/")
|
||||
private GithubApi githubApi;
|
||||
|
||||
@Remote("https://api.github.com/")
|
||||
private WebTarget webTarget;
|
||||
|
||||
@GET
|
||||
@Path("github")
|
||||
public Single<GithubRepository> getRepository() {
|
||||
return githubApi.getRepository("alex-shpak", "rx-jersey").toSingle();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Manual proxy client creation
|
||||
You can use `WebResourceFactory` from `net.winterly.rxjersey.client` package in order to create proxy client
|
||||
|
||||
#### RxJava
|
||||
```java
|
||||
WebResourceFactory.newResource(
|
||||
ResourceInterface.class,
|
||||
rxWebTarget,
|
||||
new ObservableClientMethodInvoker()
|
||||
);
|
||||
```
|
||||
#### RxJava 2
|
||||
```java
|
||||
WebResourceFactory.newResource(
|
||||
ResourceInterface.class,
|
||||
webTarget,
|
||||
new FlowableClientMethodInvoker()
|
||||
);
|
||||
```
|
||||
|
||||
## Url resolving
|
||||
Below is example of URL merging based on `@Remote` annotation value
|
||||
|
||||
| Annotation Value | Jersey Context Path | Result URL |
|
||||
| ----------------------------- | --------------------------- | ---------------------------- |
|
||||
| @Remote("http://example.com") | http://baseurl.com/resource | http://example.com/ |
|
||||
| @Remote("/resource/") | http://baseurl.com/some | http://baseurl.com/resource/ |
|
||||
@@ -0,0 +1,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.
|
||||
Reference in New Issue
Block a user