如何在 Spring 中使用 RestTemplate 获取数据
How to fetch data using RestTemplate in Spring
我的任务是编写代码以通过 URL 从剩余 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,我哪里错了?
首先,这个问题中没有关于 Spring Integration 的内容。以后请注意标签选择。
你在这里的错误是你错过了你使用 POST
HTTP 方法和 curl 的部分。但是对于 RestRemplate
你选择 GET
:
/**
* Retrieve an entity by doing a GET on the specified URL.
* The response is converted and stored in an {@link ResponseEntity}.
* <p>URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param responseType the type of the return value
* @param uriVariables the variables to expand the template
* @return the entity
* @since 3.0.2
*/
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException;
我猜你是这个意思:
/**
* Create a new resource by POSTing the given object to the URI template,
* and returns the response as {@link ResponseEntity}.
* <p>URI Template variables are expanded using the given URI variables, if any.
* <p>The {@code request} parameter can be a {@link HttpEntity} in order to
* add additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed (may be {@code null})
* @param uriVariables the variables to expand the template
* @return the converted object
* @since 3.0.2
* @see HttpEntity
*/
<T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables)
throws RestClientException;
请商议HTTP response codes, explained,你会看到500是“我操蛋”的意思,这是服务器的观点。这是服务器错误,不是你的。
当然,服务器错误可能是由于输入格式错误造成的,例如您发送的完全错误URL /person/query -d '{'name': 'name'}'
.
我的任务是编写代码以通过 URL 从剩余 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,我哪里错了?
首先,这个问题中没有关于 Spring Integration 的内容。以后请注意标签选择。
你在这里的错误是你错过了你使用 POST
HTTP 方法和 curl 的部分。但是对于 RestRemplate
你选择 GET
:
/**
* Retrieve an entity by doing a GET on the specified URL.
* The response is converted and stored in an {@link ResponseEntity}.
* <p>URI Template variables are expanded using the given URI variables, if any.
* @param url the URL
* @param responseType the type of the return value
* @param uriVariables the variables to expand the template
* @return the entity
* @since 3.0.2
*/
<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException;
我猜你是这个意思:
/**
* Create a new resource by POSTing the given object to the URI template,
* and returns the response as {@link ResponseEntity}.
* <p>URI Template variables are expanded using the given URI variables, if any.
* <p>The {@code request} parameter can be a {@link HttpEntity} in order to
* add additional HTTP headers to the request.
* @param url the URL
* @param request the Object to be POSTed (may be {@code null})
* @param uriVariables the variables to expand the template
* @return the converted object
* @since 3.0.2
* @see HttpEntity
*/
<T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables)
throws RestClientException;
请商议HTTP response codes, explained,你会看到500是“我操蛋”的意思,这是服务器的观点。这是服务器错误,不是你的。
当然,服务器错误可能是由于输入格式错误造成的,例如您发送的完全错误URL /person/query -d '{'name': 'name'}'
.