简体   繁体   English

无法使用RESTTemplate发送GET请求

[英]Cannot send a GET request with RESTTemplate

I need to send a request as following 我需要发送以下请求

 http://server.com/server1/index.php?id=123&name=jack&date=12.1.2016

I am using following code but it seems like it does not send a correct request as response object is empty. 我正在使用以下代码,但由于响应对象为空,因此它似乎未发送正确的请求。 I am also wondering how to show the complete url that restTemplate is sending? 我也想知道如何显示restTemplate发送的完整URL? I know WireShark can be used but is there any way to retrieve it using restTemplate? 我知道可以使用WireShark,但是有什么方法可以使用restTemplate检索它吗?

Code

String url = "http://server.com/server1/index.php?"
Map<String,String> vars = new HashMap<String,String>();
vars.put("id","123");
vars.put("name","jack");
vars.put("date","12.1.2016");

Profile profile = restTemplate.getForObject(url,Profile.class,vars);

You can set the Log Level for org.springframework.web.client.RestTemplate to debug then you'll get an output similar to this: 您可以将org.springframework.web.client.RestTemplate的日志级别设置为调试,然后将获得类似于以下的输出:

12:11:22.072 [main] DEBUG osweb.client.RestTemplate - Created GET request for " http://echo.jsontest.com/key/value/one/two " 12:11:22.072 [main]调试osweb.client.RestTemplate-为“ http://echo.jsontest.com/key/value/one/two ”创建GET请求
12:11:22.110 [main] DEBUG osweb.client.RestTemplate - Setting request Accept header to [application/json, application/*+json] 12:11:22.110 [main]调试osweb.client.RestTemplate-将请求接受标头设置为[application / json,application / * + json]
12:11:24.492 [main] DEBUG osweb.client.RestTemplate - GET request for " http://echo.jsontest.com/key/value/one/two " resulted in 200 (OK) 12:11:24.492 [main]调试osweb.client.RestTemplate-GET请求“ http://echo.jsontest.com/key/value/one/two ”导致200(确定)
12:11:24.494 [main] DEBUG osweb.client.RestTemplate - Reading [class com.example.TestOptional$Echo] as "application/json;charset=ISO-8859-1" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@16f7c8c1] 12:11:24.494 [main]调试osweb.client.RestTemplate-使用[org.springframework.http.converter]将[com.example.TestOptional $ Echo类]读取为“ application / json; charset = ISO-8859-1”。 json.MappingJackson2HttpMessageConverter@16f7c8c1]

There you can see the Request the URL and the returned HTTP Status code. 在此处,您可以看到“请求URL”和返回的HTTP状态代码。

If you are using Logback xml configuration you would write 如果您使用的是Logback xml配置,则应编写

<logger name="org.springframework.web.client.RestTemplate" level="DEBUG" />

There are no template variables in this URL template: 此URL模板中没有模板变量:

String url = "http://server.com/server1/index.php?";

I think you need to specify them explicitly like this: 我认为您需要像这样明确指定它们:

String url = "http://server.com/server1/index.php?id={id}&name={name}&date={date}";

In this template, id is the name of the query string parameter, and {id} refers to a key in your vars map. 在此模板中, id是查询字符串参数的名称,而{id}vars映射中的键。

In addition to switching on logging as andih describes, you can instantiate the URL template with a given set of variables as follows: 除了按照andih所述打开日志记录之外,还可以使用给定的变量集实例化URL模板,如下所示:

new UriTemplate(url).expand(vars).toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM