Open In App

Enum Mapping in Spring Boot

Spring Boot framework is one of the famous frameworks for developing web applications. This framework provides a lot of features to optimize the software application code. Spring Auto-Configuration, Spring Annotations, Spring Integration Spring Security and other features are available in the Spring Boot framework.

Spring Boot is open open-source Java-based framework And also Spring Boot is also used for standalone and production-grade Spring-based applications with less effort. Now, we will explain Enum mapping in the Spring Boot framework. The Enum is used for declaring the constants with upper case letters. Below we provide a simple Spring Boot project for mapping Enum.

What is Enum?

Syntax:

public enum Role {
    ELEMENT_1, ELEMENT_2 
}

Prerequisites:

Project Structure:

Below we can see the structure of the project, after successfully creating the project.


Project Structure


Steps to Implement Enum Mapping in Spring Boot

Here, we will create a simple Spring Boot application for Enum Mapping in Spring Boot by utilizing RestController API endpoints. Upon accessing these APIs in a browser, we'll be able to view the output.

Step 1:

Create a Spring Boot project by using Spring initializer with required project dependencies.

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}


Step 2:

Once project is successfully created, then configure the mongo db connection by using database name, port number and host name.

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=work


Step 3:

package com.app;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Document(collection = "userdata")
public class User {
    @Id
    private String id;
    private String username;
    private String age;
    private Role role;
}


Step 4:

package com.app;

public enum Role {
    ADMIN, USER, GUEST
}


Step 5:

package com.app;

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.stereotype.Repository;

@Repository
@EnableMongoRepositories
public interface UserRepository extends MongoRepository<User, String> {

    List<User> findByRole(Role role);

}


Step 6:

package com.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    
    @Autowired
    private UserRepository userRepository;
    
    // save user data
    public User createUser(User user) {
        return userRepository.save(user);
    }
    
    // get all users data
    public List<User> findAllUsers() {
        return userRepository.findAll();
    }
    
    // find by role 
    public List<User> findUsersByRole(Role role) {
        return userRepository.findByRole(role);
    }
}


Step 7:

package com.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserService userService;
    
    // Endpoint for creating a new user
    @PostMapping("/save")
    public ResponseEntity<User> createUser(@RequestBody User user) {
        User createdUser = userService.createUser(user);
        return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
    }
    
    // Endpoint for retrieving all users
    @GetMapping("/getAll")
    public ResponseEntity<List<User>> getAllUsers() {
        List<User> users = userService.findAllUsers();
        return ResponseEntity.ok(users);
    }
    
    // Endpoint for finding users by role
    @GetMapping("/byRole")
    public ResponseEntity<List<User>> findUsersByRole(@RequestParam("role") Role role) {
        List<User> users = userService.findUsersByRole(role);
        return ResponseEntity.ok(users);
    }
    
}


Step 8:

Once entire logic is developed, then run this project as Spring Boot App and this project run on server port number 8080 and this project uses Apache Tomcat server. Below is the output image for reference.


Running Project


Step 9:

http://localhost:8080/save
Save API


Step 10:

http://localhost:8080/getAll
Get All API


Step 11:


User Role Output

If we follow the above steps, then we can demonstrate the enum mapping in spring boot application.

Article Tags :