Illegal character in scheme name at index 0: RestTemplate - spring

Making a RestTemplate project.
Invoke ResponseEntity<List> responseEntity = restTemplate.exchange(baseUrl,
HttpMethod.GET,
requestEntity,
List.class)
my baseUrl is "http://94.198.50.185:7081/api/users",
I get an error: Illegal character in scheme name at index 0: http://94.198.50.185:7081/api/users
What's this? ))
https://github.com/anatoliy19/3.1.5.git

All is working, just be more attentive

Related

Spring resttemplate InvalidMediaTypeException. Invalid mime type due to Invalid token character '#' in token

The api I am calling responds with a content-type that has # in the value for in response to my resttemplate call.
final HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.ACCEPT, "key=abcd#def");
final ResponseEntity<String> responseEntity = resttemplate.exchange(
path, HttpMethod.GET, new HttpEntity<>(null, headers),
String.class);
Response Header
content-type: key=abc#def
Error I get
org.springframework.http.InvalidMediaTypeException:
Invalid mime type "key=abcd#def": Invalid token character '#' in token "abcd#def"

MessageConverter issue while using RestTemplate with StreamingResponseBody

We have a REST API (server side) implemented using Spring Boot. This API is streaming a PDF file as StreamingResponseBody wrapped in ResponseEntity where content-type is given as MediaType.APPLICATION_OCTET_STREAM.
I am trying to access this API from client application with the help of RestTemplate. This client application is again a Spring Boot app. This client application is existing and this was supporting MappingJackson2HttpMessageConverter with two supportive media types so far.
application/json and application/x-www-form-urlencoded
I followed few suggestions and tried with these items
Added MediaType.APPLICATION_OCTET_STREAM to existing
MappingJackson2HttpMessageConverter
Added ByteArrayHttpMessageConverter which has a default support to MediaType.APPLICATION_OCTET_STREAM
Added ResourceHttpMessageConverter which supporting streaming response.
But with all these suggestions I was facing the following errors. At this point of time I am not really sure if is there anything that I am missing from configuration. Team, it will be of a great help really if you can redirect me to a short examples or solutions in achieving this integration.
org.springframework.web.client.RestClientException: Error while extracting response for type [interface org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody] and content type [application/octet-stream]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: (PushbackInputStream); line: 1, column: 2]
This following error was when I tried with ByteArrayHttpMessageConverter (or) ResourceHttpMessageConverter
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody] and content type [application/octet-stream]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:123) ~[spring-web-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
Updating Question with the current implementation:
This is how resttemplate bean I am creating.
#Bean
public RestTemplate restTemplate() {
final RestTemplate restTemplate = new RestTemplate(httpRequestFactory());
final List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
final MappingJackson2HttpMessageConverter converter = new
MappingJackson2HttpMessageConverter();
final List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON);
mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
//mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM)
converter.setSupportedMediaTypes(mediaTypes);
messageConverters.add(converter);
restTemplate.setMessageConverters(messageConverters);
return restTemplate;
}
And my API client call is
ResponseEntity<StreamingResponseBody> response = reportRestTemplate.exchange(builder.buildAndExpand(uriParams).toUriString(),HttpMethod.GET,entity,StreamingResponseBody.class,uriParams);

Why im getting Exception- Not enough variables available to expand in Spring Rest URI call?

im working on a rest API call related to Payments. But i encountered with an exception. I know this comes due to setting parameters to the rest uri.
String uri = "https://test-gateway.mastercard.com/api/rest/version/52/merchant/{merchantId}/order/{orderId}/transaction/{transactionId}";
LinkedMultiValueMap<String, String> uriVars = new LinkedMultiValueMap<>();
uriVars.add("merchantId", _paymentInstrument.getAcquirerMid());
uriVars.add("orderId", _paymentInstrument.getOrderId().toString());
uriVars.add("transactionId", _paymentInstrument.getTargetTransactionId().toString());
String uri = UriComponentsBuilder.fromHttpUrl(url)
.queryParams(uriVars).build().toUriString();
VoidRequest voidRequest =
createVoidRequest(_paymentInstrument.getTargetTransactionId());
HttpEntity<VoidRequest> requestEntity = new HttpEntity<>(voidRequest, headers);
ResponseEntity<TransactionResponse> responseEntity = restTemplate.exchange(uri, HttpMethod.PUT, requestEntity, TransactionResponse.class);
Exception im getting is Using RestTemplate in Spring. Exception- Not enough variables available to expand "merchantId".

Could not write request: no suitable HttpMessageConverter found for request type

I have used to RestTemplate class to call a rest service, I am getting the below exception.
org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [UmlerUpdateRequest]
The below is my code snippet that I have tried:
RestTemplate restTemplate = new RestTemplate();
UmlerUpdateRequest obj= new UmlerUpdateRequest();
obj.setTrnspEqpId(equipId);
obj.setLstMaintId(lesseeScac);
obj.setRecLseScac(userId);
obj.setTareWgt(tareWeight);
obj.setBldD(builtDate);
String returns = restTemplate.postForObject(url, obj, String.class);
Can anyone help me on this?

translating http to RestTemplate

I have this elasticsearch query that works just fine when I plug it in to a browser:
http://my.server:9200/customer/customer/_search?q=age:[0+TO+1]&pretty
I'd really like to use it with Spring's RestTemplate:
String a = "http://my.server:9200/customer/customer/_search?q=age:[\"0\"+TO+\"1\"]";
String s = restTemplate.getForObject(a, String.class);
JSONObject j = new JSONObject(s);
and then access the keys/values via the JSONObject api.
Problem is, I'm getting the error:
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
from:
String s = restTemplate.getForObject(a, String.class);
What am I missing?
Is ES throwing the 400 ?
you might want to try that :
String url = "http://my.server:9200/customer/customer/_search?q={q}
Map params = new LinkedHashMap();
params.put("q", "age:[0 TO 1]");
restTemplate.getForObject(url, String.class, params)

Resources