[英]How I can make RequestParam dynamic?
I have a list of POST
requests, where request bodies are quite similar 我有一个POST
请求列表,其中的请求正文非常相似
{
"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
我有一个通用的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 在我的Post Controller
我有方法
@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? 无论如何,是否可以根据类型分配通用类型T
? 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. 这可以通过使用@JsonTypeInfo
批注来完成。
For example: 例如:
Define entities according to different structures under "parts" key: 根据“ parts”键下的不同结构定义实体:
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: 在EntityBody
,删除type
字段并添加注释:
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. 您可以使用JsonTypeInfo
和JsonSubTypes
Jackson
批注。 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. 如您所见,您不需要type
属性。 It will be read by Jackson
to find out a car type. Jackson
将阅读该书以找出汽车类型。 I have created Car
base class/interface but you do not need to do that. 我已经创建了Car
基类/接口,但是您不需要这样做。
Your POST
method could look like this: 您的POST
方法可能如下所示:
@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. 您在这里不需要PathVariable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.