我有一个Query类,它可以保存我可以发送的查询:
Class Query {
Integer product_id
Integer collection_id
Integer id
}
我使用对象映射器将我的Query对象转换为这样的映射:
def q = new Query(product_id: 12345)
Map <String, Object> toMap = new ObjectMapper().convertValue( q, Map )
然后我依次传递我的RESTClient,因此它包含在请求中
def client = new RESTClient ('http://somewebsite.com')
client.get(path: 'somePath/anotherPath.json',
contentType: ContentType.JSON,
query: q)
发送请求后,查询映射中的空键也会在请求中发送,从而导致响应出现问题
GET somePath/anotherPath.json?product_id=12345&collection_id=&id=
正如标题所示,有没有办法在地图中删除带有空值的键,这样当您发送REST GET请求时它们就不会包含在请求中。我希望它是这样的:
GET somePath/anotherPath.json?product_id=12345
在请求中未发送具有空值(collection_id和id)的键。
答案 0 :(得分:1)
您可以使用注释@JsonInclude(Include.NON_NULL)
//I suggest you store blog images in public folder
//I assume you have created this folder `public\blogpost`
$path = public_path("blogpost/{$postId}");
//Lets create path for post_id if it doesn't exist yet e.g `public\blogpost\23`
if(!File::exists($path)) File::makeDirectory($path, 775);
//Lets save the image
$image->save($path . '/' . $file->getClientOriginalName());
请参阅文档here
答案 1 :(得分:0)
ObjectMapper
是否有任何禁用导出空值的参数?
如果没有,你可以这样做:
Map <String, Object> toMap = new ObjectMapper().convertValue(q, Map).findAll { it.value != null }