Merge branch 'master' into BAEL-1711-move-code-snippets

This commit is contained in:
Rajat Garg
2018-05-13 21:40:31 +05:30
committed by GitHub
550 changed files with 10365 additions and 2075 deletions
+10 -1
View File
@@ -61,7 +61,7 @@
- [Creating and Configuring Jetty 9 Server in Java](http://www.baeldung.com/jetty-java-programmatic)
- [Introduction To OpenCSV](http://www.baeldung.com/opencsv)
- [Introduction to Akka Actors in Java](http://www.baeldung.com/akka-actors-java)
- [Asynchronous HTTP with async-http-client in Java](https://github.com/eugenp/tutorials/tree/master/libraries)
- [Asynchronous HTTP with async-http-client in Java](http://www.baeldung.com/async-http-client)
- [Introduction to Smooks](http://www.baeldung.com/smooks)
- [WebSockets with AsyncHttpClient](http://www.baeldung.com/async-http-client-websockets)
- [A Guide to Infinispan in Java](http://www.baeldung.com/infinispan)
@@ -70,7 +70,16 @@
- [Introduction to Akka Actors in Java](http://www.baeldung.com/akka-actors-java)
- [A Guide to Apache Commons Collections CollectionUtils](http://www.baeldung.com/apache-commons-collection-utils)
- [A Guide to Byte Buddy](http://www.baeldung.com/byte-buddy)
- [Introduction to jOOL](http://www.baeldung.com/jool)
- [Consumer Driven Contracts with Pact](http://www.baeldung.com/pact-junit-consumer-driven-contracts)
- [Apache Commons BeanUtils](http://www.baeldung.com/apache-commons-beanutils)
- [Apache Commons Collections BidiMap](http://www.baeldung.com/commons-collections-bidi-map)
- [Introduction to Atlassian Fugue](http://www.baeldung.com/java-fugue)
- [Publish and Receive Messages with Nats Java Client](http://www.baeldung.com/nats-java-client)
- [Java Concurrency Utility with JCTools](http://www.baeldung.com/java-concurrency-jc-tools)
- [Apache Commons Collections MapUtils](http://www.baeldung.com/apache-commons-map-utils)
- [Testing Netty with EmbeddedChannel](http://www.baeldung.com/testing-netty-embedded-channel)
The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own.
+6
View File
@@ -680,6 +680,12 @@
<version>${unirest.version}</version>
</dependency>
<!-- javalin -->
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>1.6.0</version>
</dependency>
<!-- Atlassian Fugue -->
<dependency>
<groupId>io.atlassian.fugue</groupId>
@@ -0,0 +1,16 @@
package com.baeldung.javalin;
import com.baeldung.javalin.User.UserController;
import io.javalin.Javalin;
public class JavalinApp {
public static void main(String[] args) {
Javalin app = Javalin.create()
.port(7000)
.start();
app.get("/hello", ctx -> ctx.html("Hello, Javalin!"));
app.get("/users", UserController.fetchAllUsernames);
app.get("/users/:id", UserController.fetchById);
}
}
@@ -0,0 +1,11 @@
package com.baeldung.javalin.User;
public class User {
public final int id;
public final String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
}
@@ -0,0 +1,24 @@
package com.baeldung.javalin.User;
import io.javalin.Handler;
import java.util.Objects;
public class UserController {
public static Handler fetchAllUsernames = ctx -> {
UserDao dao = UserDao.instance();
Iterable<String> allUsers = dao.getAllUsernames();
ctx.json(allUsers);
};
public static Handler fetchById = ctx -> {
int id = Integer.parseInt(Objects.requireNonNull(ctx.param("id")));
UserDao dao = UserDao.instance();
User user = dao.getUserById(id);
if (user == null) {
ctx.html("Not Found");
} else {
ctx.json(user);
}
};
}
@@ -0,0 +1,33 @@
package com.baeldung.javalin.User;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
class UserDao {
private final List<User> users = Arrays.asList(
new User(0, "Steve Rogers"),
new User(1, "Tony Stark"),
new User(2, "Carol Danvers")
);
private static UserDao userDao = null;
private UserDao() {
}
static UserDao instance() {
if (userDao == null) {
userDao = new UserDao();
}
return userDao;
}
Optional<User> getUserById(int id) { return users.stream().filter(u -> u.id == id).findFirst(); }
Iterable<String> getAllUsernames() {
return users.stream().map(user -> user.name).collect(Collectors.toList());
}
}