Skip to content

Commit

Permalink
improved spring boot sample (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
rybalkinsd authored Nov 25, 2019
1 parent 206ac9f commit fd0e98a
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 71 deletions.
44 changes: 44 additions & 0 deletions samples/spring/README.md
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
```
2 changes: 1 addition & 1 deletion samples/spring/settings.gradle.kts
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"
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())
}
}
}
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"

Expand All @@ -31,7 +40,8 @@ class Controller {
"email" to "[email protected]"
}
}
}
}.also { it.close() }

return res.code()
}
}
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)
}

This file was deleted.

This file was deleted.

0 comments on commit fd0e98a

Please sign in to comment.