嘿,我有一个包含多个查询参数的URL,用于搜索。这是一个HATEOAS链接。
https://someurl/customers?customer-id={customer-id}&type={type}&something={something}
我希望只替换其中两个参数。
Map<String, String> params = new HashMap<>();
params.put("customer-id", customerId);
params.put("something", something)
UriComponents customerUrl = UriComponentsBuilder
.fromHttpUrl(specialURL)
.buildAndExpand(params).encode();
但是这会抛出异常。
java.lang.IllegalArgumentException: Map has no value for 'type'
at org.springframework.web.util.UriComponents$MapTemplateVariables.getValue(UriComponents.java:346) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
这里最好的解决方法是什么?假设我有大约7个参数,用空字符串替换它们或将字符串切成一半似乎都很别扭。
builder.replaceQuery("")
。 - Wim Deblauwe