我的网址编码器有问题,它可以在本地和舞台上工作,但不能在现场工作。 我正在使用过滤器来获取品牌名称,但其中一些品牌名称带有空格。当我实时发送请求时,它返回 500 错误。
java.net.URISyntaxException:索引 236 处的查询中存在非法字符:http://gateway-service/deal-solution-service/api/deals?categories=false&phonesAndSim=false<eAndSim=false&tabletsAndLaptops=false&connectivity=false&priceMin=0&priceMax=50000&duration36=false&duration24 =假&品牌=华为Spa
本来就是要照顾这个“华为温泉”的
我的服务Impl
@Override
public Collection<String> getDistinctBrands(Filter1 productFilter) {
List<String> distinctBrands = new ArrayList<>();
if (productFilter != null && productFilter.getBrand() != null && !productFilter.getBrand().isEmpty()) {
String encodedBrand = URLEncoder.encode(productFilter.getBrand(), StandardCharsets.UTF_8);
distinctBrands.addAll(Arrays.asList(encodedBrand.split("[^a-zA-Z0-9\\-_']+")));
} else {
distinctBrands = dealRepository.findDistinctBrands();
}
return distinctBrands;
}
我的控制器
@Override
public ResponseEntity<List<String>> getDistinctBrands(Filter1 filter) {
Collection<String> distinctBrands = dealService.getDistinctBrands(filter);
if (!distinctBrands.isEmpty()) {
List<String> decodedBrands = new ArrayList<>();
for (String brand : distinctBrands) {
decodedBrands.add(URLDecoder.decode(brand, StandardCharsets.UTF_8));
}
return ResponseEntity.ok(decodedBrands);
} else {
return ResponseEntity.notFound().build();
}
}
我在使用过滤器时遇到问题(按品牌名称和空格进行过滤),然后我添加了一个 URL 编码器,它开始在本地和临时环境中工作,但在实时环境中仍然无法工作。
如果我的品牌名称包含空格,我想对我的 URL 进行编码以填充空格。