Skip to content

Commit 6cb4a16

Browse files
author
wlanboy
committed
added swagger and switched to record
1 parent 219b6fe commit 6cb4a16

6 files changed

Lines changed: 21 additions & 12 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ Spring based rest service to test http routes
1818
curl -L -X POST 'http://127.0.0.1:8080/client' -H 'Content-Type: application/json' \
1919
-d '{"url" : "https://github.com", "copyHeaders": "false"}'
2020
```
21+
22+
# swagger uri
23+
- http://localhost:8080/swagger-ui/index.html#/http-client-controller/postMapping

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>3.5.7</version>
9+
<version>3.5.8</version>
1010
<relativePath/> <!-- lookup parent from repository -->
1111
</parent>
1212

@@ -40,6 +40,11 @@
4040
<groupId>org.springframework.boot</groupId>
4141
<artifactId>spring-boot-starter-actuator</artifactId>
4242
</dependency>
43+
<dependency>
44+
<groupId>org.springdoc</groupId>
45+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
46+
<version>2.8.3</version>
47+
</dependency>
4348

4449
<dependency>
4550
<groupId>org.springframework.boot</groupId>

src/main/java/com/wlanboy/javahttpclient/client/ClientService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ public ResponseEntity<String> sendRequest(JavaHttpRequest requestData, HttpHeade
4747
try {
4848
List<String> badheaders = new ArrayList<>(Arrays.asList("Host","Content-Type","Content-Length","Accept-Encoding","host","content-type","content-length", "connection","accept-encoding"));
4949
List<String> headers = new ArrayList<>(Arrays.asList("test","wlanboy"));
50-
if (requestData.copyHeaders) {
50+
if (requestData.copyHeaders()) {
5151
httpHeaders.entrySet().stream().filter( (entry) -> !badheaders.contains(entry.getKey()) ).forEach((entry) -> {
5252
headers.add(entry.getKey());
5353
headers.add(entry.getValue().get(0));
5454
});
5555
}
5656

5757
HttpRequest request = HttpRequest.newBuilder()
58-
.uri(URI.create(requestData.url))
58+
.uri(URI.create(requestData.url()))
5959
.headers(headers.toArray(String[]::new))
6060
.GET()
6161
.build();

src/main/java/com/wlanboy/javahttpclient/controller/HttpClientController.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.wlanboy.javahttpclient.controller;
22

33
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.http.HttpEntity;
4+
import org.springframework.http.HttpHeaders;
55
import org.springframework.http.ResponseEntity;
66
import org.springframework.validation.annotation.Validated;
77
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestHeader;
810
import org.springframework.web.bind.annotation.RestController;
911

1012
import com.wlanboy.javahttpclient.client.ClientService;
@@ -16,8 +18,10 @@ public class HttpClientController {
1618
ClientService service;
1719

1820
@PostMapping(value = "/client")
19-
public ResponseEntity<String> postMapping(@Validated HttpEntity<JavaHttpRequest> requestdata) {
20-
return service.sendRequest(requestdata.getBody(), requestdata.getHeaders());
21+
public ResponseEntity<String> postMapping(@RequestBody @Validated JavaHttpRequest requestData,
22+
@RequestHeader HttpHeaders headers
23+
) {
24+
return service.sendRequest(requestData, headers);
2125
}
2226

2327
}

src/main/java/com/wlanboy/javahttpclient/controller/JavaHttpRequest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,5 @@
22

33
import org.springframework.lang.NonNull;
44

5-
public final class JavaHttpRequest {
5+
public record JavaHttpRequest(@NonNull String url, @NonNull Boolean copyHeaders) {}
66

7-
@NonNull
8-
public String url;
9-
@NonNull
10-
public Boolean copyHeaders;
11-
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
springdoc.api-docs.path=/api-docs
2+
springdoc.swagger-ui.path=/swagger-ui.html
13

0 commit comments

Comments
 (0)