Skip to content

Instantly share code, notes, and snippets.

@benelog
Last active January 15, 2024 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benelog/2b5b53a3ccf154ab66fe1ba5d31a13bd to your computer and use it in GitHub Desktop.
Save benelog/2b5b53a3ccf154ab66fe1ba5d31a13bd to your computer and use it in GitHub Desktop.
GET + body by RestTemplate
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.1'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.httpcomponents.client5:httpclient5'
implementation 'io.projectreactor.netty:reactor-netty-http'
implementation 'org.eclipse.jetty:jetty-client'
implementation 'com.squareup.okhttp3:okhttp'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController("/")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping
public String echo(@RequestBody String body) {
return body;
}
}
package com.example.demo;
import static org.assertj.core.api.Assertions.assertThat;
import java.net.URI;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.http.client.JettyClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.client.ReactorNettyClientRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
class GetWithBodyRequestTest {
@ParameterizedTest
@MethodSource("provideRequestFactory")
void requestGetWithBody(ClientHttpRequestFactory requestFactory) {
var restTemplate = new RestTemplate(requestFactory);
var request = new HttpEntity<>("hello1");
ResponseEntity<String> response = restTemplate.exchange(
URI.create("http://localhost:8080/"),
HttpMethod.GET,
request,
String.class
);
assertThat(response.getBody()).isEqualTo("hello1");
}
static Stream<ClientHttpRequestFactory> provideRequestFactory() {
return Stream.of(
new JdkClientHttpRequestFactory(), // success
new HttpComponentsClientHttpRequestFactory(), // success
new ReactorNettyClientRequestFactory(), // success
new JettyClientHttpRequestFactory(), // success
new SimpleClientHttpRequestFactory(), // fail
new OkHttp3ClientHttpRequestFactory() // fail
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment