BAEL-5884 - URI encoding with Rest Template (#13207)
* BAEL-5884 - URI encoding with Rest Template * BAEL-5884 - URI encoding with Rest Template - indentation corrected
This commit is contained in:
parent
49550ca022
commit
8103e7087b
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.encoding;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung.encoding;
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.http.HttpRequest;
|
||||||
|
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||||
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||||
|
import org.springframework.http.client.ClientHttpResponse;
|
||||||
|
import org.springframework.http.client.support.HttpRequestWrapper;
|
||||||
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
public class UriEncodingInterceptor implements ClientHttpRequestInterceptor {
|
||||||
|
@Override
|
||||||
|
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
|
||||||
|
HttpRequest encodedRequest = new HttpRequestWrapper(request) {
|
||||||
|
@Override
|
||||||
|
public URI getURI() {
|
||||||
|
URI uri = super.getURI();
|
||||||
|
String escapedQuery = uri.getRawQuery().replace("+", "%2B");
|
||||||
|
return UriComponentsBuilder.fromUri(uri)
|
||||||
|
.replaceQuery(escapedQuery)
|
||||||
|
.build(true).toUri();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return execution.execute(encodedRequest, body);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.encoding.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
import org.springframework.web.util.DefaultUriBuilderFactory;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RestTemplateConfig {
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplate() {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
DefaultUriBuilderFactory defaultUriBuilderFactory = new DefaultUriBuilderFactory();
|
||||||
|
defaultUriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);
|
||||||
|
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory());
|
||||||
|
return restTemplate;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.encoding.config;
|
||||||
|
|
||||||
|
import com.baeldung.encoding.UriEncodingInterceptor;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RestTemplateWithInterceptorsConfig {
|
||||||
|
@Qualifier("restTemplateWithInterceptors")
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplateWithInterceptors() {
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
restTemplate.setInterceptors(Collections.singletonList(new UriEncodingInterceptor()));
|
||||||
|
return restTemplate;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.encoding.service;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class HttpBinService {
|
||||||
|
private final RestTemplate restTemplate;
|
||||||
|
|
||||||
|
public HttpBinService(RestTemplate restTemplate) {
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String get(String parameter) throws JsonProcessingException {
|
||||||
|
String url = "http://httpbin.org/get?parameter={parameter}";
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class, parameter);
|
||||||
|
Map<String, Object> mapping = new ObjectMapper().readValue(response.getBody(), HashMap.class);
|
||||||
|
Map<String, String> args = (Map<String, String>) mapping.get("args");
|
||||||
|
return args.get("parameter");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.encoding.service;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class HttpBinServiceUnitTest {
|
||||||
|
@Autowired
|
||||||
|
private HttpBinService httpBinService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenWithoutPlusSign_whenGet_thenSameValueReturned() throws JsonProcessingException {
|
||||||
|
String parameterWithoutPlusSign = "springboot";
|
||||||
|
String responseWithoutPlusSign = httpBinService.get(parameterWithoutPlusSign);
|
||||||
|
assertEquals(parameterWithoutPlusSign, responseWithoutPlusSign);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenWithPlusSign_whenGet_thenSameValueReturned() throws JsonProcessingException {
|
||||||
|
String parameterWithPlusSign = "spring+boot";
|
||||||
|
String responseWithPlusSign = httpBinService.get(parameterWithPlusSign);
|
||||||
|
assertEquals(parameterWithPlusSign, responseWithPlusSign);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user