Skip to main content

Spring Boot Prompt for Java Records & Sealed Interface Patterns

Modernize Java domain modeling with records, sealed interfaces, and pattern matching — immutable value objects and exhaustive switches.

Vul de plaatshouders in

Edit the values, then copy your finished prompt.

Jouw Prompt
prompt.txt

                                

What this prompt does

This prompt asks the AI to refactor a [domain] Java [java_version] application toward modern language features so illegal states become unrepresentable. It introduces records for immutable value objects ([value_objects]), sealed interfaces for the [sealed_hierarchies] with exhaustive switch expressions, pattern matching for instanceof with deconstruction, record patterns in switch for [switch_use_case], compact constructors with validation, record-based DTOs replacing POJOs with a shown migration, a sealed-interface Result<T, E> type for error handling without exceptions, Jackson serialization of sealed hierarchies, record projections for JPA, and unit tests verifying immutability, equals/hashCode, and switch exhaustiveness.

The structure works because records and sealed hierarchies let the compiler enforce correctness: an exhaustive switch over a sealed type fails to compile if you miss a case, and a Result type makes error handling explicit instead of relying on thrown exceptions. Grounding the work in your real [value_objects] and [sealed_hierarchies] keeps it a genuine refactor rather than a feature tour. Compact constructors carry their weight here as well, validating invariants at construction time so an invalid value object cannot exist in the first place, which removes a layer of defensive checks elsewhere in the code.

When to use it

  • You are modeling a domain where some types form a closed set and you want exhaustive handling enforced.
  • You want immutable value objects ([value_objects]) as records instead of mutable POJOs.
  • You want pattern matching and record deconstruction to replace chains of instanceof and casts.
  • You prefer explicit error handling via a Result type over exceptions for expected failures.
  • You are migrating DTOs to records and want a clear before/after path.
  • You want tests that verify immutability and that switches stay exhaustive as the hierarchy grows.

Example output

Expect record definitions for [value_objects] with compact-constructor validation, sealed interfaces for [sealed_hierarchies] with exhaustive switches, pattern-matching examples including record patterns for [switch_use_case], a Result<T, E> type, the POJO-to-record DTO migration, Jackson configuration for the sealed hierarchies, JPA record projections, and unit tests. It reads as a guided modernization of real code rather than isolated snippets.

Pro tips

  • List [value_objects] that are genuinely immutable (Money, AccountId, DateRange); records fit values, not entities with identity and lifecycle.
  • Define [sealed_hierarchies] as truly closed sets so exhaustive switches are meaningful and the compiler can enforce them.
  • Match [java_version] to your runtime, since record patterns and sealed-type support vary across recent Java releases.
  • Use compact constructors to validate invariants at construction, so an invalid value object simply cannot exist.
  • Keep records out of JPA entities and use them as projections instead, as the prompt does, since records do not fit Hibernate's entity lifecycle.
  • Iterate by asking it to model one specific hierarchy from [sealed_hierarchies] end to end, including the Jackson round-trip.

Frequently Asked Questions

Why use sealed interfaces with exhaustive switches?
A sealed interface defines a closed set of subtypes, so a switch over it can be checked for exhaustiveness at compile time. If you add a new subtype and forget to handle it, the code fails to compile, which catches a whole class of missed-case bugs early.
Can I use records as JPA entities?
No, and the prompt deliberately avoids that. Records are immutable and lack the no-arg constructor and mutability Hibernate's entity lifecycle expects. Instead, the prompt uses records as query projections, which is where their immutability is an advantage rather than an obstacle.
What is the Result type for?
It is a sealed-interface `Result<T, E>` that represents success or failure explicitly, so expected errors are returned as values instead of thrown as exceptions. This makes error handling visible in method signatures and forces callers to handle both outcomes.
Does it handle JSON serialization of sealed types?
Yes. The prompt configures Jackson to serialize and deserialize the sealed hierarchies, which usually needs type information so the right subtype is reconstructed. Without that configuration, polymorphic sealed types do not round-trip correctly through JSON.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

Meer in Java & Spring Boot Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support