use of io.helidon.media.common.MediaContext in project helidon by oracle.
the class Main method supports.
/**
* Combination of filtering {@link Handler} pattern with {@link io.helidon.webserver.Service Service} registration capabilities
* can be used by other frameworks for the integration. WebServer is shipped with several integrated libraries (supports)
* including <i>static content</i>, JSON and Jersey. See {@code POM.xml} for requested dependencies.
*/
public void supports() {
Routing routing = Routing.builder().register(StaticContentSupport.create("/static")).get("/hello/{what}", (req, res) -> res.send(JSON.createObjectBuilder().add("message", "Hello " + req.path().param("what")).build())).register("/api", JerseySupport.builder().register(HelloWorldResource.class).build()).build();
MediaContext mediaContext = MediaContext.builder().addWriter(JsonpSupport.writer()).build();
startServer(routing, mediaContext);
}
use of io.helidon.media.common.MediaContext in project helidon by oracle.
the class Main method mediaReader.
/**
* Use a custom {@link MessageBodyReader reader} to convert the request content into an object of a given type.
*/
public void mediaReader() {
Routing routing = Routing.builder().post("/create-record", Handler.create(Name.class, (req, res, name) -> {
System.out.println("Name: " + name);
res.status(Http.Status.CREATED_201).send(name.toString());
})).build();
// Create a media support that contains the defaults and our custom Name reader
MediaContext mediaContext = MediaContext.builder().addReader(NameReader.create()).build();
startServer(routing, mediaContext);
}
use of io.helidon.media.common.MediaContext in project helidon by oracle.
the class RequestContentTest method requestTestStub.
private static Request requestTestStub(Publisher<DataChunk> flux) {
BareRequest bareRequestMock = mock(BareRequest.class);
doReturn(URI.create("http://0.0.0.0:1234")).when(bareRequestMock).uri();
doReturn(flux).when(bareRequestMock).bodyPublisher();
WebServer webServer = mock(WebServer.class);
MediaContext mediaContext = MediaContext.create();
doReturn(mediaContext.readerContext()).when(webServer).readerContext();
doReturn(mediaContext.writerContext()).when(webServer).writerContext();
return new RequestTestStub(bareRequestMock, webServer);
}
use of io.helidon.media.common.MediaContext in project helidon by oracle.
the class MediaContextTest method testMediaSupportWithoutDefaults.
@Test
public void testMediaSupportWithoutDefaults() throws Exception {
WebClient client = WebClient.builder().baseUri("http://localhost:" + webServer.port() + "/greet").mediaContext(MediaContext.empty()).build();
client.get().request(String.class).thenAccept(it -> fail("No reader for String should be registered!")).exceptionally(ex -> {
assertThat(ex.getCause().getMessage(), is("No reader found for type: class java.lang.String"));
return null;
}).toCompletableFuture().get();
}
Aggregations