无法使用RESTTemplate发送GET请求

时间:2015-06-13 09:56:41

标签: java spring resttemplate

我需要发送以下请求

SELECT CASE WHEN MIN(Col) <> MAX(Col) THEN 
          'Both'
       ELSE
          MIN(Col)
       END
FROM YourTable

我正在使用以下代码,但似乎它没有发送正确的请求,因为响应对象为空。我也想知道如何显示restTemplate发送的完整网址?我知道可以使用WireShark,但有没有办法使用restTemplate检索它?

代码

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

2 个答案:

答案 0 :(得分:1)

您可以将org.springframework.web.client.RestTemplate的日志级别设置为调试,然后您将获得与此类似的输出:

  

12:11:22.072 [main] DEBUG o.s.web.client.RestTemplate - 为“http://echo.jsontest.com/key/value/one/two”创建了GET请求   12:11:22.110 [main] DEBUG o.s.web.client.RestTemplate - 设置请求接受标题到[application / json,application / * + json]
  12:11:24.492 [main] DEBUG o.s.web.client.RestTemplate - GET请求“http://echo.jsontest.com/key/value/one/two”导致200(OK)
  12:11:24.494 [main] DEBUG osweb.client.RestTemplate - 使用[org.springframework.http.converter将[class com.example.TestOptional $ Echo]读作“application / json; charset = ISO-8859-1” .json.MappingJackson2HttpMessageConverter @ 16f7c8c1]

在那里,您可以看到请求URL和返回的HTTP状态代码。

如果您使用的是Logback xml配置 你会写

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

答案 1 :(得分:1)

此网址模板中没有模板变量:

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

我认为您需要明确指定它们:

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

在此模板中,id是查询字符串参数的名称,{id}是指vars地图中的关键字。

除了按照andih描述的方式打开日志记录外,您还可以使用给定的变量集来实例化URL模板,如下所示:

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