Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8985903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:25:40+00:00 2026-06-15T21:25:40+00:00

I’d like to accept JSON on an REST endpoint and convert it to the

  • 0

I’d like to accept JSON on an REST endpoint and convert it to the correct type for immediate validation.

The endpoint looks like this:

@POST
public Response createCar(@Valid Car car) { 
    // persist to DB
    // ... 
}

But there are many subclasses of Car, e.g. Van, SelfDrivingCar, RaceCar, etc. How could I accept the different JSON representations on the endpoint, while keeping the validation code
in the Resource as concise as something like @Valid Car car?

Again: I send in JSON like (here, it’s the representation of a subclass of Car, namely SelfDrivingCar):

{ 
    "id" : "t1",                      // every Car has an Id
    "kind" : "selfdriving",           // every Car has a type-hint
    "max_speed" : "200 mph",          // some attribute
    "ai_provider" : "fastcarsai ltd." // this is SelfDrivingCar-specific
}

and I’d like the validation machinery look into the kind attribute, create an instance of the appropriate subclass, here e.g. SelfDrivingCar and perform validation.

I know I could create different endpoints for all kind of cars, but thats does not seem DRY. And I know that I could use a real Validator instead of the annotation and do it by hand, so I’m just asking if there’s some elegant shortcut for this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:25:41+00:00Added an answer on June 15, 2026 at 9:25 pm

    It’s not the responsibility of your validation framework to extract the correct polymorphic type and validate it. Infact, by the time validation kicks in, the object has already been created. It can only work with what it has at that point. Note however that:

    public Response createCar(@Valid Car car) {
        // ...
    }
    

    Will invoke validation on whatever the runtime type of ‘car’ actually is. So quite obviously what you want to do is assure that the car variable is bound at runtime to the correct type. This is the responsibility of your deserialization framework. You will need to determine the correct way to do this with the JSON library being used in your application, but with Jackson you would have used the @JsonTypeInfo annotation to scope your polymorphic types.

    @JsonTypeInfo(use=Id.NAME, include=As.PROPERTY, property="kind")
    public class Car implements Serializable {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y’all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.