Skip to content

Instantly share code, notes, and snippets.

@omidp
Last active April 8, 2021 08:11
Show Gist options
  • Save omidp/256626538f55af75f93257a4ab4cd28b to your computer and use it in GitHub Desktop.
Save omidp/256626538f55af75f93257a4ab4cd28b to your computer and use it in GitHub Desktop.
sprint resttemplate with redirect
protected static HttpClientBuilder getClientBuilder(boolean skipSslValidation) {
HttpClientBuilder builder = HttpClients.custom()
.useSystemProperties()
.setRedirectStrategy(new DefaultRedirectStrategy());
if (skipSslValidation) {
builder.setSslcontext(getNonValidatingSslContext());
builder.setSSLHostnameVerifier(new NoopHostnameVerifier());
}
builder.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE);
return builder;
}
package com.jedlab.userservice;
import java.util.List;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/api/v1")
public class RedirectController
{
@PostMapping(value = "/redirect", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String getUserList(@RequestBody MultiValueMap<String, String> formData)
{
List<String> list = formData.get("tokenIdentity");
if (list != null)
System.out.println("tokenIdentity ##### : " + list.get(0));
return "you are here";
}
@PostMapping(value = "/shapar", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> shapar(@RequestBody String input)
{
System.out.println("!!!!!!!!! Input ");
System.out.println(input);
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
HttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
factory.setHttpClient(httpClient);
RestTemplate rt = new RestTemplate(factory);
HttpHeaders headers = new HttpHeaders();
// headers.setAccept(Collections.singletonList(MediaType.APPLICATION_FORM_URLENCODED));
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> reqBody = new LinkedMultiValueMap<String, String>();
reqBody.add("tokenIdentity", "AWSXCDERFV");
HttpEntity<?> requestEntity = new HttpEntity<>(reqBody, headers);
return rt.exchange("http://localhost:9999/api/v1/redirect", HttpMethod.POST, requestEntity,
String.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment