JAVA-21440: Changes made for upgrade jooby module to 2.16.1 to make j… (#14064)

* JAVA-21440: Chnages made for upgrade jooby module to 2.16.1 to make java-17 compatable

* JAVA-21440: commenting redis code as this will only work in the local or infra should have a redis setup
This commit is contained in:
Bipin kumar 2023-05-21 15:15:40 +05:30 committed by GitHub
parent e6b0ef1d86
commit 3b73517b00
9 changed files with 218 additions and 144 deletions

View File

@ -1,2 +1,4 @@
#application.secret = 2o128940921eo298e21 #application.secret = 2o128940921eo298e21
#db = /url/to/the/datastore #db = /url/to/the/datastore
#redis = "redis://localhost:6379"

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.jooby</groupId> <groupId>com.baeldung.jooby</groupId>
<artifactId>jooby</artifactId> <artifactId>jooby</artifactId>
@ -14,26 +14,25 @@
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
</parent> </parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jooby</groupId>
<artifactId>modules</artifactId>
<version>${jooby.version}</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.jooby</groupId> <groupId>io.jooby</groupId>
<artifactId>jooby-netty</artifactId> <artifactId>jooby</artifactId>
<version>${jooby.version}</version> <version>${jooby.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.jooby</groupId> <groupId>io.jooby</groupId>
<artifactId>jooby-jedis</artifactId> <artifactId>jooby-test</artifactId>
<version>${jooby.version}</version>
</dependency>
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-jetty</artifactId>
<version>${jooby.version}</version>
</dependency>
<dependency>
<groupId>io.jooby</groupId>
<artifactId>jooby-redis</artifactId>
<version>${jooby.version}</version> <version>${jooby.version}</version>
</dependency> </dependency>
<dependency> <dependency>
@ -42,23 +41,43 @@
<scope>test</scope> <scope>test</scope>
<version>${rest-assured.version}</version> <version>${rest-assured.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${squareup.okhttp.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin.version}</version> <version>${maven-shade-plugin.version}</version>
</plugin> </plugin>
<plugin>
<groupId>io.jooby</groupId>
<artifactId>jooby-maven-plugin</artifactId>
<version>${jooby.version}</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
<properties> <properties>
<jooby.version>1.1.3</jooby.version> <jooby.version>2.16.1</jooby.version>
<rest-assured.version>3.1.1</rest-assured.version> <rest-assured.version>3.1.1</rest-assured.version>
<application.class>com.baeldung.jooby.App</application.class> <application.class>com.baeldung.jooby.App</application.class>
<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version> <maven-shade-plugin.version>3.2.4</maven-shade-plugin.version>
<maven-compiler.version>3.8.1</maven-compiler.version>
<squareup.okhttp.version>4.9.1</squareup.okhttp.version>
</properties> </properties>
</project> </project>

View File

@ -1,17 +1,18 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Insert title here</title> <title>Insert title here</title>
</head> </head>
<body> <body>
<form enctype="application/x-www-form-urlencoded" action="/submitForm" method="post"> <form enctype="application/x-www-form-urlencoded" action="/submitForm" method="post">
<input name="id" /> <label>Employed id:</label><br>
<input name="name" /> <input name="id"/><br>
<input name="email" /> <label>Name:</label><br>
<input name="phone" /> <input name="name"/><br>
<input name="address" /> <label>Email:</label><br>
<input type="submit" value="Submit"/> <input name="email"/><br><br>
</form> <input type="submit" value="Submit"/>
</form>
</body> </body>
</html> </html>

View File

@ -1,95 +1,135 @@
package com.baeldung.jooby; package com.baeldung.jooby;
import org.jooby.Jooby;
import org.jooby.Mutant;
import org.jooby.Session;
import org.jooby.jedis.Redis;
import org.jooby.jedis.RedisSessionStore;
import com.baeldung.jooby.bean.Employee; import com.baeldung.jooby.bean.Employee;
import io.jooby.Jooby;
import io.jooby.ServerOptions;
import io.jooby.Session;
import io.jooby.SessionStore;
import io.jooby.redis.RedisModule;
import io.jooby.redis.RedisSessionStore;
import io.lettuce.core.RedisClient;
public class App extends Jooby { public class App extends Jooby {
{
setServerOptions(new ServerOptions().setPort(8080)
.setSecurePort(8433));
}
{
port(8080);
securePort(8443);
}
{ {
get("/", () -> "Hello World!"); get("/", ctx -> "Hello World!");
} }
{ {
get("/user/{id}", req -> "Hello user : " + req.param("id").value()); get("/user/{id}", ctx -> "Hello user : " + ctx.path("id")
get("/user/:id", req -> "Hello user: " + req.param("id").value()); .value());
get("/uid:{id}", req -> "Hello User with id : uid" + req.param("id").value()); get("/user/:id", ctx -> "Hello user: " + ctx.path("id")
} .value());
get("/uid:{id}", ctx -> "Hello User with id : uid = " + ctx.path("id")
.value());
}
{ {
onStart(() -> { onStarting(() -> {
System.out.println("starting app"); System.out.println("starting app");
}); });
onStop(() -> { onStop(() -> {
System.out.println("stopping app"); System.out.println("stopping app");
}); });
onStarted(() -> { onStarted(() -> {
System.out.println("app started"); System.out.println("app started");
}); });
} }
{ {
get("/login", () -> "Hello from Baeldung"); get("/login", ctx -> "Hello from Baeldung");
} }
{ {
post("/save", req -> { post("/save", ctx -> {
Mutant token = req.param("token"); String userId = ctx.query("id")
return token.intValue(); .value();
}); return userId;
} });
}
{ {
{ {
assets("/employee", "form.html"); assets("/employee", "public/form.html");
} }
post("/submitForm", req -> { post("/submitForm", ctx -> {
Employee employee = req.params(Employee.class); Employee employee = ctx.path(Employee.class);
// TODO // TODO
return "empoyee data saved successfullly"; return "employee data saved successfully";
}); });
}
{ }
get("/filter", (req, resp, chain) -> {
// TODO
// resp.send(...);
chain.next(req, resp);
});
get("/filter", (req, resp) -> {
resp.send("filter response");
});
}
{
// cookieSession();
// use(new Redis());
//
// session(RedisSessionStore.class);
get("/session", req -> { {
Session session = req.session(); decorator(next -> ctx -> {
session.set("token", "value"); System.out.println("first");
return session.get("token").value(); // Moves execution to next handler: second
}); return next.apply(ctx);
} });
decorator(next -> ctx -> {
System.out.println("second");
// Moves execution to next handler: third
return next.apply(ctx);
});
public static void main(final String[] args) { get("/handler", ctx -> {
return "third";
});
}
run(App::new, args); {
} get("/sessionInMemory", ctx -> {
Session session = ctx.session();
session.put("token", "value");
return session.get("token")
.value();
});
}
{
String secret = "super secret token";
setSessionStore(SessionStore.signed(secret));
get("/signedSession", ctx -> {
Session session = ctx.session();
session.put("token", "value");
return session.get("token").value();
});
}
{
get("/sessionInMemoryRedis", ctx -> {
Session session = ctx.session();
session.put("token", "value");
return session.get("token")
.value();
});
}
/* This will work once redis is installed locally
{
install(new RedisModule("redis"));
setSessionStore(new RedisSessionStore(require(RedisClient.class)));
get("/redisSession", ctx -> {
Session session = ctx.session();
session.put("token", "value");
return session.get("token");
});
}*/
public static void main(final String[] args) {
runApp(args, App::new);
}
} }

View File

@ -1,22 +1,23 @@
package com.baeldung.jooby.mvc; package com.baeldung.jooby.mvc;
import org.jooby.Result; import java.util.HashMap;
import org.jooby.Results;
import org.jooby.mvc.GET; import io.jooby.ModelAndView;
import org.jooby.mvc.Path; import io.jooby.annotations.GET;
import io.jooby.annotations.Path;
@Path("/hello") @Path("/hello")
public class GetController { public class GetController {
@GET @GET
public String hello() { public String hello() {
return "Hello Baeldung"; return "Hello Baeldung";
} }
@GET @GET
@Path("/home") @Path("/home")
public Result home() { public ModelAndView home() {
return Results.html("welcome").put("model", new Object()); return new ModelAndView("welcome.html", new HashMap<>());
} }
} }

View File

@ -1,14 +1,13 @@
package com.baeldung.jooby.mvc; package com.baeldung.jooby.mvc;
import org.jooby.mvc.POST; import io.jooby.annotations.POST;
import org.jooby.mvc.Path; import io.jooby.annotations.Path;
@Path("/submit") @Path("/submit")
public class PostController { public class PostController {
@POST @POST
public String hello() { public String hello() {
return "Submit Baeldung"; return "Submit Baeldung";
} }
} }

View File

@ -1,21 +1,32 @@
package com.baeldung.jooby; package com.baeldung.jooby;
import static io.restassured.RestAssured.get; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.Matchers.equalTo;
import org.jooby.test.JoobyRule; import java.io.IOException;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import io.jooby.JoobyTest;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
@JoobyTest(value = App.class, port = 8080)
public class AppLiveTest { public class AppLiveTest {
@ClassRule static OkHttpClient client = new OkHttpClient();
public static JoobyRule app = new JoobyRule(new App());
@Test @Test
public void given_defaultUrl_expect_fixedString() { public void given_defaultUrl_expect_fixedString() {
get("/").then().assertThat().body(equalTo("Hello World!")).statusCode(200) Request request = new Request.Builder().url("http://localhost:8080")
.contentType("text/html;charset=UTF-8"); .build();
} try (Response response = client.newCall(request)
.execute()) {
assertEquals("Hello World!", response.body()
.string());
} catch (IOException e) {
e.printStackTrace();
}
}
} }

View File

@ -2,15 +2,16 @@ package com.baeldung.jooby;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import org.jooby.test.MockRouter;
import org.junit.Test; import org.junit.Test;
import io.jooby.MockRouter;
public class AppUnitTest { public class AppUnitTest {
@Test @Test
public void given_defaultUrl_with_mockrouter_expect_fixedString() throws Throwable { public void given_defaultUrl_with_mockrouter_expect_fixedString() {
String result = new MockRouter(new App()).get("/"); MockRouter router = new MockRouter(new App());
assertEquals("Hello World!", result); assertEquals("Hello World!", router.get("/")
} .value());
}
} }

View File

@ -24,7 +24,7 @@
<module>javax-servlets</module> <module>javax-servlets</module>
<module>javax-servlets-2</module> <module>javax-servlets-2</module>
<module>jee-7</module> <module>jee-7</module>
<!--<module>jooby</module>--> <module>jooby</module>
<module>linkrest</module> <module>linkrest</module>
<!-- <module>ninja</module>--> <!-- <module>ninja</module>-->
<!-- <module>play-modules</module> --> <!-- Not a maven project --> <!-- <module>play-modules</module> --> <!-- Not a maven project -->