How to accept RequestBody of different class types dynamically

JackAss

I am using Spring Boot . Writing rest api's where for the same api url , the request json structure varies Is there any way we can apply Factory design or some thing else

    @RequestMapping(value = "/myservice/{type}", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<?> myServiceApi(@PathVariable String type,
        @RequestBody SomeClass1 somereq) {
    // here based on type , the RequestBody can be either SomeClass1 or SomeClass2 
    // both the SomeClass1 and SomeClass2 has nothing in common .

}

The above code will work only if the request json is in SomeClass1 format , but i needed it to accept among {SomeClass1 , SomeClass2}

Plog

You could do this by passing the JSON as a String into your controller method and then mapping this to whichever object you expect to need:

@PostMapping(value = "/myservice/{type}")
public ResponseEntity<?> myServiceApi(@PathVariable String type,
         @RequestBody String somereq) {
     ObjectMapper mapper = new ObjectMapper();
    if (<something that indicates SomeClass1>) {
        SomeClass1 someClass1 = mapper.readValue(somereq, SomeClass1.class);
    } else if (<something that indicates SomeClass2>) {
        SomeClass2 someClass2 = mapper.readValue(somereq, SomeClass2.class);
    }
}

Although to be honest if you really are expecting bodies with completely different structures my advice would be to just make separate API calls for these.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to accept different types of slices in the same function?

Typescript: How to accept 2 different types

How to get a class with generic type accept an array of different by same generic types?

How to hint types for class methods that are dynamically created?

How can I make my function accept different types of input?

How to make spark udf accept a list with different data types?

How to make List in python dataclass that can accept multiple different types?

How to make a Record in TypeScript can accept different types

How to depict in a UML class diagram that a method accept multiple types?

How to accept multiple query parameters with different parameter name in one class

How to dynamically allocate structure memory and add data of different data types into it?

Python: How to rebase or dynamically replace a class with a different base class

Allowing TextBox.Text to accept different types

How to accept either of two types

Spring @RequestBody containing a list of different types (but same interface)

Add methods to class dynamically with types?

How can i change following interface to accept parameters for different types of messaging system. any design pattern ?

How do I make a generic List which can accept two different, unrelated types?

How can I make a Java method accept arrays of different (primitive) variable types?

How to Add a generic type to a component accept data as a props ( data might have different types)

How to dynamically change view background colour from different class

How to define different types for the same class in C++

How to deserialize JSON string into different complex class types?

How to create a generic factory returning different interface types and an abstract class

How to make a copy constructor for different types within a template class?

Create method on different class types

Register same class for different types

Putting enum types in different class

How to specify that I can accept all media types in the Accept header?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive