简体   繁体   中英

How I can make RequestParam dynamic?

I have a list of POST requests, where request bodies are quite similar

{
  "entity":{
             "type":"Nissan"
             "parts":{
                     "Nissan_unique_content1":"value",
                     "Nissan_unique_content2":"value"
                   }
           }
  "updateDate":"Date..."
}
{
  "entity":{
             "type":"Ford"
             "parts":{
                     "Ford_unique_content1":"value",
                     "Ford_unique_content2":"value",
                     "Ford_unique_content3":"value"
                   }
           }
  "updateDate":"Date..."
}

I have a generic RequestBody

public class RequestBody<T>{
  EntityBody<T> entity;
  Date updateDate;
}

public class EntityBody<T>{
  String type;
  T parts;
}

In my Post Controller I have method as

@RequestMapping(value = "/{type}")
public ResponseEntity<?> create(
            @PathVariable(value = "type") String type,
            @RequestBody RequestBody<T> body) {
...
}

Is there anyway that generic type T can be assigned depends on type? In this case I wouldn't need create multiple create method, otherwise I need create multiple method, like

@RequestMapping(value = "/nissan")
public ResponseEntity<?> createNissan(
            @RequestBody RequestBody<NissanContent> body) {
...
}

@RequestMapping(value = "/ford")
public ResponseEntity<?> createFord(
            @RequestBody RequestBody<Ford> body) {
...
}

which are unnecessary repetitions.

This can be done by using @JsonTypeInfo annotation.

For example:

Define entities according to different structures under "parts" key:

class NissanParams {

  @JsonProperty("Nissan_unique_content1")
  private String nissanUniqueContent1;

  @JsonProperty("Nissan_unique_content2")
  private String nissanUniqueContent2;

  // getters + setters

}

In EntityBody , remove type field and add the annotations:

public class EntityBody<T> {

  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
  @JsonSubTypes({ @JsonSubTypes.Type(value = NissanParams.class, name = "Nissan")})
  private T parts;

}

And there will be a single controller method:

@PostMapping(path = "{make}",
  produces = MediaType.APPLICATION_JSON_VALUE,
  consumes = MediaType.APPLICATION_JSON_VALUE)
public RequestBody<Object> create(@PathVariable("make") String make,
                                 @org.springframework.web.bind.annotation.RequestBody RequestBody<Object> body) {
  // please change the name of "RequestBody" entity, in order to avoid name clash with the annotation
}

You can use JsonTypeInfo and JsonSubTypes Jackson annotations. Your model could look like:

class EntityBody {

    private Car parts;

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
    @JsonSubTypes({
            @JsonSubTypes.Type(name = "Ford", value = Ford.class),
            @JsonSubTypes.Type(name = "Nissan", value = Nissan.class)
    })
    public Car getParts() {
        return parts;
    }
}

As you can see, you do not need type property. It will be read by Jackson to find out a car type. I have created Car base class/interface but you do not need to do that.

Your POST method could look like this:

@RequestMapping(value = "/cars", method = RequestMethod.POST)
public ResponseEntity<?> create(@RequestBody RequestPayload body) {
    logger.info(body.toString());

    return ResponseEntity.ok("OK");
}

You do not need PathVariable here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM