Skip to content

Commit

Permalink
Merge pull request micronaut-projects#2276 from micronaut-projects/is…
Browse files Browse the repository at this point in the history
…sue-1265-section6

Issue 1265 section6
  • Loading branch information
jameskleeh authored Oct 28, 2019
2 parents de8f82d + 16548bd commit db763c2
Show file tree
Hide file tree
Showing 111 changed files with 3,314 additions and 530 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.micronaut.function.client.aws;

//tag::import[]
import io.micronaut.context.ApplicationContext;
import io.micronaut.function.client.FunctionClient;
import javax.inject.Named;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
//end::rxImport[]
//end::import[]

import io.micronaut.runtime.server.EmbeddedServer;
//tag::rxImport[]
import io.reactivex.Single;

public class LocalFunctionInvokeJavaSpec {

//tag::invokeLocalFunction[]
@Test
public void testInvokingALocalFunction() {
Sum sum = new Sum();
sum.setA(5);
sum.setB(10);

EmbeddedServer server = ApplicationContext.run(EmbeddedServer.class);
MathClient mathClient = server.getApplicationContext().getBean(MathClient.class);

assertEquals(Long.valueOf(Integer.MAX_VALUE), mathClient.max());
assertEquals(2, mathClient.rnd(1.6f));
assertEquals(15, mathClient.sum(sum));

}
//end::invokeLocalFunction[]

//tag::invokeRxLocalFunction[]
@Test
public void testInvokingALocalFunctionRX() {
Sum sum = new Sum();
sum.setA(5);
sum.setB(10);

EmbeddedServer server = ApplicationContext.run(EmbeddedServer.class);
RxMathClient mathClient = server.getApplicationContext().getBean(RxMathClient.class);

assertEquals(Long.valueOf(Integer.MAX_VALUE), mathClient.max().blockingGet());
assertEquals(2, mathClient.rnd(1.6f).blockingGet().longValue());
assertEquals(15, mathClient.sum(sum).blockingGet().longValue());

}
//end::invokeRxLocalFunction[]

//tag::beginFunctionClient[]
@FunctionClient
interface MathClient {
//end::beginFunctionClient[]

//tag::functionMax[]
Long max(); //<1>
//end::functionMax[]

//tag::functionRnd[]
@Named("round")
int rnd(float value);
//end::functionRnd[]

long sum(Sum sum);
//tag::endFunctionClient[]
}
//end::endFunctionClient[]


//tag::rxFunctionClient[]
@FunctionClient
interface RxMathClient {
Single<Long> max();

@Named("round")
Single<Integer> rnd(float value);

Single<Long> sum(Sum sum);
}
//end::rxFunctionClient[]
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ The previous example presented a trivial example that uses the parameters of a m
include::{testsclient}/annotation/PetOperations.java[tags=save, indent=0]
----

////
NOTE: for some weird reason every time I try to replace this with a snippet it creates the following error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':publishGuide'.
> org.jruby.exceptions.RaiseException: (TypeError) cannot convert instance of class org.jruby.RubyObject to interface org.asciidoctor.ast.DocumentRuby
I found a closed issue on github that sounds similar https://github.com/asciidoctor/asciidoctorj/issues/267 but not really certain
////

The `save` method when called will perform an HTTP `POST` with the following JSON by default:

.Example Produced JSON
[source,json]
----
{"name":"Dino", age:10}
{"name":"Dino", "age":10}
----

You may however want to customize what is sent as the body, the parameters, URI variables and so on. The ann:http.client.annotation.Client[] annotation is very flexible in this regard and supports the same pkg:http.annotation[HTTP Annotations] as Micronaut's HTTP server.
Expand Down
11 changes: 2 additions & 9 deletions src/main/docs/guide/httpClient/clientFilter.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ As an example say you want to build a client to communicate with the https://bin

To resolve this burden you can define a filter. The following is an example `BintrayService`:

[source,groovy]
----
include::{testsdir}/client/ThirdPartyClientFilterSpec.groovy[tags=bintrayApiConstants, indent=0]
include::{testsdir}/client/ThirdPartyClientFilterSpec.groovy[tags=bintrayService, indent=0]
----
snippet::io.micronaut.docs.client.ThirdPartyClientFilterSpec[tags="bintrayApiConstants, bintrayService", indent=0]

<1> An api:http.client.RxHttpClient[] is injected for the Bintray API
<2> The organization is configurable via configuration

The Bintray API is secured. To authenticate you need to add an `Authorization` header for every request. You could modify `fetchRepositories` and `fetchPackages` methods to include the necessary HTTP Header for each request. Using a filter is much simpler though:

[source,groovy]
----
include::{testsdir}/client/ThirdPartyClientFilterSpec.groovy[tags=bintrayFilter, indent=0]
----
snippet::io.micronaut.docs.client.ThirdPartyClientFilterSpec[tags="bintrayFilter", indent=0]

<1> You can match only a subset of paths with a Client filter.
<2> The `username` and `token` are injected via configuration
Expand Down
5 changes: 1 addition & 4 deletions src/main/docs/guide/httpServer/datavalidation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ snippet::io.micronaut.docs.datavalidation.pogo.EmailController[tags="imports,cla

The validation of POJOs is shown in the following test:

[source,groovy]
----
include::{testsuitegroovy}/datavalidation/pogo/EmailControllerSpec.groovy[tag=pojovalidated,indent=0]
----
snippet::io.micronaut.docs.datavalidation.pogo.EmailControllerSpec[tags="pojovalidated",indent=0]

NOTE: Bean injection is supported in custom constraints with the hibernate validator configuration.
21 changes: 6 additions & 15 deletions src/main/docs/guide/ioc/lifecycle.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

If you wish for a particular method to be invoked when a bean is constructed then you can use the `javax.annotation.PostConstruct` annotation:

[source,groovy]
----
include::{testsdir}/lifecycle/V8Engine.groovy[tags=imports, indent=0]
include::{testsdir}/lifecycle/V8Engine.groovy[tags=class, indent=0]
----
snippet::io.micronaut.docs.lifecycle.V8Engine[tags="imports, class", indent=0]

<1> The `PostConstruct` annotation is imported
<2> A field is defined that requires initialization
Expand All @@ -17,22 +12,18 @@ include::{testsdir}/lifecycle/V8Engine.groovy[tags=class, indent=0]

If you wish for a particular method to be invoked when the context is closed then you can use the `javax.annotation.PreDestroy` annotation:

[source,groovy]
----
include::{testsdir}/lifecycle/PreDestroyBean.groovy[tags=class, indent=0]
----
snippet::io.micronaut.docs.lifecycle.PreDestroyBean[tags="class", indent=0]

<1> The `PreDestroy` annotation is imported
<2> A method is annotated with `@PreDestroy` and will be invoked when the context is closed.

For factory beans, the `preDestroy` value in the api:context.annotation.Bean[] annotation can be used to tell Micronaut which method to invoke.

[source,groovy]
----
include::{testsdir}/lifecycle/ConnectionFactory.groovy[tags=class, indent=0]
snippet::io.micronaut.docs.lifecycle.ConnectionFactory[tags="class", indent=0]

'''

include::{testsdir}/lifecycle/Connection.groovy[tags=class, indent=0]
----
snippet::io.micronaut.docs.lifecycle.Connection[tags="class", indent=0]

<1> The `preDestroy` value is set on the annotation
<2> The annotation value matches the method name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
An Endpoint can be created by annotating a class with the link:{api}/io/micronaut/management/endpoint/annotation/Endpoint.html[Endpoint] annotation, and supplying it with (at minimum) an endpoint id.
An Endpoint can be created by annotating a class with the link:{api}/io/micronaut/management/endpoint/Endpoint.html[Endpoint] annotation, and supplying it with (at minimum) an endpoint id.

[source,java]
.FooEndpoint.java
Expand Down Expand Up @@ -40,12 +40,4 @@ It is possible to supply additional (named) arguments to the annotation. Other p

The following example `Endpoint` class will create an endpoint accessible at `/date`:

[source,groovy]
.CurrentDateEndpoint.groovy
----
include::{testsuite}/server/endpoints/CurrentDateEndpoint.groovy[tags=endpointImport, indent=0]
include::{testsuite}/server/endpoints/CurrentDateEndpoint.groovy[tags=endpointClassBegin, indent=0]
//.. endpoint methods
include::{testsuite}/server/endpoints/CurrentDateEndpoint.groovy[tags=endpointClassEnd, indent=0]
----
snippet::io.micronaut.docs.server.endpoint.CurrentDateEndpoint[tags="endpointImport, endpointClassBegin, methodSummary, endpointClassEnd", indent=0, title="CurrentDateEndpoint"]
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,23 @@ Endpoints respond to `GET` ("read"), `POST` ("write") and `DELETE` ("delete") re
|===
|Annotation|Description

|api:management.endpoint.annotation.Read[]
|api:management.endpoint.Read[]
|Responds to `GET` requests

|api:management.endpoint.annotation.Write[]
|api:management.endpoint.Write[]
|Responds to `POST` requests

|api:management.endpoint.annotation.Delete[]
|api:management.endpoint.Delete[]
|Responds to `DELETE` requests

|===

== Read Methods

Annotating a method with the api:management.endpoint.annotation.Read[] annotation will cause it to respond to `GET` requests.
[source,groovy]
.CurrentDateEndpoint.groovy
----
include::{testsuite}/server/endpoints/CurrentDateEndpoint.groovy[tags=endpointImport, indent=0]
include::{testsuite}/server/endpoints/CurrentDateEndpoint.groovy[tags=readImport, indent=0]

include::{testsuite}/server/endpoints/CurrentDateEndpoint.groovy[tags=endpointClassBegin, indent=0]
include::{testsuite}/server/endpoints/CurrentDateEndpoint.groovy[tags=currentDate, indent=4]
snippet::io.micronaut.docs.server.endpoint.CurrentDateEndpoint[tags="endpointImport, readImport, endpointClassBegin, currentDate, simpleRead, endpointClassEnd", indent=0, title="CurrentDateEndpoint"]

include::{testsuite}/server/endpoints/CurrentDateEndpoint.groovy[tags=simpleRead, indent=4]
include::{testsuite}/server/endpoints/CurrentDateEndpoint.groovy[tags=endpointClassEnd, indent=0]
----

The above method responds to the following request:

Expand All @@ -41,7 +31,7 @@ $ curl -X GET localhost:55838/date
1526085903689
----

The api:management.endpoint.annotation.Read[] annotation accepts an optional `produces` argument, which sets the media type returned from the method (default is `application/json`):
The api:management.endpoint.Read[] annotation accepts an optional `produces` argument, which sets the media type returned from the method (default is `application/json`):

[source,groovy]
.CurrentDateEndpoint.groovy
Expand Down Expand Up @@ -70,7 +60,7 @@ the_date_is: Fri May 11 19:24:21 CDT

== Write Methods

Annotating a method with the api:management.endpoint.annotation.Write[] annotation will cause it to respond to `POST` requests.
Annotating a method with the api:management.endpoint.Write[] annotation will cause it to respond to `POST` requests.
[source,groovy]
.CurrentDateEndpoint.groovy
----
Expand All @@ -95,7 +85,7 @@ $ curl -X POST http://localhost:39357/date
Current date reset
----

The api:management.endpoint.annotation.Write[] annotation accepts an optional `consumes` argument, which sets the media type accepted by the method (default is `application/json`):
The api:management.endpoint.Write[] annotation accepts an optional `consumes` argument, which sets the media type accepted by the method (default is `application/json`):

[source,groovy]
.MessageEndpoint.groovy
Expand Down Expand Up @@ -124,7 +114,7 @@ Message updated

== Delete Methods

Annotating a method with the api:management.endpoint.annotation.Delete[] annotation will cause it to respond to `DELETE` requests.
Annotating a method with the api:management.endpoint.Delete[] annotation will cause it to respond to `DELETE` requests.

[source,groovy]
.MessageEndpoint.groovy
Expand Down
Loading

0 comments on commit db763c2

Please sign in to comment.