Spring Boot Testcontainers Example

This article shows how to test the Spring Boot REST endpoints using TestRestTemplate and Testcontainers (PostgreSQL container). Technologies used : Spring Boot 3.1.2 (Spring Web MVC, Spring Data JPA and Spring Test) Testcontainers 1.19.0 PostgreSQL 15, Alpine Linux base image postgres:15-alpine Java 17 JUnt 5 Tables of contents: 1. Project Structure 2. Project Dependencies 3. …

Read more

Spring Boot JobRunr examples

This article shows how to use Spring Boot to create REST endpoints to run the background jobs managed by the JobRunr. Tested with: Spring Boot 2.3.12.RELEASE JobRunr 3.1.2 Maven 3 1. Directory Structure. A standard Maven project structure. 2. Project Dependencies. We need a single jobrunr-spring-boot-starter to integrate Spring Boot web and JobRunr. pom.xml <!– …

Read more

Spring Boot + Spring Data JPA + PostgreSQL example

This article shows how to use Spring Web MVC to create REST endpoints for CRUD database operations using the Spring Data JPA and PostgreSQL. At the end of the tutorial, we will use Docker to start a PostgreSQL container to test the Spring Boot REST endpoints using curl commands. We will use Spring Test and …

Read more

Spring REST Error Handling Example

In this article, we will show you error handling in Spring Boot REST application. Technologies used : Spring Boot 2.1.2.RELEASE Spring 5.1.4.RELEASE Maven 3 Java 8 1. /error 1.1 By default, Spring Boot provides a BasicErrorController controller for /error mapping that handles all errors, and getErrorAttributes to produce a JSON response with details of the …

Read more

Spring REST Validation Example

In this article, we will enhance the previous Spring REST Hello World example, by adding bean validation and custom validator. Technologies used : Spring Boot 2.1.2.RELEASE Spring 5.1.4.RELEASE Maven 3 Java 8 1. Controller Review the previous REST Controller again : BookController.java package com.mkyong; import com.mkyong.error.BookNotFoundException; import com.mkyong.error.BookUnSupportedFieldPatchException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import …

Read more

Spring REST Hello World Example

In this article, we will show you how to develop a Spring Boot REST style web service to handle CRUD operations from a H2 In-memory database. Technologies used : Spring Boot 2.1.2.RELEASE Spring 5.1.4.RELEASE Spring Data JPA 2.1.4.RELEASE H2 In-memory Database 1.4.197 Tomcat Embed 9.0.14 JUnit 4.12 Maven 3 Java 8 1. Project Directory 2. …

Read more

cURL – POST request examples

Some cURL POST request examples for self reference. 1. Normal POST 1.1 To POST without data. $ curl -X POST http://localhost:8080/api/login/ 1.2 To POST with data. $ curl -d "username=mkyong&password=abc" http://localhost:8080/api/login/ 1.3 Spring REST to accept normal POST data. @PostMapping("/api/login") public ResponseEntity<?> login(@RequestParam("username") String username, @RequestParam("password") String password) { //… } @PostMapping("/api/login") public ResponseEntity<?> login(@ModelAttribute …

Read more

Spring REST @RequestMapping extract incorrectly if value contains ‘.’

Problem See following Spring REST example, if a request such as “http://localhost:8080/site/google.com” is submitted, Spring returns “google“. Look like Spring treats “.” as file extension, and extract half of the parameter value. SiteController.java package com.mkyong.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/site") public class SiteController { @RequestMapping(value = "/{domain}", method …

Read more

Spring 3 REST hello world example

In Spring 3, old RequestMapping class is enhanced to support RESTful features, which makes Spring developers easier to develop REST services in Spring MVC. In this tutorial, we show you how to use Spring 3 MVC annotations to develop a RESTful style web application. 1. Project Directory Review the project folder structure. 2. Project Dependency …

Read more