/spring-boot-ktor

ktor and spring-web integrate

Primary LanguageKotlin

ktor and spring-web integrate

  • easy to migrate from spring-boot-web(or webflux)

version

  • route registry
  • parameter receive
  • support suspend functions
  • support java class route
  • suport request header / cookie value
  • support response redirect
  • support static resource
  • file upload(support multipart file) and download (inject ApplicationResponse)
  • handle response view freemarker supported
  • use different parameter resolvers instead of single one
  • support multi file upload
  • support more annotations like GetMapping/PostMapping etc.

roadmap

  • support dynamic registry route
  • filters or intercepts
  • handle exception
  • handle CQRS

quick start

  • add repository
    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
  • add dependency
        <dependency>
            <groupId>com.theoxao</groupId>
            <artifactId>spring-boot-ktor</artifactId>
            <version>0.2.1-alpha</version>
        </dependency>
  • controller(kotlin)
@RestController
@RequestMapping("/ocr")
class OCRController(private val ocrService: OCRService) {
    @PostMapping("/recognize")
    suspend fun base64(@RequestParam("file") file: MultipartFile): String {
        return ocrService.recognize(file)
    }
}
  • same with java code (of course, no suspend)
@RestController
@RequestMapping("/ocr/java")
public class OCRJavaController {
    @Autowired
    private OCRService ocrService;

    @RequestMapping("/recognize")
    public String base64(MultipartFile file){
        return ocrService.recognize(file);
    }
}
  • configuration
spring:
  ktor:
    port: 8080
    enableTrace: false
    engine: "Netty"  //Netty and CIO only
    staticRoot: "static"
    templatesRoot: "templates"
  • supported spring-web annotaiton
@Controller
@CookieValue
@RequestBody
@RequestHeader
@RequestMapping
@ResponseBody
@RestController
@GetMapping
@PostMapping
@DeleteMapping
@PutMapping
  • request and response inject
fun base64(request:ApplicationRequest,response:ApplicationResponse): String ...