resttemplate

Spring/RestTemplate - PUT entity to server

不问归期 提交于 2019-12-08 23:06:02
问题 Please look at this simple code: final String url = String.format("%s/api/shop", Global.webserviceUrl); RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); HttpHeaders headers = new HttpHeaders(); headers.set("X-TP-DeviceID", Global.deviceID); HttpEntity entity = new HttpEntity(headers); HttpEntity<Shop[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, Shop[].class); shops = response.getBody(); As

RestTemplate map JSON key-value pair object with with dynamic keys

扶醉桌前 提交于 2019-12-08 13:46:09
问题 I get a response of JSON key-value pair object with with dynamic keys for a HTTP request done using Java Spring RestTemplate as shown below. Response: { "1234x": { "id": "1234x", "description": "bla bla", ... }, "5678a": { "id": "5678a", "description": "bla bla bla", ... }, ... } How to map the response object to a POJO or a Map ? I am using RestTemplate as following. RestTemplate restTemplate = new RestTemplate(); String url = "my url"; HttpHeaders headers = new HttpHeaders(); HttpEntity

Spring cloud - Resttemplate doesn't get injected in interceptor

假如想象 提交于 2019-12-08 09:39:18
问题 I created a resttemplate in my spring boot application like this: @Configuration public class MyConfiguration { @LoadBalanced @Bean RestTemplate restTemplate() { return new RestTemplate(); } } This works fine in all classes when autowired. However, in my interceptor, this throws up nullpointer exception. What could be the reason and how can I configure a loadbalanced (using Ribbon) resttemplate in my interceptor? Update: my interceptor: public class MyInterceptor implements

Translate authorized curl -u post request with JSON data to RestTemplate equivalent

妖精的绣舞 提交于 2019-12-08 05:18:48
问题 I am using github api to create repositories using curl command as shown below and it works fine. curl -i -u "username:password" -d '{ "name": "TestSystem", "auto_init": true, "private": true, "gitignore_template": "nanoc" }' https://github.host.com/api/v3/orgs/Tester/repos Now I need to execute the same above url through HttpClient and I am using RestTemplate in my project. I have worked with RestTemplate before and I know how to execute simple url but not sure how to post the above JSON

alfresco file Upload Unexpected error occurred during upload of new content

让人想犯罪 __ 提交于 2019-12-08 04:11:10
问题 I want to upload a file to alfresco using the backend Webscript: alfresco/service/api/upload . Here is how I build my json: _ticket = Login.getAlfTicket(login, psswd); String url = "http://localhost:8080/alfresco/service/api/upload"; File file = new File("C:/the-file-name.txt"); byte[] bytes = Files.readAllBytes(file.toPath()); bytes = Base64.encodeBase64(bytes); JSONObject json = new JSONObject(); json.put("filedata", new String(bytes)); json.put("siteid", "google"); json.put("containerId",

How to change MediaType for MappingJacksonHttpMessageConverter in OAuth2RestTemplate

╄→尐↘猪︶ㄣ 提交于 2019-12-08 00:42:49
问题 I have an application that is using Spring Source OAuth2 as s client to retrieve user data from a resource server and create a local user. I keep getting the error when the OAuth2ClientContextFilter tries to retrieve the token: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [org.springframework.security.oauth2.common.OAuth2AccessToken] and content type [application/x-javascript;charset=utf-8] I

Halo(十三)

纵饮孤独 提交于 2019-12-07 17:54:19
Spring Boot Actuator 请求跟踪 Spring Boot Actuator 的关键特性是在应用程序里提供众多 Web 接口, 通过它们了解应用程序运行时的内部状况,且能监控和度量 Spring Boot 应用程序。 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> Spring Boot Actuator 默认会把最近100次的 HTTP 请求记录到内存中,对应的实现类是 InMemoryHttpTraceRepository。 使用 @Service public class TraceService { private final HttpTraceRepository httpTraceRepository; public TraceServiceImpl(HttpTraceRepository httpTraceRepository) { this.httpTraceRepository = httpTraceRepository; } public List<HttpTrace> listHttpTraces() { return

Spring Cloud第二篇 | 使用并认识Eureka注册中心

混江龙づ霸主 提交于 2019-12-07 17:09:27
​ 本文是Spring Cloud专栏的 第二篇 文章,了解 前一篇 文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 ​​ 一、SpringCloud快速开发入门 SpringCloud是构建在SpringBoot基础之上的 1、创键一个服务提供者(springcloud-service-provider) 1-1、创键提供者类 @RestController @RequestMapping("/provider") public class ProviderController { @RequestMapping("/hello") public String hello(){ return "spring cloud provider-01 hello world"; } } 1-2、配置服务提供者的application.yml文件 spring: application: name: springcloud-service-provider server: port: 8080 2、创键一个服务消费者(springcloud-service-consumer) 2-1、配置RestTemplate类 @Configuration public class BeanConfig { /** *

How to send JSON as a parameter in url using Spring RestTemplate?

半城伤御伤魂 提交于 2019-12-07 12:32:08
问题 I am trying to achieve same thing as this: How to use query parameter represented as JSON with Spring RestTemplate?, sending JSON string as a URL parameter in restTemplate.exchange() . The accepted answer mentions that sending JSON object as request parameter is generally not a good idea since you will probably face problem with curly brackets inside JSON. That is exactly what is happening when I am trying to make a GET call to an API. Since this is an API from another system, I cannot ask

Spring RestTemplate POST method for Xml Response?

吃可爱长大的小学妹 提交于 2019-12-07 12:03:11
问题 I've tested a POST request via REST API tool and getting the response in an XML format. I'm not sure which method need to use and what should the response type class for the above. Could someone help me with the code snippet for the same? Thanks, Kathir 回答1: You need to add xml message converter to covert the xml messages to java objects. See solution Stack Overflow Also you can capture the result in String format and manually handle it then. Example : String xmlData = restTemplate