我正在尝试实现包含请求主体的GET请求调用。是的,我知道。所以这是我的问题。
我有一个Spring MVC控制器对以下伪命令响应很好:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:9098/a/{aID}/b/{bID}/c -d '{"header":{"foo":"foofoofoo","timestamp":"2015-06-23T03:45:43-04:00"}}'
我正在尝试使用 RestTemplate.exchange 从另一个spring mvc应用程序重现此调用,但在客户端应用程序上不断收到400错误请求错误 - 服务器端应用程序没有'似乎记录了有关通话的任何错误。
客户端控制器
@GET
@Produces(APPLICATION_JSON)
@Path("/a/{aID}/b/{bID}/c")
public String getC(
@PathParam(value = "aID") String aId,
@PathParam(value = "bID") String bId) {
ResponseEntity<String> result = null;
RestTemplate restTemplate = new RestTemplate();
StringBuilder url = new StringBuilder(serverApplicationUrlLocalhost9098);
url.append("/a/").append(aId);
url.append("/b/").append(bId);
url.append("/c");
String finalUrl = url.toString();
try {
org.springframework.http.MediaType mediaType = org.springframework.http.MediaType.APPLICATION_JSON;
List<org.springframework.http.MediaType> accepts = new ArrayList<org.springframework.http.MediaType>();
accepts.add(org.springframework.http.MediaType.APPLICATION_JSON);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(accepts);
headers.setContentType(mediaType);
Foo foo = new Foo();
HttpEntity<Foo> request = new HttpEntity<Foo>(foo, headers);
result = restTemplate.exchange(finalUrl, HttpMethod.GET, request, String.class);
} catch (Exception e) {
logger.error("Error calling REST service: " + finalUrl, e);
}
return result.getBody();
}
为了测试,我有一个私有类Foo
private class Header {
private String foo = "foofoofoo";
private String timestamp = "2015-06-23T03:45:43-04:00";
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
private class Foo {
private Header header = new Header();
public Header getHeader() {
return header;
}
public void setHeader(Header header) {
this.header = header;
}
}