resttemplate

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

╄→尐↘猪︶ㄣ 提交于 2019-12-19 05:12:33
问题 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

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

主宰稳场 提交于 2019-12-18 17:56:13
问题 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

Spring MVC - RestTemplate launch exception when http 404 happens

落爺英雄遲暮 提交于 2019-12-18 12:46:09
问题 I have a rest service which send an 404 error when the resources is not found. Here the source of my controller and the exception which send Http 404. @Controller @RequestMapping("/site") public class SiteController { @Autowired private IStoreManager storeManager; @RequestMapping(value = "/stores/{pkStore}", method = RequestMethod.GET, produces = "application/json") @ResponseBody public StoreDto getStoreByPk(@PathVariable long pkStore) { Store s = storeManager.getStore(pkStore); if (null == s

How to cancel AsyncRestTemplate HTTP request if they are taking too much time?

纵饮孤独 提交于 2019-12-18 12:27:52
问题 Since starting, I was always confuse of how to deal with InterruptedException and how to properly cancel the http request if they are taking too much time. I have a library in which I have provided two methods, sync and async for our customer. They can call whichever method they feel is right for their purpose. executeSync() - waits until I have a result, returns the result. executeAsync() - returns a Future immediately which can be processed after other things are done, if needed. They will

How to set RequestConfiguration per request using RestTemplate?

∥☆過路亽.° 提交于 2019-12-18 07:36:19
问题 I have a library which is being used by customer and they are passing DataRequest object which has userid , timeout and some other fields in it. Now I use this DataRequest object to make a URL and then I make an HTTP call using RestTemplate and my service returns back a JSON response which I use it to make a DataResponse object and return this DataResponse object back to them. Below is my DataClient class used by customer by passing DataRequest object to it. I am using timeout value passed by

Spring Boot multipart content type HTTP Request using RestTemplate

ぐ巨炮叔叔 提交于 2019-12-18 07:07:30
问题 I am trying to emulate this request using RestTemplate in Spring Boot curl -X POST 'https://my.craftar.net/api/v0/image/?api_key=123456789abcdefghijk123456789abcdefghijk' -F "item=/api/v0/item/4fe672886ec142f6ab6d72d54acf046f/" -F "file=@back_cover.png" Here's my code: MultiValueMap<String, Object> params= new LinkedMultiValueMap<>(); params.add("item", "/api/v0/item/4fe672886ec142f6ab6d72d54acf046f/"); final String filename=file.getOriginalFilename(); Resource contentsAsResource = new

How do I retrieve HTTP status code and response body when an RestClientException is thrown?

左心房为你撑大大i 提交于 2019-12-18 04:29:20
问题 The methods of RestTemplate such as postForEntity() throw RestClientException . I would like to extract the HTTP status code and response body from that exception object in the catch block. How can I do that? 回答1: Instead of catching RestClientException , catch the special HttpClientErrorException . Here's an example: try { Link dataCenterLink = serviceInstance.getLink("dataCenter"); String dataCenterUrl = dataCenterLink.getHref(); DataCenterResource dataCenter = restTemplate.getForObject

Force Spring RestTemplate to use XmlConverter

主宰稳场 提交于 2019-12-18 02:41:44
问题 We are integrating with a third party that is sending xml with the content-type header as text/html. We were planning on using Spring's RestTemplate to map it to classes we've generated from xsds, but the RestTemplate fails to find an appropriate converter to use for the content. The third party refuses to fix the content-type because it might break other partner's integration. Is there a way with Spring's RestTemplate to force it to use a specific converter? We are basically just doing the

RestTemplate PATCH request

泄露秘密 提交于 2019-12-17 23:17:18
问题 I have the following definition for PersonDTO: public class PersonDTO { private String id private String firstName; private String lastName; private String maritalStatus; } Here is a sample record : { "id": 1, "firstName": "John", "lastName": "Doe", "maritalStatus": "married" } Now, John Doe gets divorced. So I need to send a PATCH request to this URL: http://localhost:8080/people/1 With the following request body: { "maritalStatus": "divorced" } I cannot figure out how to do it. Here is what

How can I interrupt RestTemplate call as soon as my thread is interrupted?

有些话、适合烂在心里 提交于 2019-12-17 20:36:15
问题 I need to make a library in which I will have synchronous and asynchronous feature. executeSynchronous() - waits until I have a result, returns the result. executeAsynchronous() - returns a Future immediately which can be processed after other things are done, if needed. Core Logic of my Library The customer will use our library and they will call it by passing DataKey builder object. We will then construct a URL by using that DataKey object and make a HTTP client call to that URL by