Rename directory, new concerete client example

This commit is contained in:
Michael Pratt 2018-07-06 13:49:23 +00:00 committed by José Carlos Valero Sánchez
parent a49358cfff
commit b4969dd2b8
22 changed files with 111 additions and 42 deletions

View File

@ -1,20 +0,0 @@
package com.baeldung.micronaut.server.controller;
import com.baeldung.micronaut.server.service.GreetingService;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import reactor.core.publisher.Mono;
import javax.inject.Inject;
@Controller("/async/greet")
public class AsyncGreetController {
@Inject
private GreetingService greetingService;
@Get("/{name}")
public Mono<String> greet(String name) {
return Mono.just(greetingService.getGreeting() + name);
}
}

View File

@ -1,17 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hello.world.server</groupId>
<artifactId>hello-world-server</artifactId>
<groupId>com.baeldung.micronaut</groupId>
<artifactId>hello-world</artifactId>
<version>0.1</version>
<properties>
<exec.mainClass>com.baeldung.micronaut.server.ServerApplication</exec.mainClass>
<exec.mainClass>com.baeldung.micronaut.helloworld.server.ServerApplication</exec.mainClass>
<micronaut.version>1.0.0.M2</micronaut.version>
<jdk.version>9</jdk.version>
</properties>
<repositories>
<repository>
<id>jcenter.bintray.com</id>
<url>https://jcenter.bintray.com</url>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencyManagement>

View File

@ -0,0 +1,28 @@
package com.baeldung.micronaut.helloworld.client;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.client.Client;
import io.micronaut.http.client.RxHttpClient;
import io.reactivex.Single;
import javax.inject.Singleton;
@Singleton
public class ConcreteGreetingClient
{
private RxHttpClient httpClient;
public ConcreteGreetingClient(@Client("/") RxHttpClient httpClient) {
this.httpClient = httpClient;
}
public String greet(String name) {
HttpRequest<String> req = HttpRequest.GET("/greet/" + name);
return httpClient.retrieve(req).blockingFirst();
}
public Single<String> greetAsync(String name) {
HttpRequest<String> req = HttpRequest.GET("/async/greet/" + name);
return httpClient.retrieve(req).first("An error as occurred");
}
}

View File

@ -1,10 +1,14 @@
package com.baeldung.micronaut.client;
package com.baeldung.micronaut.helloworld.client;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.client.Client;
import io.micronaut.http.client.RxHttpClient;
import javax.inject.Inject;
@Client("/greet")
public interface GreetingClient {
@Get("/{name}")
String greet(String name);
}

View File

@ -1,4 +1,4 @@
package com.baeldung.micronaut.server;
package com.baeldung.micronaut.helloworld.server;
import io.micronaut.runtime.Micronaut;

View File

@ -0,0 +1,20 @@
package com.baeldung.micronaut.helloworld.server.controller;
import com.baeldung.micronaut.helloworld.server.service.GreetingService;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.reactivex.Single;
import javax.inject.Inject;
@Controller("/async/greet")
public class AsyncGreetController {
@Inject
private GreetingService greetingService;
@Get("/{name}")
public Single<String> greet(String name) {
return Single.just(greetingService.getGreeting() + name);
}
}

View File

@ -1,6 +1,6 @@
package com.baeldung.micronaut.server.controller;
package com.baeldung.micronaut.helloworld.server.controller;
import com.baeldung.micronaut.server.service.GreetingService;
import com.baeldung.micronaut.helloworld.server.service.GreetingService;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;

View File

@ -1,4 +1,4 @@
package com.baeldung.micronaut.server.service;
package com.baeldung.micronaut.helloworld.server.service;
import io.micronaut.context.annotation.Primary;

View File

@ -1,4 +1,4 @@
package com.baeldung.micronaut.server.service;
package com.baeldung.micronaut.helloworld.server.service;
public interface GreetingService {

View File

@ -1,4 +1,4 @@
package com.baeldung.micronaut.server.service;
package com.baeldung.micronaut.helloworld.server.service;
import javax.inject.Singleton;

View File

@ -2,4 +2,4 @@ micronaut:
application:
name: hello-world-server
server:
port: 8080
port: 9080

View File

@ -0,0 +1,38 @@
package com.baeldung.micronaut.helloworld.client;
import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.server.EmbeddedServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class ConcreteGreetingClientTest
{
private EmbeddedServer server;
private ConcreteGreetingClient client;
@Before
public void setup()
{
server = ApplicationContext.run(EmbeddedServer.class);
client = server.getApplicationContext().getBean(ConcreteGreetingClient.class);
}
@After
public void cleanup()
{
server.stop();
}
@Test
public void testGreeting() {
assertEquals(client.greet("Mike"), "Hello Mike");
}
@Test
public void testGreetingAsync() {
assertEquals(client.greetAsync("Mike").blockingGet(), "Hello Mike");
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.micronaut.client;
package com.baeldung.micronaut.helloworld.client;
import io.micronaut.context.ApplicationContext;
import io.micronaut.runtime.server.EmbeddedServer;
@ -15,19 +15,18 @@ public class GreetingClientTest {
@Before
public void setup()
{
this.server = ApplicationContext.run(EmbeddedServer.class);
this.client = server.getApplicationContext().getBean(GreetingClient.class);
}
@Test
public void shouldReturnName() {
String response = client.greet("Mike");
assertEquals(response, "Hello Mike");
server = ApplicationContext.run(EmbeddedServer.class);
client = server.getApplicationContext().getBean(GreetingClient.class);
}
@After
public void cleanup()
{
this.server.stop();
server.stop();
}
@Test
public void testGreeting() {
assertEquals(client.greet("Mike"), "Hello Mike");
}
}