0

I am working in a SpringBoot project.

In my model class I have a enum type property:

public class Car {
   CarType type;
   ...
}

The enum CarType:

public enum CarType {
  SEDAN, HATCHBACK, SUV
}

In my DTO layer, the payload of request should tell the car type, normally it uses String type to indicate car type:

public class RequestPayload {
   String carType;
   ...
}

But if I define it as enum type:

public class RequestPayload {
       CarType carType;
       ...
}

I wonder is there a way to directly map the value from json to enum type value?

1 Answer 1

1

Yes, you can define carType as an enum in your DTO Layer.

public class RequestPayload {
       CarType carType;
       ...
}

It will Automatically Maps Json value to enum and in case value doesn't match enum values then will throw an Exception.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you mean springboot will automatically maps json to enum? Or who will do so exactly? Do I need to have dependency of some 3rd party library to do so?
No, you don't need any Third party Library to do the same. Springboot will Automatically maps it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.