Spring ModelAttribute Fails to Convert List of Strings to Enums in Non-REST Controller

I’m working on a Spring Boot application where I need to handle form submissions from an HTML page. The form data includes a list of strings that should be mapped to an enum in my UserSurvey model.

Here’s a simplified version of my model:

data class UserSurvey(
    var reasonsForApplying: SurveyOptions<ApplicationReason>? = null,
    var concernsAboutProcess: SurveyOptions<ApplicationConcern>? = null
)

data class SurveyOptions<T>(
    var selected: MutableList<T>? = null,
    var other: String? = null
)

enum class ApplicationReason {
    CAREER_ADVANCEMENT, EDUCATIONAL_OPPORTUNITIES, SOCIAL_SECURITY_BENEFITS, FAMILY_REUNIFICATION, OTHER
}

enum class ApplicationConcern {
    HIGH_APPLICATION_FEE, LANGUAGE_DIFFICULTIES, LENGTHY_PROCESS, FEAR_OF_DENIAL, INSUFFICIENT_INFORMATION, OTHER
}

@Controller
class SurveyController {

    @PostMapping("/submitSurvey")
    fun submitSurvey(
        @ModelAttribute survey: UserSurvey
    ): String {
        survey. reasonsForApplying.selected.forEach {
            println(it)
        }
        return "survey-thank-you"
    }
}


When I change SurveyOptions<ApplicationReason> to SurveyOptions<String>, it works fine. However, with enum, while accessing the bean reasonsForApplying, I get the following error:

java.lang.String cannot be cast to com.example.controller.ApplicationReason

Looks like, spring is creating the List with type String instead of the Enum.

My HTML page

<form action="/submitSurvey" method="post">
    <label>Reasons for Applying:</label><br>
    <input type="checkbox" name="reasonsForApplying.selected" value="CAREER_ADVANCEMENT"> Career Advancement<br>
    <input type="checkbox" name="reasonsForApplying.selected" value="EDUCATIONAL_OPPORTUNITIES"> Educational Opportunities<br>
    <input type="checkbox" name="reasonsForApplying.selected" value="SOCIAL_SECURITY_BENEFITS"> Social Security Benefits<br>
    <input type="checkbox" name="reasonsForApplying.selected" value="FAMILY_REUNIFICATION"> Family Reunification<br>
    <input type="checkbox" name="reasonsForApplying.selected" value="OTHER"> Other<br>
    <input type="text" name="reasonsForApplying.other"><br><br>

    <label>Concerns About Process:</label><br>
    <input type="checkbox" name="concernsAboutProcess.selected" value="HIGH_APPLICATION_FEE"> High Application Fee<br>
    <input type="checkbox" name="concernsAboutProcess.selected" value="LANGUAGE_DIFFICULTIES"> Language Difficulties<br>
    <input type="checkbox" name="concernsAboutProcess.selected" value="LENGTHY_PROCESS"> Lengthy Process<br>
    <input type="checkbox" name="concernsAboutProcess.selected" value="FEAR_OF_DENIAL"> Fear of Denial<br>
    <input type="checkbox" name="concernsAboutProcess.selected" value="INSUFFICIENT_INFORMATION"> Insufficient Information<br>
    <input type="checkbox" name="concernsAboutProcess.selected" value="OTHER"> Other<br>
    <input type="text" name="concernsAboutProcess.other"><br><br>

    <button type="submit">Submit</button>
</form>

I’ve tried several approaches, including:

Property Editors: Registered a custom property editor, but setAsText and getAsText methods are not called.

Converters: Used ConversionService with custom converters, but the issue persists.

Custom Deserializers: Tried using Jackson’s @JsonDeserialize but that only works for REST controllers.

DataBinder: Also didn’t resolve the issue.

None of these approaches have worked in the context of a non-REST controller. How can I correctly map a list of strings from an HTML form to enum types in my model using @ModelAttribute in a non-REST controller?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật