【发布时间】:2017-06-30 09:08:32
【问题描述】:
我的任务是编写代码,通过这样的 URL 从 REST API 获取数据
curl -k -v -XPOST -H "username: password" -H "Content-type: application/json" http://localhost:8181/api/rest/person/query -d '
{
"name": "person's name",
"role": "role code",
"programme": "programme code"
}'
这是我的代码
public JsonUser fetchJsonUserByName(String name) throws IOException{
String jsonName = "'{'name': "+"'"+name+"'}'";
String url = "/person/query -d "+jsonName;
ResponseEntity<JsonUser> response = restTemplate.getForEntity(url, JsonUser.class);
try {
if(response.hasBody()) return response.getBody();
else return null;
} catch (HttpStatusCodeException e) {
if(e.getStatusCode() == HttpStatus.NOT_FOUND) return null;
throw new IOException("error fetching user", e);
} catch (Exception e) {
throw new IOException("error fetching user", e);
}
}
当我运行我的代码时,我得到了错误代码 500,我哪里错了?
【问题讨论】:
标签: json resttemplate spring-web