[JAVA] Convert request parameter to Enum in Spring

Introduction

I was looking for a good method because it was troublesome to get the value to be handled by Enum once with String and convert it.

Conversion method

Prepare an Enum and write the acquisition and generation process using @JsonValue and @JsonCreator.

EnvironmentType.java


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.stream.Stream;

public enum EnvironmentType {
    PRODUCTION(1),
    STAGE(2),
    DEVELOP(3);

    private int value;

    EnvironmentType(int value) {
        this.value = value;
    }

    @JsonValue
    public int getValue() {
        return value;
    }

    @JsonCreator
    public static EnvironmentType create(Integer value) {
        if (value == null) {
            return null;
        }
        return Stream.of(values())
                .filter(v -> value.equals(v.getValue()))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException());
    }
}

Define properties using Enum type for Bean used in request.

Request.java


import EnvironmentType;

public class Request {
    private EnvironmentType type;

    public EnvironmentType getType() {
        return type;
    }

    public void setType(EnvironmentType type) {
        this.type = type;
    }
}

At the end

It was easy to write using Jackson annotations.

Recommended Posts

Convert request parameter to Enum in Spring
Refer to enum in Thymeleaf
How to use Lombok in Spring
Convert numbers to Roman numerals in Ruby
How to include Spring Tool in Eclipse 4.6.3?
Convert SVG files to PNG files in Java
To write Response data directly in Spring
How to bind request parameters in list format with bean property in Spring
Convert to a tag to URL string in Rails
Output request and response log in Spring Boot
JUnit 5: How to write test cases in enum
[Java] Convert DB code to code value using enum
How to add a classpath in Spring Boot
Map GET requests to complex objects in Spring.
Request parameter log output sample Java & Spring MVC
How to bind to property file in Spring Boot
How to define multiple orm.xml in Spring4, JPA2.1
Convert Java enum enums and JSON to and from Jackson
Sample code to convert List to List <String> in Java Stream
[Ruby on Rails] How to write enum in Japanese
How to create a Spring Boot project in IntelliJ
Convert csv file to fixed length record file using enum
How to use CommandLineRunner in Spring Batch of Spring Boot
How to test file upload screen in Spring + Selenium
How to convert A to a and a to A using AND and OR in Java
Introduce swagger-ui to REST API implemented in Spring Boot
How to convert a file to a byte array in Java
How to use In-Memory Job repository in Spring Batch
Inject Logger in Spring
To debug in eclipse
Use Interceptor in Spring
Microservices in Spring Cloud
Get cookies in Spring
Let's find out how to receive in Request Body with REST API of Spring Boot
How to change application.properties settings at boot time in Spring boot
Understand the rough flow from request to response in SpringWebMVC
Convert a Java byte array to a string in hexadecimal notation
How to call and use API in Java (Spring Boot)
How to use Java enums (Enum) in MyBatis Mapper XML
Create API to send and receive Json data in Spring
I tried to convert a string to a LocalDate type in Java
To receive an empty request with Spring Web MVC @RequestBody
Set up Multipart Resolver to allow file uploads in Spring
I wrote a code to convert numbers to romaji in TDD
[Rails] How to write user_id (foreign key) in strong parameter
Use @ControllerAdvice, @ExceptionHandler, HandlerExceptionResolver in Spring Boot to catch exceptions
[Ruby] How to batch convert strings in an array to numbers
How to control transactions in Spring Boot without using @Transactional