Spring Boot – How to init a Bean for testing?

In Spring Boot, we can create a @TestConfiguration class to initialize some beans for testing class only. P.S Tested with Spring Boot 2 1. @TestConfiguration + @Import This @TestConfiguration class will not pick up by the component scanning, we need to import it manually. TestConfig.java package com.mkyong; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import java.time.Duration; …

Read more

ElasticSearch Hello World Example

ElasticSearch is an Open-source Enterprise REST based Real-time Search and Analytics Engine. It’s core Search Functionality is built using Apache Lucene, but supports many other features. It is written in Java Language. It supports Store, Index, Search and Analyze Data in Real-time. Like MongoDB, ElasticSearch is also a Document-based NoSQL Data Store. Note ElasticSearch website: …

Read more

Spring REST API Validation

In Spring MVC, just annotate a @Valid on the @RequestBody to fire the validation process. Note For complete source code, please refer to this – Spring Boot Ajax example P.S Tested with Spring Boot 1.5.1.RELEASE (Spring 4.3.6.RELEASE) 1. JSR 303 Validation Add JSR303 annotations on a bean. package com.mkyong.model; import org.hibernate.validator.constraints.NotBlank; public class SearchCriteria { …

Read more

Spring Boot Ajax example

This article will show you how to use jQuery.ajax to send a HTML form request to a Spring REST API and return a JSON response. Tools used : Spring Boot 1.5.1.RELEASE Spring 4.3.6.RELEASE Maven 3 jQuery Bootstrap 3 Note You may interest at this classic Spring MVC Ajax example 1. Project Structure A standard Maven …

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

cURL – Post JSON data to Spring REST

This article shows you how to use cURL command to POST JSON data to a Spring REST API. 1. Spring REST A Simple Spring REST API to validate a login. LoginController.java package com.mkyong.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller public class LoginController { private final Logger …

Read more

Spring Boot file upload example – Ajax and REST

This article shows you how to upload files in Spring Boot web application (REST structure), using Ajax requests. Tools used in this article : Spring Boot 1.4.3.RELEASE Spring 4.3.5.RELEASE Thymeleaf jQuery (webjars) Maven Embedded Tomcat 8.5.6 Google Chrome Browser (Network Inspect) 1. Project Structure A standard Maven project structure. 2. Project Dependency Declares an extra …

Read more