这是我的模型类:
public class RequestBody {
@JsonProperty("polygon")
private GeoPolygon geoPolygon;
@JsonProperty("domain_id")
private String domainId;
private String availability;
@JsonProperty("start_time")
private String startTime;
@JsonProperty("end_time")
private String endTime;
@JsonProperty("page_size")
private int pageSize;
private int offset;
//getters, setters, toString()
下面是我的 Controller :
@RequestMapping(value = "/request", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity getResponse(RequestBody request){
// process request, return response.
}
这就是我调用 API 的方式:
http://localhost:9876/request?availability=TEMPORARY&start_time=2017-06-06T13:24:23Z&end_time=2017-06-05T13:24:23Z&polygon={"type":"polygon","coordinates":[[[-120,10],[-30,10],[-30,60],[-120,60],[-120,10]]]}&domain_id=XYZ&page_size=10&offset=1
现在的问题是:
所有属性都没有被映射。特别是带有 @JsonProperty
注释的。这些字段保持为空。
我将相同的模型发送到同一端点的 POST
请求,并且工作完全正常。 GET
不支持 @JsonProperty
吗?
最佳答案
在 JSON 序列化/反序列化期间将考虑 @JsonProperty
应用程序。如果提取的内容来自请求正文(又名 @RequestBody
Spring 注释),则会发生 JSON 反序列化。
您的RequestBody
(注意名称冲突)是从请求参数中提取的。在这种情况下,Spring 不会使用 JSON 反序列化,而只是调用相应的 Java Bean setter。
如果您希望 startTime
与 start_time
映射,您的 setter 必须是:
public setStart_time(String startTime) {
this.startTime = startTime;
}
其他领域也是如此。
关于java - 缺少 @RequestParam 变量的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44452727/