More server stuff; create client and test
This commit is contained in:
parent
56c27f346b
commit
a49358cfff
@ -4,7 +4,7 @@
|
|||||||
<artifactId>hello-world-server</artifactId>
|
<artifactId>hello-world-server</artifactId>
|
||||||
<version>0.1</version>
|
<version>0.1</version>
|
||||||
<properties>
|
<properties>
|
||||||
<exec.mainClass>hello.world.server.Application</exec.mainClass>
|
<exec.mainClass>com.baeldung.micronaut.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>
|
||||||
@ -69,7 +69,12 @@
|
|||||||
<version>4.12</version>
|
<version>4.12</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
<dependency>
|
||||||
|
<groupId>io.projectreactor</groupId>
|
||||||
|
<artifactId>reactor-core</artifactId>
|
||||||
|
<version>3.1.6.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.micronaut.client;
|
||||||
|
|
||||||
|
import io.micronaut.http.annotation.Get;
|
||||||
|
import io.micronaut.http.client.Client;
|
||||||
|
|
||||||
|
@Client("/greet")
|
||||||
|
public interface GreetingClient {
|
||||||
|
@Get("/{name}")
|
||||||
|
String greet(String name);
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.micronaut.server;
|
||||||
|
|
||||||
|
import io.micronaut.runtime.Micronaut;
|
||||||
|
|
||||||
|
public class ServerApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Micronaut.run(ServerApplication.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.micronaut.server.controller;
|
||||||
|
|
||||||
|
import com.baeldung.micronaut.server.service.GreetingService;
|
||||||
|
import io.micronaut.http.MediaType;
|
||||||
|
import io.micronaut.http.annotation.Body;
|
||||||
|
import io.micronaut.http.annotation.Controller;
|
||||||
|
import io.micronaut.http.annotation.Get;
|
||||||
|
import io.micronaut.http.annotation.Post;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
@Controller("/greet")
|
||||||
|
public class GreetController {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
private GreetingService greetingService;
|
||||||
|
|
||||||
|
@Get("/{name}")
|
||||||
|
public String greet(String name) {
|
||||||
|
return greetingService.getGreeting() + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post(value = "/{name}", consumes = MediaType.TEXT_PLAIN)
|
||||||
|
public String setGreeting(@Body String name)
|
||||||
|
{
|
||||||
|
return greetingService.getGreeting() + name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.micronaut.server.service;
|
||||||
|
|
||||||
|
import io.micronaut.context.annotation.Primary;
|
||||||
|
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
@Primary
|
||||||
|
@Singleton
|
||||||
|
public class EnglishGreetingService implements GreetingService {
|
||||||
|
@Override
|
||||||
|
public String getGreeting() {
|
||||||
|
return "Hello ";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package com.baeldung.micronaut.server.service;
|
||||||
|
|
||||||
|
public interface GreetingService {
|
||||||
|
|
||||||
|
String getGreeting();
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.micronaut.server.service;
|
||||||
|
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
public class SpanishGreetingService implements GreetingService {
|
||||||
|
@Override
|
||||||
|
public String getGreeting() {
|
||||||
|
return "Hola ";
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
package hello.world.server;
|
|
||||||
|
|
||||||
import io.micronaut.runtime.Micronaut;
|
|
||||||
|
|
||||||
public class Application {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Micronaut.run(Application.class);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
package hello.world.server.controller;
|
|
||||||
|
|
||||||
import io.micronaut.http.annotation.Body;
|
|
||||||
import io.micronaut.http.annotation.Controller;
|
|
||||||
import io.micronaut.http.annotation.Get;
|
|
||||||
import io.micronaut.http.annotation.Post;
|
|
||||||
|
|
||||||
@Controller("/greet")
|
|
||||||
public class GreetController {
|
|
||||||
|
|
||||||
private String greeting = "Hello, ";
|
|
||||||
|
|
||||||
@Get("/{name}")
|
|
||||||
public String greet(String name) {
|
|
||||||
return greeting + name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Post
|
|
||||||
public void setGreeting(@Body String greeting)
|
|
||||||
{
|
|
||||||
this.greeting = greeting;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.micronaut.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 GreetingClientTest {
|
||||||
|
private EmbeddedServer server;
|
||||||
|
private GreetingClient client;
|
||||||
|
|
||||||
|
@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");
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void cleanup()
|
||||||
|
{
|
||||||
|
this.server.stop();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user