-1

I have a @PostMapping that allows the user to send a plain json map, like:

{
   "firstname": "john",
   "lastname": "doh"
}

Servlet:

@RestController 
public class PersonController {
   @PostMapping("/generic")
   public void post(Map<String, String> params) {
   }
}

This works fine. But now I want to accept also a list of objects on the same endpoint. But I cannot just add another method that takes a different parameter. Because spring the complains about ambiguous mapping:

   @PostMapping("/generic")
   public void post2(List<Map<String, String>> params) {
   }

Question: how can I accept json data that can be both a Map and a List? I could lateron continue in the business code conditionally if the input is either map/list. But how can I accept them at all in a spring controller side by side?

membersound
  • 66,525
  • 139
  • 452
  • 886
  • 1
    Why on the same endpoint? If you control the clients you could require that all requests must be lists, even if with one element inside. Also, instead of sending just a Map, I'd go for a strongly typed DTO (and I'd make separate endpoints for single object vs. list of dtos). – Kayaman Apr 19 '18 at 11:38
  • 1
    Well you certainly managed to rack up downvotes quickly... – Kayaman Apr 19 '18 at 11:39
  • Well for the purpose of the question the *why* does not matter. I'm just asking for the *how*. But as a sidenote: I'm creating a generic database service with generic CRUD templates. The same endpoint should be used for all requests, and decisions are made based on the templates requested. – membersound Apr 19 '18 at 11:39
  • 1
    For me the *why* always matters, I'm not here to just answer people's questions like some sort of overpaid robot. From experience I can also tell you that while the idea of "generic systems" is often appealing, it usually results in horrible horrible things like the [Inner Platform Effect](https://en.wikipedia.org/wiki/Inner-platform_effect). Not saying it does so here, but I've had plenty of bad experiences with people trying to "code less" and come up with horrible results. – Kayaman Apr 19 '18 at 11:45
  • 1
    To rephrase your question: "I want a square that is both red and blue at the same time." Maybe yours is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), where you've solved (or think you've solved) half the problem, and want help solving the other half. Maybe it's best to know the entire problem, and yes, the "why" is indeed important. – SeverityOne Apr 19 '18 at 11:45
  • 1
    Why not using `@PostMapping` as `@PostMapping(value = "/generic", params = "unique-identifier")` with different "unique-identifier" on different methods – Afridi Apr 19 '18 at 12:04

1 Answers1

0
@PostMapping
public void post(JsonNode json) {
    if (json.isObject()) {
        Map<String, String> map = mapper.convertValue(json, Map.class);
    } else if (json.isArray()) {
        List<Map<String, String>> list = mapper.convertValue(json, List.class);
    }
}
membersound
  • 66,525
  • 139
  • 452
  • 886