У меня есть два Microservices, скажем, Microservice1 выполняет вызов Microservice2 с помощью RestTemplate.
Вот метод обработки на Microservice2.
@GetMapping("/careerRoles")
public ResponseEntity<Map<String, Object>> getRole(@RequestParam(value =
"roleName", required = false) String roleName,
@RequestParam(value = "domain", required = false) String domain,
@RequestParam("proficiencyLevel") String proficiencyLevel,
@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "limit", required = false) Integer limit) {
Map<String, Object> query = new HashMap<>();
Map<String, Object> json = new HashMap<>();
if (page == null && limit == null) {
page = 1;
limit = 10;
}
query.put("page", page);
query.put("limit", limit);
List<Role> roles = new ArrayList<>();
try {
if (roleName == null) {
query.put("proficiencyLevel", proficiencyLevel);
roles = this.roleService.getRolesByProficiencyLevel(proficiencyLevel, page, limit);
} else {
query.put("proficiencyLevel", proficiencyLevel);
query.put("roles", roleName);
roles = this.roleService.getRolesByNameAndProficiencyLevel(roleName, proficiencyLevel, page, limit);
}
json.put("error", null);
json.put("query", query);
json.put("result", roles);
return new ResponseEntity<Map<String, Object>>(json, HttpStatus.OK);
} catch (RoleNotFoundException e) {
json.put("error", e.getMessage());
json.put("query", query);
json.put("result", roles);
return new ResponseEntity<Map<String, Object>>(json, HttpStatus.NOT_FOUND);
}
}
Код вызова в Microservice1 такой:
@GetMapping("careerRole")
public ResponseEntity<Map<String,Object>> getCareerRole(
@RequestParam(value = "roleName", required = false) String roleName,
@RequestParam(value = "domain", required = false) String domain,
@RequestParam("proficiencyLevel") String proficiencyLevel,
@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "limit", required = false) Integer limit) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>("Hello World!",
headers);
UriComponentsBuilder builder = UriComponentsBuilder
.fromUriString(uri)
// Add query parameter
.queryParam("roleName",roleName)
.queryParam("proficiencyLevel", proficiencyLevel);
Map<String,Object> map = restTemplate.getForObject(builder.toUriString(),
Map.class);
return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
}
}
Код работает и возвращает JSON, как и ожидалось, когда я отправляю это на Microservice1, прослушивая порт 8081
http://localhost:8081/api/v1/careerRoles?proficiencyLevel=Novice
Однако, если я отправляю два параметра запроса, как этот, код завершается ошибкой.
http://localhost:8081/api/v1/careerRoles?roleName=Java Developer&proficiencyLevel=Novice
Однако, если я использую Postman для прямого доступа к Microservice 2 с обоими предыдущими GET-запросами, результаты получаются, как и ожидалось.
Что я делаю не так?Заранее спасибо за любую помощь.