|
1 | 1 | package com.baeldung; |
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import com.baeldung.filter.RequestValidatorFilter; |
| 7 | +import com.baeldung.model.Employee; |
| 8 | + |
| 9 | +import ratpack.guice.Guice; |
| 10 | +import ratpack.hikari.HikariModule; |
| 11 | +import ratpack.http.MutableHeaders; |
| 12 | +import ratpack.jackson.Jackson; |
3 | 13 | import ratpack.http.MutableHeaders; |
4 | 14 | import ratpack.server.RatpackServer; |
5 | 15 |
|
6 | 16 | public class Application { |
7 | 17 |
|
8 | | - public static void main(String... args) throws Exception { |
9 | | - RatpackServer.start(server -> server.handlers(chain -> chain.all(ctx -> { |
10 | | - MutableHeaders headers = ctx.getResponse().getHeaders(); |
11 | | - headers.set("Access-Control-Allow-Origin", "*"); |
12 | | - headers.set("Accept-Language", "en-us"); |
13 | | - headers.set("Accept-Charset", "UTF-8"); |
14 | | - ctx.next(); |
15 | | - }) |
16 | | - .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!")) |
17 | | - .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!")) |
18 | | - .post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!")) |
19 | | - )); |
| 18 | + public static void main(String[] args) throws Exception { |
| 19 | + |
| 20 | + List<Employee> employees = new ArrayList<Employee>(); |
| 21 | + employees.add(new Employee(1L, "Mr", "John Doe")); |
| 22 | + employees.add(new Employee(2L, "Mr", "White Snow")); |
| 23 | + |
| 24 | + |
| 25 | + RatpackServer.start( |
| 26 | + server -> server.registry(Guice.registry(bindings -> bindings.module(HikariModule.class, config -> { |
| 27 | + config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); |
| 28 | + config.addDataSourceProperty("URL", |
| 29 | + "jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'"); |
| 30 | + }))).handlers(chain -> chain |
| 31 | + .all( |
| 32 | + // ctx -> { |
| 33 | + // MutableHeaders headers = |
| 34 | + // ctx.getResponse().getHeaders(); |
| 35 | + // headers.set("Access-Control-Allow-Origin","*"); |
| 36 | + // headers.set("Accept-Language", "en-us"); |
| 37 | + // headers.set("Accept-Charset", "UTF-8"); |
| 38 | + // ctx.next(); |
| 39 | + // } |
| 40 | + new RequestValidatorFilter()) |
| 41 | + .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!")) |
| 42 | + .get("data/employees", ctx -> ctx.render(Jackson.json(employees))) |
| 43 | + .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!")) |
| 44 | + .post(":amount", ctx -> ctx |
| 45 | + .render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!")))); |
20 | 46 | } |
21 | 47 |
|
22 | 48 | } |
0 commit comments