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 9268775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:56:43+00:00 2026-06-18T14:56:43+00:00

The scenario I am required to build an API that provides clients access to

  • 0

The scenario

I am required to build an API that provides clients access to several kinds of objects that are stored in a DB (my DAO is Hibernate-based as of today). These can be GETted or POSTed.

Currently the GET signature is the following:

@RequestMapping(value = "/rest/{datatype}", method = RequestMethod.GET, produces = { "application/json" })
    public @ResponseBody
    Object[] getData(@PathVariable("datatype") String dataType,
            WebRequest request) throws HttpException {

This means that depending on the data type in the URL I will be going to access a given repository and return an object of my business domain. It works fine so far.

For POST, I must be able to submit an object in JSON format, convert it to a class and store it into the database.

Obviously if I use postFunfData(SpecificDataType object) Jackson converts JSON to object successfully, but I don’t know the object type at compile time.

Another requirement is flexibility: When a new class is added to the domain, little code work has to be done to support the new datatype. In the GET ter I just add a switch to support the new datatype, instantiate correct repository with Spring and query the DB.

The question

How do I write a Spring MVC REST API that accepts "anything" as request body and allows me to convert from JSON to the correct object? Something like:

public void postData(@RequestBody Object objData){
    if (objData instanceof Class1) {
        Class1 obj = (Class1) objData;
        Class1Repository.store(obj);
    }
    [...]
}
  • 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-18T14:56:44+00:00Added an answer on June 18, 2026 at 2:56 pm

    Possible answer

    (Please provide your one if you find a better solution)

    Create several simple APIs based on the same skeleton

    @RequestMapping(value = "/rest/class1", method = RequestMethod.POST, consumes = { "application/json" })
    public void postData(@RequestBody Class1 objOfClass1){
        store(objOfClass1)
    }
    
    @RequestMapping(value = "/rest/class2", method = RequestMethod.POST, consumes = { "application/json" })
    public void postData(@RequestBody Class2 objOfClass2){
        store(objOfClass2)
    }
    

    This way when Spring MVC analyzes the path it finds the correct controller method that handles the request. Then it asks Jackson to convert the request body to the argument type detected by reflection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we're trying to build a web of connected clients, where each client.exe also provides
I have the following scenario.The implementation is required for a real time application. 1)I
Scenario first :- I'm using Entity Framework to do some queries, to build my
I have below scenario Got n as minimum number of rows required for the
EDIT Ok, @dhg discovered that dot-method syntax required if the code block to fold()
Scenario: WCF client app, calling a web-service (JAVA) operation, wich requires a complex object
Scenario : I'll try to put an analogy with the loan broker example from
Scenario: There is a website with 700-900 concurrent unique visitors at any given time.
Scenario: On the github exists the user guru and his primamy repository for the
Scenario While using the Maven Ant Task artifact:deploy , I'm encountering the error java.lang.OutOfMemoryError:

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.