Rename directory, new concerete client example
This commit is contained in:
parent
a49358cfff
commit
b4969dd2b8
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -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">
|
<?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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>hello.world.server</groupId>
|
<groupId>com.baeldung.micronaut</groupId>
|
||||||
<artifactId>hello-world-server</artifactId>
|
<artifactId>hello-world</artifactId>
|
||||||
<version>0.1</version>
|
<version>0.1</version>
|
||||||
<properties>
|
<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>
|
<micronaut.version>1.0.0.M2</micronaut.version>
|
||||||
<jdk.version>9</jdk.version>
|
<jdk.version>9</jdk.version>
|
||||||
</properties>
|
</properties>
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
<id>jcenter.bintray.com</id>
|
<id>jcenter.bintray.com</id>
|
||||||
<url>https://jcenter.bintray.com</url>
|
<url>http://jcenter.bintray.com</url>
|
||||||
</repository>
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -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.annotation.Get;
|
||||||
import io.micronaut.http.client.Client;
|
import io.micronaut.http.client.Client;
|
||||||
|
import io.micronaut.http.client.RxHttpClient;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
@Client("/greet")
|
@Client("/greet")
|
||||||
public interface GreetingClient {
|
public interface GreetingClient {
|
||||||
|
|
||||||
@Get("/{name}")
|
@Get("/{name}")
|
||||||
String greet(String name);
|
String greet(String name);
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.micronaut.server;
|
package com.baeldung.micronaut.helloworld.server;
|
||||||
|
|
||||||
import io.micronaut.runtime.Micronaut;
|
import io.micronaut.runtime.Micronaut;
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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.MediaType;
|
||||||
import io.micronaut.http.annotation.Body;
|
import io.micronaut.http.annotation.Body;
|
||||||
import io.micronaut.http.annotation.Controller;
|
import io.micronaut.http.annotation.Controller;
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.micronaut.server.service;
|
package com.baeldung.micronaut.helloworld.server.service;
|
||||||
|
|
||||||
import io.micronaut.context.annotation.Primary;
|
import io.micronaut.context.annotation.Primary;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.micronaut.server.service;
|
package com.baeldung.micronaut.helloworld.server.service;
|
||||||
|
|
||||||
public interface GreetingService {
|
public interface GreetingService {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.micronaut.server.service;
|
package com.baeldung.micronaut.helloworld.server.service;
|
||||||
|
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
@ -2,4 +2,4 @@ micronaut:
|
|||||||
application:
|
application:
|
||||||
name: hello-world-server
|
name: hello-world-server
|
||||||
server:
|
server:
|
||||||
port: 8080
|
port: 9080
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.micronaut.client;
|
package com.baeldung.micronaut.helloworld.client;
|
||||||
|
|
||||||
import io.micronaut.context.ApplicationContext;
|
import io.micronaut.context.ApplicationContext;
|
||||||
import io.micronaut.runtime.server.EmbeddedServer;
|
import io.micronaut.runtime.server.EmbeddedServer;
|
||||||
@ -15,19 +15,18 @@ public class GreetingClientTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setup()
|
public void setup()
|
||||||
{
|
{
|
||||||
this.server = ApplicationContext.run(EmbeddedServer.class);
|
server = ApplicationContext.run(EmbeddedServer.class);
|
||||||
this.client = server.getApplicationContext().getBean(GreetingClient.class);
|
client = server.getApplicationContext().getBean(GreetingClient.class);
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void shouldReturnName() {
|
|
||||||
String response = client.greet("Mike");
|
|
||||||
assertEquals(response, "Hello Mike");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void cleanup()
|
public void cleanup()
|
||||||
{
|
{
|
||||||
this.server.stop();
|
server.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGreeting() {
|
||||||
|
assertEquals(client.greet("Mike"), "Hello Mike");
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user