Posts

Showing posts with the label Spring Boot Examples

Spring Boot - RestTemplate

This guide walks you through the process of creating an application that produces RESTful web services and consumes a RESTful web service. To demonstrates the functionality we need a web service first. Name it ‘REST Producer’, because it provides data for the ‘REST Consumer’ REST Producer Controller Class @ RestController public class Controller { @ Autowired UserService service; @ GetMapping (value = "/getUser" ) public ResponseEntity < Map < Integer , User >> listAllUsers() { return new ResponseEntity <>(service.getUser(), HttpStatus.OK); } @ PostMapping (value = "/deleteUser" ) public ResponseEntity < Map < Integer , User >> deleteUser(@ RequestBody Key key) { return new ResponseEntity <>(service.deleteuser(key.getKey()), HttpStatus.OK); } Key-POJO Class public class Key { private Integer key; public Integer getKey() { return key; } public void se

Using Undertow as Embedded Server in Spring Boot

Image
For a Spring Boot Application, you can generate an application jar that contains Embedded Tomcat or Undertow or jetty. You can run a web application as a normal Java application! The advantage of this is you don't need the server pre-installed in the deployment environment. Below we are showing a simple Spring Boot application example using Undertow as Embedded Server. Undertow is a flexible performant web server, providing both blocking and non-blocking APIs based on NIO. It has a composition based architecture that allows you to build a web server by combining small single purpose handlers. The gives you the flexibility to choose between a full Java EE servlet container, or a low-level non-blocking handler, to anything in between. The main advantages of undertow are HTTP/2 Support, HTTP Upgrade Support, Web Socket Support, Servlet 4.0, Embeddable, Flexible. 1. Maven/Dependency Management Remove the existing tomcat dependency on spring-boot-starter-web and add undertow depende

Using Jetty as Embedded Server in Spring Boot

Image
For a Spring Boot Application, you can generate an application jar that contains Embedded Tomcat or Undertow or jetty . You can run a web application as a normal Java application! The advantage of this is you don't need the server pre-installed in the deployment environment.   Below we are showing a simple Spring Boot application example using Jetty as Embedded Server. 1. Maven/Dependency Management [pom.xml] Remove the existing tomcat dependency on spring-boot-starter-web and added jetty dependency <?xml version = "1.0" encoding = "UTF-8" ?> <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0  https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0.0 </modelVersion> <parent> <groupId> org.springframework.boot </

Hystrix Circuit breaker pattern with Spring Boot

Image
In this article, we will go through how to implement the circuit breaker design pattern using Hystrix and Spring Boot What is Hystrix? Hystrix is the fault tolerance library. Using this library we can implement a circuit breaker design pattern. Hystrix is watching methods for failing calls to related services. If there is such a failure, it will open the circuit and forward the call to a fallback method. The library will tolerate failures up to a threshold. Beyond that, it leaves the circuit open. Which means, it will forward all subsequent calls to the fallback method, to prevent future failures. This creates a time buffer for the related service to recover from its failing state. For a quick start following technologies stack being used: Spring Boot 2.1.1.RELEASE Spring 5.1.3.RELEASE Hystrix  Maven 3 JDK 1.8 Eclipse Oxygen Project Structure To demonstrate the circuit breaker design pattern we need a web service first. Name it ‘REST Producer’, because it provides data

Vaadin + Spring Boot - example

Image
Today, we will go through how to develop web applications using the Vaadin framework. Vaadin is the only framework that allows you to write UI 100% in Java without getting bogged down in JS, HTML, and CSS. If you prefer, you can also create layouts in HTML or with a visual designer. Vaadin apps run on the server and handle all communication automatically and securely. More related topics, Vaadin + Spring Boot + JPA CRUD UI example Vaadin + Kotlin CRUD example   Following technologies stack being used: Spring Boot 2.1.1.RELEASE Maven 3 JDK 1.8 Eclipse Oxygen Vaadin 1. Project Structure 2. Maven/Dependency Management [pom.xml]   <?xml version = "1.0" encoding = "UTF-8" ?> <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0  http://maven.apache.org/xsd/maven-4.0.0.xsd