Tag: resttemplate

如何在关机时等待RestTemplate响应?

我正在使用带RestTemplate将POST请求发送到网络服务器。 当我的应用程序关闭时(例如从tomcat取消部署),应该延迟关闭,直到收到所有挂起的响应(在超时内)。 restTemplate在引擎盖下使用HttpComponentsClientHttpRequestFactory 。 问题:如何告诉spring延迟关机? @PreDestroy可能是一种可能,但我如何检测restTemplate上的待处理请求?

jackson杰森将一个对象反序列化为一个列表

我正在使用Spring的RestTemplate和使用Jackson反序列化来使用Web服务。 在我来自服务器的JSON响应中,其中一个字段可以是对象或列表。 意思是它可以是”result”: [{}]或”result”: {} 。 有没有办法通过对我反序列化的类型的注释来处理这类事情? 将成员定义为array[]或List并在第二个示例的情况下插入单个对象? 我可以编写一个新的HttpMessageConverter来处理它吗?

如何使用Spring RestTemplate调用HTTPS restful Web服务

我正在使用Tomcat7,Spring框架进行ReST Web服务。 我试图使用Spring RestTemplate调用https Web服务。 我收到以下错误: 无法找到所请求目标的有效证书路径; 嵌套exception是javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到所请求目标的有效证书路径 我在stackoverflow上在线检查。 我尝试了url中的示例代码: 使用Spring RestTemplate访问Https Rest服务 我无法让它发挥作用。 任何人都可以根据下面的代码告诉我,我需要更改什么? 也有人可以告诉我或者提供我需要的java库的pom.xml文件吗? import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.client.RestTemplate; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.journaldev.spring.controller.EmpRestURIConstants; import com.journaldev.spring.model.CostControlPost; import com.journaldev.spring.model.Employee; import com.journaldev.spring.model.RfxForUpdate; import static org.junit.Assert.*; import org.apache.commons.codec.binary.Base64; import javax.net.ssl.*; import java.io.*; import java.security.KeyStore; import java.security.MessageDigest; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class TestExample2 { […]

如何使用AsyncRestTemplate同时进行多个调用?

我不明白如何有效地使用AsyncRestTemplate进行外部服务调用。 对于以下代码: class Foo { public void doStuff() { Future<ResponseEntity> future1 = asyncRestTemplate.getForEntity( url1, String.class); String response1 = future1.get(); Future<ResponseEntity> future2 = asyncRestTemplate.getForEntity( url2, String.class); String response2 = future2.get(); Future<ResponseEntity> future3 = asyncRestTemplate.getForEntity( url3, String.class); String response3 = future3.get(); } } 理想情况下,我希望同时执行所有3个调用,并在完成所有操作后处理结果。 但是,在调用get()之前不会获取每个外部服务调用,但get()阻止get() 。 那么这不是打败了AsyncRestTemplate的目的吗? 我不妨使用RestTemplate 。 所以我不明白我怎么能让他们同时执行?

RestTemplate uriVariables未展开

我尝试使用弹簧RestTemplate.getForObject()访问rest端点,但我的uri变量未展开,并作为参数附加到url。 这是我到目前为止所得到的: Map uriParams = new HashMap(); uriParams.put(“method”, “login”); uriParams.put(“input_type”, DATA_TYPE); uriParams.put(“response_type”, DATA_TYPE); uriParams.put(“rest_data”, rest_data.toString()); String responseString = template.getForObject(endpointUrl, String.class, uriParams); endpointUrl变量的值是http://127.0.0.1/service/v4_1/rest.php ,它的确是什么叫它,但我希望http://127.0.0.1/service/v4_1/rest.php?method=login&input_type…被调用。 任何提示都表示赞赏。 我正在使用Spring 3.1.4.RELEASE 问候。

resttemplate getForObject map responsetype

更新02/05/2018(大约4年后)…我再次对此进行了测试,因为人们一直在讨论我的问题/答案,Sotirios Delimanolis是正确的,我不应该在我的答案中编写代码来完成这项工作。 我使用了基本相同的RestTemplate / REST服务设置,如我的问题所示,REST服务具有已确认的响应内容类型application / json,RestTemplate能够处理响应而没有问题进入Map。 我正在调用一个返回JSON的rest服务,如下所示: { “some.key” : “some value”, “another.key” : “another value” } 我想我可以使用java.util.Map作为响应类型来调用此服务,但这对我不起作用。 我得到这个例外: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.Map] 我应该只指定String作为响应类型并将JSON转换为Map吗? 编辑我 这是我的restTemplate调用: private Map getBuildInfo(String buildUrl) { return restTemplate.getForObject(buildUrl, Map.class); } 这是我如何设置restTemplate: @PostConstruct public void initialize() { List interceptors = […]

如何从RestTemplate调用URL中提取HTTP状态代码?

我正在使用RestTemplate对我们的服务进行HTTP调用,该服务返回一个简单的JSON响应。 我根本不需要解析那个JSON。 我只需要返回我从该服务中获得的任何内容。 所以我将它映射到String.class并将实际的JSON response作为字符串返回。 RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject(url, String.class); return response; 现在的问题是 – 我试图在点击URL后提取HTTP Status codes 。 如何从上面的代码中提取HTTP状态代码? 我是否需要以当前的方式对其进行任何更改? 更新: – 这是我尝试过的,我能够得到回复和状态代码。 但是我总是需要像下面那样设置HttpHeaders和Entity对象吗? RestTemplate restTemplate = new RestTemplate(); //and do I need this JSON media type for my use case? HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); //set my entity […]

使用桌面应用程序中的RestTemplate消费服务时出现问题

我在桌面应用程序中使用RestTemplate消费rest服务时遇到问题,而在Web应用程序中使用时不会出现问题。 这是调试日志 15:30:40.448 [main] DEBUG osweb.client.RestTemplate – Reading [java.util.List] as “application/json” using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@98adae2] 15:30:40.452 [main] DEBUG httpclient.wire.content – << "[{"name":"Indonesia","id":1},{"name":"AlaySia","id":2},{"name":"Autraliya","id":3}]" 线程“main”中的exceptionjava.lang.ClassCastException:java.util.LinkedHashMap无法强制转换为com.mgm.domain.Country 这是我使用的代码。 String url = “http://localhost:8080/mgm/country”; List mediaTypes = new ArrayList(); mediaTypes.add(MediaType.APPLICATION_JSON); HttpHeaders headers = new HttpHeaders(); headers.setAccept(mediaTypes); HttpEntity httpEntity = new HttpEntity(null, headers); try { ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, List.class); List countries […]

请解释RestTemplate

我上课了 public class Client extends RestTemplate // org.springframework.web.client.RestTemplate RestTemplate用于什么?

使用RestTemplate的spring webservices的超时配置

我想在客户端配置使用RestTemplate的spring webservices的超时。 我尝试了以下配置: 但是当我启动tomcat时,我有一个NoClassDefFoundError: 06 févr. 2012 10:43:43,113 [ERROR,ContextLoader] Context initialization failed java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpMethodBase 但是我在我的pom.xml中包含了commons-httpclient: commons-httpclient commons-httpclient 3.1 </dependency 知道如何做/解决这个问题吗? 提前致谢 !