The following shows entry point of Ktor application and its conceptually similar to Spring Boot application’s @SpringBootApplication annotated class and its main method.
Quarkus is a full-stack, Kubernetes-native Java framework made for Java virtual machines (JVMs) and native compilation, optimising Java specifically for containers and enabling it to become an effective platform for serverless, cloud, and Kubernetes environments. Quarkus is designed to work with popular Java standards, frameworks, and libraries like Eclipse MicroProfile and Spring, as well as Apache Kafka, RESTEasy (JAX-RS), Hibernate ORM (JPA), Spring, Infinispan, Camel, and many more.
Container-first
Whether an application is hosted on a public cloud or in an internally hosted Kubernetes cluster, characteristics like fast startup and low memory consumption are important to keeping overall host costs down.
Quarkus was built around a container-first philosophy, meaning it’s optimised for lower memory usage and faster startup times in the following ways:
First-class support for Graal/SubstrateVM
Build-time metadata processing
Reduction in reflection usage
Native image preboot
So Quarkus builds applications to consume 1/10th the memory when compared to traditional Java, and has a faster startup time (as much as 300 times faster), both of which greatly reduce the cost of cloud resources.
Quarkus is an effective solution for running Java in this new world of serverless architecture, microservices, containers, Kubernetes, function-as-a-service (FaaS), and cloud because it was created with all these things in mind.
Introduction
Whether an application is hosted on a public cloud or in an internally hosted Kubernetes cluster, characteristics like fast startup and low memory consumption are important to keeping overall host costs down.
Quarkus was built around a container-first philosophy, meaning it’s optimised for lower memory usage and faster startup times in the following ways:
First-class support for Graal/SubstrateVM
Build-time metadata processing
Reduction in reflection usage
Native image preboot
So Quarkus builds applications to consume 1/10th the memory when compared to traditional Java, and has a faster startup time (as much as 300 times faster), both of which greatly reduce the cost of cloud resources.
Quarkus is an effective solution for running Java in this new world of serverless architecture, microservices, containers, Kubernetes, function-as-a-service (FaaS), and cloud because it was created with all these things in mind.
@Path("/api/users")publicclassUserController {
@Inject
UserService userService;
@GET @Path("/{id}") public User getUser(@PathParam("id") Long id) {
return userService.getUserById(id);
}
@GET public List<User> getAllUsers() {
return userService.getUsers();
}
@POST public User createUser(final User user) {
UsercreatedUser= userService.createUser(user);
return createdUser;
}
@PUT @Path("/{id}") public User updateUser(@RestPath Long id, User user) {
return userService.updateUser(id, user);
}
@PATCH @Path("/{id}") public User patchUpdateUser(@RestPath Long id, User user) {
return userService.patchUpdateUser(id, user);
}
@DELETE @Path("/{id}") publicvoiddeleteUser(@RestPath Long id) {
userService.deleteUser(id);
}
}
Service Layer
@ApplicationScopedpublicclassUserServiceImplimplementsUserService {
@Inject private UsersRepository usersRepository;
@Override public User getUserById(Long id) {
Optional<User> maybeUser = usersRepository.findById(id);
return maybeUser.orElseThrow(() -> newRuntimeException("User not found"));
}
@Override public User createUser(User user) {
usersRepository.findByEmail(user.getEmail())
.ifPresent(p -> {
thrownewRuntimeException("User with email " + p.getEmail() + " already exists ");
});
return usersRepository.save(user);
}
@Override public User updateUser(Long id, User user) {
UseruserFound= usersRepository.findById(id).orElseThrow(() -> newRuntimeException("User not found"));
userFound.setFirstName(user.getFirstName());
userFound.setLastName(user.getLastName());
userFound.setEmail(user.getEmail());
userFound.setFirstLineOfAddress(user.getFirstLineOfAddress());
userFound.setSecondLineOfAddress(user.getSecondLineOfAddress());
userFound.setTown(user.getTown());
userFound.setPostCode(user.getPostCode());
return usersRepository.save(userFound);
}
@Override public User patchUpdateUser(Long id, User user) {
UseruserFound= usersRepository.findById(id).orElseThrow(() -> newRuntimeException("User not found"));
ModelMappermodelMapper=newModelMapper();
//copy only non null values
modelMapper.getConfiguration().setSkipNullEnabled(true).setMatchingStrategy(MatchingStrategies.STRICT);
modelMapper.map(user, userFound);
return usersRepository.save(userFound);
}
@Override publicvoiddeleteUser(Long id) {
usersRepository.deleteById(id);
}
@Override public List<User> getUsers() {
Iterable<User> userIterable = usersRepository.findAll();
return StreamSupport.stream(userIterable.spliterator(), false)
.collect(Collectors.toList());
}
}
Repository Layer
Entity
Entity/Persistence annotation name remain same as used in spring, but belongs to jakarta package