-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
206ac9f
commit fd0e98a
Showing
7 changed files
with
151 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# kohttp-spring sample | ||
|
||
## Description | ||
This is a basic application showing kohttp usage together with Spring Boot. | ||
We are using Spring Boot 2.x, however, Spring Boot 1.x uses similar ideas, except Spring MVC Router DSL. | ||
|
||
Application expose 4 endpoints on `localhost:8080` | ||
- GET /foo | ||
- POST /foo | ||
- GET /foobar | ||
- POST /foobar | ||
|
||
Each endpoint perform various requests using `kohttp`. | ||
Both GET endpoints use defaultHttpClient as a client for `kohttp` requests. | ||
However, both POST endpoints use custom http client with extended `readTimeout` and logging interceptor | ||
|
||
## Build | ||
Build sample as any other SpringBoot application. | ||
```bash | ||
./gradlew clean bootJar | ||
``` | ||
|
||
## Run | ||
Run sample as any other SpringBoot application. | ||
```bash | ||
./gradlew bootRun | ||
``` | ||
|
||
## Test | ||
|
||
### GET requests | ||
Open `localhost:8080/foo` or `localhost:8080/foobar` in your browser. | ||
Or use cURL | ||
```bash | ||
curl -X GET http://localhost:8080/foo | ||
curl -X GET http://localhost:8080/foobar | ||
``` | ||
|
||
### POST requests | ||
Use cURL | ||
```bash | ||
curl -X POST http://localhost:8080/foo | ||
curl -X POST http://localhost:8080/foobar | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
rootProject.name = "spring_kohttp" | ||
rootProject.name = "spring-kohttp" | ||
rootProject.buildFileName = "build.gradle.kts" |
54 changes: 54 additions & 0 deletions
54
samples/spring/src/main/kotlin/io/github/rybalkinsd/kohttp/spring/Configuration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.github.rybalkinsd.kohttp.spring | ||
|
||
import io.github.rybalkinsd.kohttp.dsl.httpGet | ||
import io.github.rybalkinsd.kohttp.dsl.httpPost | ||
import okhttp3.Call | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.web.servlet.function.RouterFunction | ||
import org.springframework.web.servlet.function.ServerResponse | ||
import org.springframework.web.servlet.function.router | ||
|
||
/** | ||
* Configuration based on Spring MVC Router DSL | ||
* | ||
* NOTE: available @since Spring 5.2 | ||
*/ | ||
@Configuration | ||
class Configuration { | ||
|
||
/** | ||
* Using Router DSL to define MVC controller. | ||
* | ||
* NOTE: available @since Spring 5.2 | ||
*/ | ||
@Bean | ||
fun routes(extendedTimeoutClient: Call.Factory): RouterFunction<ServerResponse> = router { | ||
// maps GET /foobar to httpGet call with defaultHttpClient | ||
GET("/foobar") { | ||
val res = httpGet { | ||
host = "api.github.com" | ||
path = "/" | ||
}.also { it.close() } | ||
|
||
ServerResponse.ok().body(res.code()) | ||
} | ||
|
||
// maps POST /foobar to httpPost call with extendedTimeoutClient | ||
POST("/foobar") { | ||
// using controller with higher timeout due to potentially slow response time | ||
val res = httpPost(extendedTimeoutClient) { | ||
host = "postman-echo.com" | ||
path = "/post" | ||
|
||
body { | ||
json { | ||
"login" to "user" | ||
"email" to "[email protected]" | ||
} | ||
} | ||
}.also { it.close() } | ||
ServerResponse.ok().body(res.code()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,36 @@ | ||
package io.github.rybalkinsd.kohttp.spring_kohttp | ||
package io.github.rybalkinsd.kohttp.spring | ||
|
||
import io.github.rybalkinsd.kohttp.dsl.httpGet | ||
import io.github.rybalkinsd.kohttp.dsl.httpPost | ||
import okhttp3.Call | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
/** | ||
* Annotation based REST controller definition | ||
*/ | ||
@RestController | ||
class Controller { | ||
|
||
@Autowired | ||
lateinit var extendedTimeoutClient : Call.Factory | ||
|
||
@GetMapping("/foo") | ||
fun getHandler(): Int { | ||
val res = httpGet { | ||
host = "api.github.com" | ||
path = "/" | ||
} | ||
}.also { it.close() } | ||
|
||
return res.code() | ||
} | ||
|
||
@PostMapping("/foo") | ||
fun posthandler(): Int { | ||
val res = httpPost { | ||
fun postHandler(): Int { | ||
// using controller with higher timeout due to potentially slow response time | ||
val res = httpPost(extendedTimeoutClient) { | ||
host = "postman-echo.com" | ||
path = "/post" | ||
|
||
|
@@ -31,7 +40,8 @@ class Controller { | |
"email" to "[email protected]" | ||
} | ||
} | ||
} | ||
}.also { it.close() } | ||
|
||
return res.code() | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
samples/spring/src/main/kotlin/io/github/rybalkinsd/kohttp/spring/SpringKohttpApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.github.rybalkinsd.kohttp.spring | ||
|
||
import io.github.rybalkinsd.kohttp.client.client | ||
import io.github.rybalkinsd.kohttp.interceptors.logging.HttpLoggingInterceptor | ||
import okhttp3.Call | ||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.runApplication | ||
import org.springframework.context.annotation.Bean | ||
|
||
/** | ||
* Basic application context initialization | ||
* ComponentScan will also trigger initialization in | ||
* - [Configuration] | ||
* - [Controller] | ||
*/ | ||
@SpringBootApplication | ||
class SpringKohttpApplication { | ||
|
||
/** | ||
* Specific client to handle potentially relatively slow server responses | ||
*/ | ||
@Bean | ||
fun extendedTimeoutClient(): Call.Factory = client { | ||
// 10 minutes timeout | ||
readTimeout = 10 * 60 * 1000 | ||
interceptors { | ||
+HttpLoggingInterceptor() | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Application entry point | ||
*/ | ||
fun main(args: Array<String>) { | ||
runApplication<SpringKohttpApplication>(*args) | ||
} |
11 changes: 0 additions & 11 deletions
11
...ring/src/main/kotlin/io/github/rybalkinsd/kohttp/spring_kohttp/SpringKohttpApplication.kt
This file was deleted.
Oops, something went wrong.
54 changes: 0 additions & 54 deletions
54
samples/spring/src/main/kotlin/io/github/rybalkinsd/kohttp/spring_kohttp/WebmvcController.kt
This file was deleted.
Oops, something went wrong.