我有两个微服务,例如Microservice1使用RestTemplate调用Microservice2。
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);
}
}
当我将其发送到侦听端口8081的Microservice1时,代码可以正常工作并返回JSON
http://localhost:8081/api/v1/careerRoles?proficiencyLevel=Novice
但是,如果我发送两个这样的请求参数,则代码将失败。
http://localhost:8081/api/v1/careerRoles?roleName=Java Developer&proficiencyLevel=Novice
但是,如果我使用Postman通过前面的两个GET请求直接访问Microservice 2,结果将按预期方式发送。
我做错了什么?预先感谢您的帮助。
答案 0 :(得分:1)
我以前遇到过这个问题。
java和开发人员之间的空间可能会产生问题,因此请在继续操作之前对其进行解码。