我必须进行REST调用,其中包括自定义头和查询参数。我设置我的HttpEntity只有头(没有正文),我使用RestTemplate.exchange()方法如下:
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
Map<String, String> params = new HashMap<String, String>();
params.put("msisdn", msisdn);
params.put("email", email);
params.put("clientVersion", clientVersion);
params.put("clientType", clientType);
params.put("issuerName", issuerName);
params.put("applicationName", applicationName);
HttpEntity entity = new HttpEntity(headers);
HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params);
这在客户端失败,因为调度程序servlet无法将请求解析到处理程序。调试之后,似乎没有发送请求参数。
当我使用请求体和没有查询参数的POST做一个交换时,它工作得很好。
有人有什么想法吗?
我采取不同的方法,你可能同意或不同意,但我想从.properties文件控制,而不是编译的Java代码
内部应用程序。属性文件
endpoint。url = https://host/resource?
Java代码在这里,你可以写if或切换条件,以找出端点URL在.properties文件中是否有@PathVariable(包含{})或@RequestParam (yourURL?key=value)等…然后调用相应的方法…这样它是动态的,不需要在未来的一站式商店更改代码…
我试图在这里给出比实际代码更多的想法……试着为@RequestParam和@PathVariable等编写泛型方法…然后在需要时进行相应的调用
@Value("${endpoint.url}")
private String endpointURL;
// you can use variable args feature in Java
public String requestParamMethodNameHere(String value1, String value2) {
RestTemplate restTemplate = new RestTemplate();
restTemplate
.getMessageConverters()
.add(new MappingJackson2HttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
HttpEntity<String> entity = new HttpEntity<>(headers);
try {
String formatted_URL = MessageFormat.format(endpointURL, value1, value2);
ResponseEntity<String> response = restTemplate.exchange(
formatted_URL ,
HttpMethod.GET,
entity,
String.class);
return response.getBody();
} catch (Exception e) { e.printStackTrace(); }
String uri = http://my-rest-url.org/rest/account/{account};
Map<String, String> uriParam = new HashMap<>();
uriParam.put("account", "my_account");
UriComponents builder = UriComponentsBuilder.fromHttpUrl(uri)
.queryParam("pageSize","2")
.queryParam("page","0")
.queryParam("name","my_name").build();
HttpEntity<String> requestEntity = new HttpEntity<>(null, getHeaders());
ResponseEntity<String> strResponse = restTemplate.exchange(builder.toUriString(),HttpMethod.GET, requestEntity,
String.class,uriParam);
//final URL: http://my-rest-url.org/rest/account/my_account?pageSize=2&page=0&name=my_name
RestTemplate:使用UriComponents (URI变量和请求参数)构建动态URI
将哈希映射转换为查询参数字符串:
Map<String, String> params = new HashMap<>();
params.put("msisdn", msisdn);
params.put("email", email);
params.put("clientVersion", clientVersion);
params.put("clientType", clientType);
params.put("issuerName", issuerName);
params.put("applicationName", applicationName);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.queryParam(entry.getKey(), entry.getValue());
}
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
HttpEntity<String> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, new HttpEntity(headers), String.class);
我提供了一个RestTemplate GET方法的代码片段与路径参数示例
public ResponseEntity<String> getName(int id) {
final String url = "http://localhost:8080/springrestexample/employee/name?id={id}";
Map<String, String> params = new HashMap<String, String>();
params.put("id", id);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity request = new HttpEntity(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, String.class, params);
return response;
}
至少从Spring 3开始,许多RestTemplate方法不是使用UriComponentsBuilder来构建URL(这有点啰嗦),而是在参数路径中接受占位符(不仅仅是交换)。
从文档中可以看到:
Many of the RestTemplate methods accepts a URI template and URI
template variables, either as a String vararg, or as
Map<String,String>.
For example with a String vararg:
restTemplate.getForObject(
"http://example.com/hotels/{hotel}/rooms/{room}", String.class, "42", "21");
Or with a Map<String, String>:
Map<String, String> vars = new HashMap<>();
vars.put("hotel", "42");
vars.put("room", "21");
restTemplate.getForObject("http://example.com/hotels/{hotel}/rooms/{room}",
String.class, vars);
参考:https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html # rest-resttemplate-uri
如果你查看JavaDoc for RestTemplate并搜索“URI Template”,你可以看到哪些方法可以使用占位符。