resttemplate

Could not write request: no suitable HttpMessageConverter found for request type [org.json.JSONObject] and content type [application/json]

安稳与你 提交于 2019-12-01 02:45:45
I'm digging myself in trying to send a POST request with a JSON payload to a remote server. This GET curl command works fine: curl -H "Accept:application/json" --user aaa@aaa.com:aaa "http://www.aaa.com:8080/aaa-project-rest/api/users/1" -i And this POST one works fine too: curl -H "Accept:application/json" -H "Content-Type: application/json" "http://www.aaa.com:8080/aaa-project-rest/api/users/login" -X POST -d "{ \"email\" : \"aaa@aaa.com\", \"password\" : \"aaa\" }" -i And so I'm trying to mimic it in my Android app. The app works fine on the first GET request but gives a 400 Bad Request on

实现自己的DiscoveryClient

隐身守侯 提交于 2019-11-30 21:09:09
需要做的:   DiscoveryClient能提供那些服务的服务名列表   返回指定服务对于的ServiceInstance列表   返回DiscoveryClient的顺序   返回HealthIndicator里显示的描述 实现LoadBalanceClient   实现自己的ServiceList<T extends Server>     Ribbon提供了AbstractServerList<T extends Server>   提供一个配置类,声明ServerListBean 实例 pom引入 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type>

SpringCloud中使用Swagger API

拟墨画扇 提交于 2019-11-30 21:00:39
一.Swagger介绍 百度百科:Swagger的目标是为REST APIs 定义一个标准的,与语言无关的接口,使人和计算机在看不到源码或者看不到文档或者不能通过网络流量检测的情况下能发现和理解各种服务的功能。当服务通过Swagger定义,消费者就能与远程的服务互动通过少量的实现逻辑。类似于低级编程接口,Swagger去掉了调用服务时的很多猜测。 我觉得把Swagger当成一种开发工具就好,省去了手写文档的步骤,生成文档并能测试,注解的方式让其他人看代码也更加清楚方便。 想了解更多有关资料点击: Swagger官网 浏览 Swagger-Spec 去了解更多关于Swagger 项目的信息,包括附加的支持其他语言的库。 二.搭建 <!--使用Swagger2构建RESTful API文档--> < dependency >   < groupId > io.springfox </ groupId >   < artifactId > springfox-swagger2 </ artifactId >    < version > 2.2.2 </ version > </ dependency > < dependency >    < groupId > io.springfox </ groupId >    < artifactId > springfox-swagger

RestTemplate的基本使用

喜你入骨 提交于 2019-11-30 19:53:26
getForObject 和 getForEntity 的区别 getForObject函数实际上是对getForEntity函数的进一步封装,如果你只关注返回的消息体的内容,对其他信息都不关注,此时可以使用getForObject。 ResponseEntity<T> 是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。如果需要提取其中的部分属性的话直接从ResponseEntity中调用相应的方法即可。(具体方法见下图) 注意:使用restTemplate发送请求的时候,url地址的开头必须是http:// ....... 一定不能是 https:// ........... 1、RestTemplate发送 get 请求携带HttpHeaders信息、参数 1 HttpHeaders httpHeaders= new HttpHeaders(); 2 httpHeaders.add("Host", "XXXX.XXXX.XXXX.XX"); 3 //这里如果需要传递参数的话,将参数封装在map集合中,写到下面式子中null的位置处 4 HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);/

Make Http call using ReactiveX for Java

风流意气都作罢 提交于 2019-11-30 19:39:30
I am new to ReactiveX for Java and I've the following code block that make external http call but it is not async. We are using rxjava 1.2, and Java 1.8 private ResponseEntity<String> callExternalUrl(String url, String json, HttpMethod method) { RestTemplate restTemplate; HttpEntity request; request = new HttpEntity(jsonContent, httpHeaders); return restTemplate.exchange(url, httpMethod, request, String.class); } I've the following code block I found online but I couldn't totally understand it and how I can apply it to my code base. private RxClient<RxObservableInvoker> httpClient; public <T>

Uploading a List of MultipartFile with Spring 4 restTemplate (Java Client & RestController)

倾然丶 夕夏残阳落幕 提交于 2019-11-30 19:32:43
问题 I am trying to post of List of MultipartFile to my RestController using spring restTemplate although I'm a bit confused as to the exact syntax & types to use for my client & controller. Here's what I have so far based on the research I've done... FileUploadClient.java public void uploadFiles(List<MultipartFile> multiPartFileList) throws IOException { MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); List<Object> files = new ArrayList<>(); for(MultipartFile file :

Setting Security cookie using RestTemplate

拈花ヽ惹草 提交于 2019-11-30 18:48:59
I am trying to call a Restful JSON service using RestTemplate and Jackson json convertor. Now in order to call the service I need to pass in a Security cookie. I can achieve this by using URLConnection (See the code below) URL url= new URL("https://XXXXXXXX"); URLConnection yc = url.openConnection(); yc.setRequestProperty("SecurityCookie", ssocookie.getValue());</code> Whats the parallel for this in RestTemplate? Here is a code snippet which I have been using to call a Restful Service using RestTemplate: RestTemplate rest = new RestTemplate(); InputBean input = new InputBean(); input

RestTemplate调用

£可爱£侵袭症+ 提交于 2019-11-30 18:00:33
一、get请求public class TestController{ @Autowired RestTemplate restTemplate; public void getlist(String a,String b) { ResponseEntity<ResultVO<List<TClNew>>> result = restTemplate.exchange("http://192.168.1.1/server/getlist/{a}/{b}", HttpMethod.GET, null, new ParameterizedTypeReference<ResultVO<List<test>>>() { }, a, b); }}二、post请求 public class TestController{ @Autowired RestTemplate restTemplate;   public void post(){   HttpHeaders headers = new HttpHeaders();   Map<String, Object> map = new HashMap<>();   map.put("aaa", "qqqq");     headers.setContentType(MediaType.MULTIPART_FORM_DATA);    

RestTemplate response.getBody throws exception on 4** and 5** errors for put and post request but works fine for get requests

别来无恙 提交于 2019-11-30 16:08:33
I am trying to intercept and log all the request-responses. To make requests i am using RestTemplate.exchange() . When i make a GET request and get an 4** error i can call the ClientHttpResponse.getBody( ) and can access the response body but for PUT and POST requests ClientHttpResponse.getBody() method throws an exception. What might be causing this and how can i get the response body for POST and PUT requests as well? This is where i make the request: apiResponse = restTemplate.exchange(url, vCloudRequest.getHttpMethod(), entity, responseType); This is the part of the interceptor that gets

RestTemplate with pem certificate

别来无恙 提交于 2019-11-30 16:06:49
问题 I have pem certificate with private key and server certificate. I can execute it using curl and all works ok. curl -O -k --cert-type pem --cert mypem.pem url But I want to use it with java, most preferably will be RestTemplate from spring. 回答1: So knowledge about using pem certificate with RestTemplate is distracted. Steps which must be done: Add server certificate to trustStore, using keytool or portecle. When you want to use custom trusttore use this script Next configure ssl to