标签: resttemplate

Spring RestTemplate - 如何启用请求/响应的完整调试/记录?

我一直在使用Spring RestTemplate一段时间,当我试图调试它的请求和响应时,我一直碰壁.我基本上希望看到当我使用curl并打开"详细"选项时看到的相同内容.例如 :

curl -v http://twitter.com/statuses/public_timeline.rss
Run Code Online (Sandbox Code Playgroud)

将显示发送的数据和接收的数据(包括标题,cookie等).

我检查了一些相关的帖子,如: 我如何在Spring RestTemplate中记录响应? 但我没有设法解决这个问题.

实现此目的的一种方法是实际更改RestTemplate源代码并在那里添加一些额外的日志记录语句,但我会发现这种方法确实是最后的手段.应该有一些方法告诉Spring Web Client/RestTemplate以更友好的方式记录所有内容.

我的目标是能够使用以下代码执行此操作:

restTemplate.put("http://someurl", objectToPut, urlPathValues);
Run Code Online (Sandbox Code Playgroud)

然后在日志文件或控制台中获取相同类型的调试信息(我使用curl).我相信这对使用Spring RestTemplate且有问题的任何人都非常有用.使用curl调试RestTemplate问题不起作用(在某些情况下).

java debugging logging resttemplate

198
推荐指数
17
解决办法
20万
查看次数

使用Spring RestTemplate获取JSON对象的列表

我有两个问题:

  • 如何使用Spring RestTemplate映射JSON对象列表.
  • 如何映射嵌套的JSON对象.

我正在尝试使用https://bitpay.com/api/rates,遵循http://spring.io/guides/gs/consuming-rest/中的教程.

java spring resttemplate

186
推荐指数
8
解决办法
31万
查看次数

如何在Spring RestTemplate请求上设置"Accept:"标头?

我想设置Accept:我使用Spring的请求中的值RestTemplate.

这是我的Spring请求处理代码

@RequestMapping(
    value= "/uom_matrix_save_or_edit", 
    method = RequestMethod.POST,
    produces="application/json"
)
public @ResponseBody ModelMap uomMatrixSaveOrEdit(
    ModelMap model,
    @RequestParam("parentId") String parentId
){
    model.addAttribute("attributeValues",parentId);
    return model;
}
Run Code Online (Sandbox Code Playgroud)

这是我的Java REST客户端:

public void post(){
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.add("parentId", "parentId");
    String result = rest.postForObject( url, params, String.class) ;
    System.out.println(result);
}
Run Code Online (Sandbox Code Playgroud)

这适合我; 我从服务器端获得了一个JSON字符串.

我的问题是:当我使用RestTemplate时,如何指定Accept:标题(例如application/json,application/xml...)和请求方法(例如,...)?GETPOST

rest spring resttemplate

173
推荐指数
6
解决办法
30万
查看次数

如何使用Spring RestTemplate发布表单数据?

我想将以下(工作)curl片段转换为RestTemplate调用:

curl -i -X POST -d "email=first.last@example.com" https://app.example.com/hr/email
Run Code Online (Sandbox Code Playgroud)

如何正确传递电子邮件参数?以下代码导致404 Not Found响应:

String url = "https://app.example.com/hr/email";

Map<String, String> params = new HashMap<String, String>();
params.put("email", "first.last@example.com");

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity( url, params, String.class );
Run Code Online (Sandbox Code Playgroud)

我试图在PostMan中制定正确的调用,我可以通过将body参数指定为正文中的"form-data"参数来使其正常工作.在RestTemplate中实现此功能的正确方法是什么?

java rest spring resttemplate

124
推荐指数
3
解决办法
25万
查看次数

通过JSON中的RestTemplate发出POST请求

我没有找到任何解决问题的例子,所以我想请你帮忙.我不能简单地使用JSON中的RestTemplate对象发送POST请求

每次我得到:

org.springframework.web.client.HttpClientErrorException:415不支持的媒体类型

我以这种方式使用RestTemplate:

...
restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
list.add(new MappingJacksonHttpMessageConverter());
restTemplate.setMessageConverters(list);
...
Payment payment= new Payment("Aa4bhs");
Payment res = restTemplate.postForObject("http://localhost:8080/aurest/rest/payment", payment, Payment.class);
Run Code Online (Sandbox Code Playgroud)

我的错是什么?

java rest spring json resttemplate

117
推荐指数
8
解决办法
31万
查看次数

Spring RestTemplate超时

我想为我的Web应用程序使用的休息服务设置连接超时.我正在使用Spring的RestTemplate与我的服务交谈.我做了一些研究,我发现并使用了下面的xml(在我的应用程序xml中),我认为这是为了设置超时.我正在使用Spring 3.0.

我也看到了同样的问题这里使用RestTemplate的spring webservices的超时配置,但解决方案看起来不干净,我更喜欢通过Spring配置设置超时值

<bean id="RestOperations" class="org.springframework.web.client.RestTemplate">
    <constructor-arg>

      <bean class="org.springframework.http.client.CommonsClientHttpRequestFactory">
        <property name="readTimeout" value="${restURL.connectionTimeout}" />
      </bean>
    </constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)

似乎无论我设置readTimeout是什么,我得到以下内容:

网络电缆断开连接: 等待大约20秒并报告以下异常:

org.springframework.web.client.ResourceAccessExcep tion:I/O错误:无主机路由:connect; 嵌套异常是java.net.NoRouteToHostException:没有到host的路由:connect

网址不正确,因此休息服务返回404: 等待大约10秒并报告以下异常:

org.springframework.web.client.HttpClientErrorException:404 Not Found

我的要求需要更短的超时时间,所以我需要能够更改这些.关于我做错了什么的任何想法?

非常感谢.

rest spring timeout resttemplate

104
推荐指数
7
解决办法
17万
查看次数

Spring Resttemplate异常处理

以下是代码段; 基本上,当错误代码不是200时,我试图传播异常.

ResponseEntity<Object> response = restTemplate.exchange(url.toString().replace("{version}", version),
                    HttpMethod.POST, entity, Object.class);
            if(response.getStatusCode().value()!= 200){
                logger.debug("Encountered Error while Calling API");
                throw new ApplicationException();
            }
Run Code Online (Sandbox Code Playgroud)

但是,如果来自服务器的500响应,我将获得异常

org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
Run Code Online (Sandbox Code Playgroud)

我真的需要在try中包装其余的模板交换方法吗?那么代码的目的是什么?

rest spring exception-handling resttemplate

88
推荐指数
8
解决办法
13万
查看次数

无法自动装配字段:Spring启动应用程序中的RestTemplate

在启动期间运行spring boot应用程序时,我遇到异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.web.client.RestTemplate com.micro.test.controller.TestController.restTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.client.RestTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Run Code Online (Sandbox Code Playgroud)

我在TestController中自动装配RestTemplate.我正在使用Maven进行依赖管理.

TestMicroServiceApplication.java

package com.micro.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestMicroServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestMicroServiceApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

TestController.java

    package com.micro.test.controller; …
Run Code Online (Sandbox Code Playgroud)

spring maven resttemplate spring-boot

86
推荐指数
5
解决办法
8万
查看次数

使用restTemplate发送带有身份验证标头的GET请求

我需要通过使用RestTemplate发送带有一些Authorization标头的GET请求来从我的服务器检索资源.

在浏览了文档之后,我注意到没有一个GET方法接受头作为参数,并且发送Headers(如accept和Authorization)的唯一方法是使用exchange方法.

由于这是一个非常基本的行动,我想知道我是否遗漏了一些东西,还有另一种更简单的方法吗?

java spring spring-mvc resttemplate

71
推荐指数
5
解决办法
15万
查看次数

使用spring restTemplate进行REST API的基本身份验证

我在RestTemplate中是全新的,基本上也在REST API中.我想通过Jira REST API在我的应用程序中检索一些数据,但是要取回401 Unauthorized.找到并发表关于jira rest api文档的文章,但实际上并不知道如何将其重写为java,因为该示例使用curl命令行方式.我将不胜感激任何建议或建议如何重写:

curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" "http://kelpie9:8081/rest/api/2/issue/QA-31"
Run Code Online (Sandbox Code Playgroud)

使用spring rest模板进入java.其中ZnJlZDpmcmVk是base64编码的username:password字符串.非常感谢你.

java spring resttemplate jira-rest-api

67
推荐指数
7
解决办法
12万
查看次数