将RequestParam数据传递给Spring RestController
我正在使用将RequestParam数据传递给Spring RestController,spring,rest,spring-boot,Spring,Rest,Spring Boot,我正在使用restemplate.exchange方法从另一个服务调用一个服务。我的请求url不接受请求参数 url的格式为http://localhost:8035/sgapp/student/fetchDeptNo?sid 在控制器中,它被写为 @RestController @RequestMapping(value="/sgapp/student") public class SiController{ @GetMapping(value = "/fetchDeptNo",co
restemplate.exchange
方法从另一个服务调用一个服务。我的请求url不接受请求参数
url的格式为http://localhost:8035/sgapp/student/fetchDeptNo?sid
在控制器中,它被写为
@RestController
@RequestMapping(value="/sgapp/student")
public class SiController{
@GetMapping(value = "/fetchDeptNo",consumes = "application/x-www-form-urlencoded")
public ResponseEntity<String> getSid(@RequestParam(name = "sid")String sid){
String sid = myRepo.getSidCode(sid);
System.out.println("sid received from DB::"+sid);
return new ResponseEntity<String>(sid,HttpStatus.OK);
}
}
有人能提供合适的解决方案吗?使用特定的
交换方法将变量替换到路径中
如果要传递查询参数,请使用UriComponentsBuilder
创建带有查询参数的URL:
UriComponentsBuilder builder =
UriComponentsBuilder
.fromHttpUrl(rootUri)
.queryParam("sid", sidData);
然后可以按如下方式使用RestTemplate:
restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET,
entity,
String.class);
该特定的exchange
方法用于将变量替换到路径中
如果要传递查询参数,请使用UriComponentsBuilder
创建带有查询参数的URL:
UriComponentsBuilder builder =
UriComponentsBuilder
.fromHttpUrl(rootUri)
.queryParam("sid", sidData);
然后可以按如下方式使用RestTemplate:
restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET,
entity,
String.class);