Backend Development 9 min read

Explore 100+ Spring Boot 3 Real-World Cases: DB Init, Redis, Security & More

This article presents a continuously updated collection of over 120 Spring Boot 3 practical examples, covering database initialization scripts, switching Redis clients, disabling and customizing security, configuring HTTP clients, and fine‑tuning Spring MVC behavior with code snippets and configuration details.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Explore 100+ Spring Boot 3 Real-World Cases: DB Init, Redis, Security & More

1. SQL Script Database Initialization

Spring Boot can automatically create a JDBC datasource or R2DBC ConnectionFactory and run DDL (schema.sql) and DML (data.sql) scripts. By default it loads optional:classpath*:schema.sql and optional:classpath*:data.sql . Locations can be customized via spring.sql.init.schema-locations and spring.sql.init.data-locations . The optional: prefix allows the application to start even if the files are missing; remove it to make missing files cause startup failure.

Example schema.sql :

<code>drop table if exists t_user;
create table t_user(
  id int auto_increment primary key,
  name varchar(32),
  sex varchar(10),
  idNo varchar(18)
);
</code>

Example data.sql :

<code>insert into t_user (name, sex, idNo) values ('张三','男','1011111');
insert into t_user (name, sex, idNo) values ('李四','女','2022222');
insert into t_user (name, sex, idNo) values ('王五','女','3033333');
insert into t_user (name, sex, idNo) values ('岳不群','男','4044444');
</code>

To enable script execution on non‑embedded databases set:

<code>spring:
  sql:
    init:
      mode: always
</code>

Custom script locations:

<code>spring:
  sql:
    init:
      mode: always
      schema-locations:
        - optional:classpath:script/schema.sql
      data-locations:
        - optional:classpath:script/data.sql
</code>

Continue on error configuration:

<code>spring:
  sql:
    init:
      continue-on-error: true
</code>

2. Switching Redis Client

Spring Boot starter-data-redis uses Lettuce by default. To switch to Jedis, exclude Lettuce and add Jedis dependency:

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-data-redis&lt;/artifactId&gt;
  &lt;exclusions&gt;
    &lt;exclusion&gt;
      &lt;groupId&gt;io.lettuce&lt;/groupId&gt;
      &lt;artifactId&gt;lettuce-core&lt;/artifactId&gt;
    &lt;/exclusion&gt;
  &lt;/exclusions&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
  &lt;groupId&gt;redis.clients&lt;/groupId&gt;
  &lt;artifactId&gt;jedis&lt;/artifactId&gt;
&lt;/dependency&gt;
</code>

3. Security Configuration

3.1 Disable Default Security

Defining a @Configuration class with a SecurityFilterChain bean disables Spring Boot’s default web security, which otherwise blocks all requests.

<code>@Configuration
public class SecurityConfig{
  @Bean
  SecurityFilterChain apiSecurity(HttpSecurity http) throws Exception{
    // custom security settings
    return http.build();
  }
}
</code>

3.2 Custom UserDetailsService

If a bean of type AuthenticationManager , AuthenticationProvider or UserDetailsService is provided, the default InMemoryUserDetailsManager is not created. Provide your own UserDetailsService to add user accounts.

4. HTTP Clients

4.1 Custom RestTemplate

Use RestTemplateCustomizer or RestTemplateBuilder to create a customized RestTemplate , for example with proxy settings.

<code>@Bean
RestTemplate restTemplate(RestTemplateBuilder builder){
  // builder.xxx
  return builder.build();
}

@Bean
RestTemplate rest(ObjectProvider<RestTemplateCustomizer> customizer){
  RestTemplate rt = new RestTemplate();
  customizer.orderedStream().forEach(c -> c.customize(rt));
  return rt;
}
</code>

5. Spring MVC Tweaks

5.1 XML Output

Add the Jackson XML module to enable XML responses:

<code>&lt;dependency&gt;
  &lt;groupId&gt;com.fasterxml.jackson.dataformat&lt;/groupId&gt;
  &lt;artifactId&gt;jackson-dataformat-xml&lt;/artifactId&gt;
&lt;/dependency&gt;
</code>

5.2 Custom Jackson ObjectMapper

Configure ObjectMapper or XmlMapper beans to adjust serialization features as needed.

5.3 Custom @ResponseBody Rendering

Register a custom MappingJackson2HttpMessageConverter to modify JSON output, e.g., adding a prefix:

<code>@Bean
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
  MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  converter.setJsonPrefix("pack");
  return converter;
}
</code>

5.4 Change DispatcherServlet Path

<code>spring:
  mvc:
    servlet:
      path: "/api"
</code>

5.5 Disable Spring MVC Auto‑configuration

Annotate a configuration class with @EnableWebMvc to take full control over MVC settings.

RedisSpring BootsecuritySpring MVCHTTP ClientDatabase Initialization
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.