当前位置: 首页 > 编程日记 > 正文

超越Android:Kotlin在后端的工作方式

by Adam Arold

亚当·阿罗德(Adam Arold)

超越Android:Kotlin在后端的工作方式 (Going Beyond Android: how Kotlin works on the Backend)

This article is part of a series.

本文是系列文章的一部分。

While most developers use Kotlin on Android, it is also a viable option on other platforms. In this article, we’ll look at how it works on the backend.

尽管大多数开发人员在Android上使用Kotlin,但在其他平台上也是可行的选择。 在本文中,我们将研究它在后端的工作方式。

As I have written about this before, I think that the interop between Java and Kotlin is quite seamless. This also means that using Kotlin in place of Java on the backend is rather easy. Apart from a few nuisances, you can pretty much start writing your new features in Kotlin within your Java project. Or if you just want to try it out, you can start by writing your tests with it.

正如我之前所写 ,我认为Java和Kotlin之间的互操作是无缝的。 这也意味着在后端使用Kotlin代替Java很容易。 除了一些麻烦之外,您几乎可以开始在Java项目中的Kotlin中编写新功能。 或者,如果您只想尝试一下,则可以开始编写测试。

If you look around, it seems that companies with a big slice of the backend pie also have the same thought: the new version of Spring has some features dedicated to Kotlin, and you can even use Kotlin to write your Gradle scripts using the kotlin-dsl.

如果环顾四周,似乎后端派中有很大一部分的公司也有相同的想法:Spring的新版本具有一些Kotlin专用的功能,您甚至可以使用Kotlin使用kotlin-编写Gradle脚本- dsl 。

What is interesting to note here is that you don’t need Kotlin support for any of these libraries, because the Java interop features of Kotlin are so good.

这里有趣的是您不需要这些库中的任何一个Kotlin支持 ,因为Kotlin的Java互操作性非常好。

决定 (Decisions)

When you start to work with Kotlin on the backend, you have several options at your disposal.

当您开始在后端使用Kotlin时,可以使用多种选择。

If you choose to use a library written in Kotlin, you get some major advantages: there will be no compatibility issues related to the language you use since everything is written in Kotlin. Another thing worth mentioning is that there are some things which are only present in Kotlin, such as Coroutines and reified generics.

如果您选择使用用Kotlin编写的库,您将获得一些主要优势:由于所有内容都是用Kotlin编写的,因此不会出现与您使用的语言相关的兼容性问题。 值得一提的另一件事是,有些东西仅在Kotlin中存在,例如协程和通用泛型。

The trade-off is that most of these libraries are not very old and might have the typical problems with new projects: lack of documentation and eventual bugs or design issues.

需要权衡的是,这些库中的大多数都不是很老,并且在新项目中可能会遇到典型的问题:缺少文档和最终的错误或设计问题。

If you need something that is battle-tested, you can’t go wrong with tools like Spring or Mockito. What you get with these is that most issues are well-documented, and you’ll get answers for your questions pretty quickly. What you lose, though, is some of the nice things about Kotlin: they don’t have Kotlin DSLs, and the code will be quite Java-ish.

如果您需要经过考验的东西,那么使用Spring或Mockito之类的工具就不会出错。 您得到的是大多数问题都有据可查,并且您很快就会得到问题的答案。 但是,您失去的是有关Kotlin的一些好处:它们没有Kotlin DSL ,并且代码非常像Java。

Another option is to pick a library which has built-in support for Kotlin. RxKotlin and vert.x are good examples of this. I’ll call these projects hybrids from now on. With them you get the best of both worlds: you will usually have a good API which is idiomatic from a Kotlin perspective, and behind it there will be something which is well-known and battle-hardened.

另一个选择是选择一个内置了对Kotlin支持的库。 RxKotlin和vert.x就是很好的例子。 从现在开始,我将这些项目称为混合项目。 有了它们,您将两全其美:通常,您将拥有一个良好的API,从Kotlin的角度来看这是惯用的,在它的后面将有一个众所周知的且经过艰苦训练的东西。

Let’s look at some libraries which you might want to try out.

让我们看一些您可能想尝试的库。

Note that the following are just my opinions, and as such they are subjective.

请注意,以下只是我的观点,因此,它们是主观的。

克托 (Ktor)

Ktor is one of the newer web frameworks written in Kotlin. It comes with embedded Netty and a nice DSL to boot. What is interesting in this one is that it takes advantage of Kotlin’s Coroutine support. This is how it looks in practice:

Ktor是用Kotlin编写的较新的Web框架之一。 它带有嵌入式Netty和不错的DSL引导。 有趣的是,它利用了Kotlin的协程支持。 在实际中是这样的:

I was concerned when I tried it out that the documentation is quite lacking. When you bump into a problem, you are more likely to get stuck. It also does not perform well, and interop with Java is a bit sketchy.

当我尝试了一下,该文档我而言相当缺乏 。 当您遇到问题时,您更有可能陷入困境。 它的性能也不佳 ,与Java的互操作有点粗略。

Javalin (Javalin)

Javalin is an other web framework which has a very simple, fluent, and readable API. It can also work with multiple embedded web servers like Netty or Undertow. What I liked most is that it strikes a balance between the minimalistic approach of Spark (not to be confused with Apache Spark) and the low-level nature of vert.x. The documentation is also very good so you can get started in no time:

Javalin是另一个Web框架,具有非常简单,流利且易读的API。 它还可以与Netty或Undertow等多个嵌入式Web服务器一起使用。 我最喜欢的是,它在Spark的简约方法(不要与Apache Spark混淆)和vert.x的低级特性之间取得平衡 。 该文档也非常好,因此您可以立即开始:

I like that the API is a little more Java-ish than Ktor’s, so if you come from a Java background it might be easier to get started with.

我喜欢API比Ktor的Java风格多一​​点,因此,如果您来自Java背景,那么上手可能会更容易。

六边形 (Hexagon)

Hexagon is an interesting choice for writing web applications. The name choice is not random: it encourages using the hexagonal architecture (more commonly known as clean architecture), and it is also more performant than ktor or even the Spring framework! The DSL which you get is also pretty descriptive:

Hexagon是编写Web应用程序的有趣选择。 名称的选择不是随机的:它鼓励使用六角形体系结构 (通常称为clean体系结构 ),并且比ktor甚至是Spring框架性能更高! 您获得的DSL也很具有描述性:

脱网 (Off the web)

It is worth noting that there are a lot of tools written in Kotlin which you can pick from. Kotlin has a very useful specification framework. If you don’t like the fact that there are no good HTTP clients for Java, you can now take advantage of Fuel which is a very handy tool for interfacing with REST endpoints and beyond. You even have tools for writing games in Kotlin.

值得注意的是,有很多用Kotlin编写的工具可供选择。 Kotlin有一个非常有用的规范框架 。 如果您不喜欢没有适用于Java的HTTP客户端的事实,那么现在就可以利用Fuel这个非常方便的工具,它可以与REST端点及其他接口交互。 您甚至拥有使用Kotlin 编写游戏的工具。

Java框架 (Java frameworks)

版本号 (vert.x)

vert.x is a multi-module web framework akin to Spring. What is important to note here is that there is a documentation section dedicated to Kotlin. vert.x might be your choice for writing web applications if performance is paramount to you: vert.x has very good benchmark scores. This should not be a surprise, since it implements the multi reactor pattern (the reactor pattern might be familiar to you from node.js).

vert.x是类似于Spring的多模块Web框架。 这里要注意的重要一点是,有一个专门针对Kotlin的文档部分 。 如果性能对您至关重要,那么vert.x可能是您编写Web应用程序的选择:vert.x具有非常好的基准得分 。 这并不奇怪,因为它实现了多React堆模式 (您可能会从node.js熟悉React堆模式)。

It is worth noting that there are very good Kotlin examples for vert.x. You’ll need them because it has some concepts which are not present elsewhere and setting it up is also a bit more involved:

值得注意的是,vert.x有很好的Kotlin示例 。 您将需要它们,因为它具有一些其他地方没有的概念,并且还需要更多地进行设置:

弹簧 (Spring)

For a lot of Java developers, Spring is the de facto tool for writing Java applications. The good news is that since 5.0 Spring has built-in support for Kotlin. There is a whole plethora of Spring projects which you can pick from. If you are interested, there is a simple tutorial which will get you started using Spring with Kotlin. Here is how Hello World looks with it:

对于许多Java开发人员而言, Spring实际上是编写Java应用程序的工具。 好消息是,自5.0以来,Spring 内置了对Kotlin的支持 。 您可以从中选择大量的Spring项目 。 如果您有兴趣,这里有一个简单的教程 ,可以帮助您开始使用Spring和Kotlin。 这是Hello World的外观:

Sparkjava的 (Sparkjava)

Sparkjava is a minimalistic (micro)web framework. You can get started with it with practically zero time investment, and if you come from node.js it is also a very good choice. You also can’t get more minimal than this:

Sparkjava是一个简约的(微型)Web框架。 您可以从几乎零时间的投资开始使用它,如果您来自node.js,它也是一个很好的选择。 您还不能获得比这更多的最小:

混合选项 (Hybrid options)

While interfacing with Java tools is usually pretty convenient, some of the tools above have tooling support written for Kotlin:

虽然与Java工具的接口通常很方便,但是上面的一些工具具有为Kotlin编写的工具支持:

Spark-kotlin provides a more streamlined experience for Kotlin users, and it is also worth noting that it is written by perwendel, the original author of Sparkjava.

Spark-kotlin为Kotlin用户提供了更简化的体验,还值得注意的是,它是由Sparkjava的原始作者perwendel编写的。

vertx-lang-kotlin provides useful Kotlin-specific options for vert.x like coroutines, one-shot workers, or reactive streams.

vertx-lang-kotlinvert.x提供有用的Kotlin特定选项,例如协程,单发工作程序或React流。

While using Mockito from Kotlin is mostly pleasant, mockito-kotlin improves upon that by giving you a nice DSL and taking care of some issues.

虽然使用Kotlin的Mockito大部分是令人愉快的,但mockito-kotlin通过为您提供漂亮的DSL并解决了一些问题改进了它。

Hamkrest is a re-implementation of Hamcrest with syntactic sugars, extensibility and more.

Hamkrest是Hamcrest的重新实现,其中包含语法糖,可扩展性等。

RxKotlin adds convenient extension functions, SAM helpers, and more to RxJava. The only problem is that the documentation is behind a paywall.

RxKotlin向RxJava添加了方便的扩展功能 ,SAM帮助器等 。 唯一的问题是文档在付费专线后面。

一个有效的例子 (A working example)

Now let’s look at a step-by-step example. We’ll use Spring Boot with Spring Initializr.

现在,让我们看一个分步示例。 我们将结合使用Spring Boot和Spring Initializr 。

The source code of this tutorial can be found here.

本教程的源代码可以在这里找到。

入门 (Getting started)

Spring Boot comes with Spring Initializr, which is a handy tool with which you can quickly kick off your project. I recommend selecting Gradle Project with Kotlin and Spring Boot 2.0.0 for this example, since with 2.0.0 you’ll get the features of Spring 5.0.

Spring Boot带有Spring Initializr ,它是一个方便的工具,您可以使用它快速启动项目。 我建议在此示例中选择使用Kotlin和Spring Boot 2.0.0的 Gradle Project ,因为使用2.0.0时,您将获得Spring 5.0的功能。

If you click “Switch to full version” you’ll also be able to piece together a fine-grained skeleton. There is a cornucopia of topics from which you can pick tools ranging from Web to AWS and more. For this exercise I picked Web only.

如果您点击“切换到完整版本”,您还可以拼凑出细粒度的骨架。 主题众多,您可以从中选择各种工具,从Web到AWS等等。 对于本练习,我只选择了Web。

After setting up Initializr, click “Generate Project” and open it in your IDE. You might notice that compared to a simple Java project, you don’t have to add much to your build.gradle which is Kotlin-specific. I’ve extracted them in this example:

设置Initializr后,单击“ Generate Project”并在IDE中打开它。 您可能会注意到,与简单的Java项目相比,无需向Kotlin特定的build.gradle添加太多内容。 在此示例中,我提取了它们:

添加一个简单的控制器 (Adding a simple Controller)

If you look at the entry point of the application, it is rather minimalistic:

如果您查看应用程序的入口点,那么它非常简单:

Now the only thing we need for a working Hello World is to add a RestController to our project:

现在, RestController Hello World正常运行,我们唯一需要的是将RestController添加到我们的项目中:

and Bam! you are done! You can go and check out the result at http://localhost:8080/ after starting it up with ./gradlew bootRun :

班! 大功告成! 使用./gradlew bootRun启动结果后,您可以在http://localhost:8080/./gradlew bootRun

You can learn more about how this works in this tutorial. Or if you want to see how the Kotlin APIs and the functional way looks, there is a nice tutorial here.

您可以在本教程中了解有关其工作原理的更多信息。 或者,如果您想了解Kotlin API和功能方式的外观,可以在此处找到一个不错的教程。

结语 (Wrapping up)

In this article, we have explored some of the more well-known options for backend development with Kotlin. We have also seen that prominent actors on the market have embraced Kotlin, and backend development can be a lot simpler with it compared to pure Java.

在本文中,我们探讨了Kotlin进行后端开发的一些更知名的选项。 我们还看到,市场上的杰出参与者已经拥抱Kotlin,并且与纯Java相比,后端开发可以用它简单得多。

In the next article we’ll explore how Kotlin can be used in your project instead of Javascript!

在下一篇文章中,我们将探讨如何在您的项目中使用Kotlin而不是Javascript!

Thanks for reading! You can read more of my articles on my blog.

谢谢阅读! 您可以在我的博客上我的文章。

翻译自: https://www.freecodecamp.org/news/going-beyond-android-kotlin-on-the-backend-2a75eef2582b/

相关文章:

词汇的理解 —— 汉译英(术语)

词汇的理解 —— 英译汉 1. 名词 机制:mechanism,系统:system;2. 动词 融资:financing;制动:braking,就是“刹车”;3. 音乐与乐器 horn:喇叭,号角…

Swift从零开始学习_08(代理协议传值)

Swift中的代理协议的写法. 这是第一个页面有一个button和一个label, button点击跳到下一个页面. 第二个页面有一个输入框和一个按钮, 点击按钮把输入框里的内容设置为第一个页面label的内容.效果如下 接下来是代码部分.跟OC的写法还是一样的.这里不再写第一个页面的那些UI的…

[微信小程序]商城之购买商品数量实现

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文: 这里有三种变更数量的方式, 加号,减号,input输入 , 这里做了限制,数量不能小于等于0并且不能超过现有库存,下面是…

测试nginx网站代码_在40行以下代码中使用NGINX进行A / B测试

测试nginx网站代码by Nitish Phanse由Nitish Phanse 在40行以下代码中使用NGINX进行A / B测试 (A/B testing with NGINX in under 40 lines of code) A/B Testing, has enabled designers and product managers to get a deep insight into user behavioral patterns.A / B测试…

HttpServletResponse,HttpServletRequest详解

HttpServletResponse,HttpServletRequest详解 1、相关的接口 HttpServletRequest HttpServletRequest接口最常用的方法就是获得请求中的参数,这些参数一般是客户端表单中的数据。同时,HttpServletRequest接口可以获取由客户端传送的名称,也可…

[微信小程序]this.setData , that.setData , this.data.val三者之间的区别和作用

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 1.this.setData({ }) <view bindtouchmove"tap_drag" bindtouchend"tap_end" bindtouchstart"tap_start" class"page-top" style"{…

jQuery(一)引入

一、jQuery简介 jQuery是一个兼容多浏览器的javascript库&#xff0c;核心理念是write less,do more(写得更少,做得更多) 二、安装 2.1、下载 下载地址&#xff1a;http://jquery.com/download/ 2.2、引入 在页面头部加入 <head> <meta http-equiv"Content-Type&…

javascript 堆栈_JavaScript调用堆栈-它是什么以及为什么它是必需的

javascript 堆栈The JavaScript engine (which is found in a hosting environment like the browser), is a single-threaded interpreter comprising of a heap and a single call stack. The browser provides web APIs like the DOM, AJAX, and Timers.JavaScript引擎(可在…

idea崩溃导致的svn插件丢失问题, maven dependencies视图丢失问题

Idea丢失Svn解决办法今天打开Idea&#xff0c;习惯用ctrlt来更新svn&#xff0c;杯具出现了&#xff0c;快捷键失效了&#xff0c;我觉得可能是其他的什么软件占用了这个快捷键&#xff0c;于是重启了一下&#xff0c;发现还是不行&#xff0c;svn信息怎么没了&#xff0c;chan…

python3代码

import urllib.request url"http://mm.taobao.com/json/request_top_list.htm?type0&page1" upurllib.request.urlopen(url)#打开目标页面&#xff0c;存入变量up contup.read()#从up中读入该HTML文件 key1<a href"http#设置关键字1key2"target&qu…

【微信小程序】侧滑栏,手动侧滑出个人中心(完整代码附效果图)

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; 博文分三部分&#xff0c;1.效果图及功能效果说明 2.实现思路 3.源代码 欢迎加入微信小程序开发交流群&#xff08;173683895&#xff09; 一.老惯例先上效果图&#xff0c;本篇博…

1:1 人脸比对 开源_Hacktoberfest:我的开源门户

1:1 人脸比对 开源by Maribel Duran通过Maribel Duran Hacktoberfest&#xff1a;我的开源门户 (Hacktoberfest: My Gateway to Open Source) “Individually, we are one drop. Together, we are an ocean.”“就个人而言&#xff0c;我们只是一滴滴。 在一起&#xff0c;我们…

地图收敛心得170405

寻路算法大总结! 交换机生成树采用的是完全不同的D-V(distance vector)距离矢量算法,并不是很可靠. 并不是任意两点之间的最短路径,因为任意两点之间取最短路径可能有环路:总权更大 交换机STP不一定是最小生成树!!!举例论证 因为它只是所有交换机到根桥最短 贪心算法的味道 kru…

微信小程序游戏开发文档以及开发工具地址

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; 微信官方于 2017 - 12 - 28 日 开发微信小程序 开发小游戏 &#xff0c; 微信小程序小游戏开发官方文档的地址 https://mp.weixin.qq.com/debug/wxagame/dev/index.html?t20171228…

c#编译执行过程

创建一个简单的控制台程序&#xff0c;源码如下&#xff1a; using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace csharpBuildProcess {class Program{static void Main(string[] args){for (int i 0; i < 100; i){if(i%20)…

渐进式web应用程序_渐进式Web应用程序简介

渐进式web应用程序Interested in learning JavaScript? Get my ebook at jshandbook.com有兴趣学习JavaScript吗&#xff1f; 在jshandbook.com上获取我的电子书 Progressive Web Apps (PWA) are the latest trend in mobile application development using web technologies.…

第二百二十节,jQuery EasyUI,Slider(滑动条)组件

jQuery EasyUI&#xff0c;Slider(滑动条)组件 学习要点&#xff1a; 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Slider(滑动条)组件的使用方法&#xff0c;这个组件依赖于 Draggable(拖动)组件。 一&#xff0e;加载方式 class 加载方式 <…

适用于SharePoint 2013 的 CAML Desinger

适用于SharePoint 2013 的 CAML Desinger 分类&#xff1a; SharePoint2013-01-15 21:52 1877人阅读 评论(0) 收藏 举报CAMLDesingerSharePoint 2013代码生成适用于如果说Sql是信息管理系统的一等公民&#xff0c;那么SharePoint 系统中的一等公民就非CAML莫属了。 但是这个一等…

微信小程序 跑马灯效果完整代码附效果图

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 一&#xff1a;功能介绍及讲解 实现的跑马灯&#xff08;跑马灯里面显示文章的title&#xff09;的效果&#xff0c;并在右侧有个查看文章的按钮&#xff0c;按钮绑定当前的跑马灯信…

热闹的聚会与尴尬的聚会_如何增加(和保存)您最喜欢的技术聚会

热闹的聚会与尴尬的聚会by Jen Weber詹韦伯(Jen Weber) 如何增加(和保存)您最喜欢的技术聚会 (How to Grow (and Save) Your Favorite Tech Meetup) Hey meetup facilitators, friends, and future leaders! Do you want more people to show up to your tech event? Or at l…

蓝桥杯-搭积木-java

/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称&#xff1a; 蓝桥杯赛题 * 作 者&#xff1a; 彭俊豪 * 完成日期&#xf…

微信小程序多张图片和表单一起上传,验证表单及进度条的实现完整代码

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 效果图&#xff1a; 完整代码: <!--pages/register/register.wxml--> <view classtop> <view>注 册 须 知 : </view> </view> <view> <view …

Android, BaseAdapter 处理大数据量时的优化

Android优化 最常见的就是ListView, Gallery, GridView, ViewPager 的大数据优化 图片优化 访问网络的优化优化的原则&#xff1a; 数据延迟加载 分批加载 本地缓存数据优化 1).复用contentview 2).创建static class ViewHolder 3).分…

meetup_我在2017年举办Meetup中学到的知识以及为何对2018年充满期待。

meetupby Daniel Deutsch由Daniel Deutsch 我在2017年举办Meetup中学到的知识以及为何对2018年充满期待。 (What I’ve learned hosting Meetups in 2017 — and why I’m looking forward to 2018.) As 2017 comes to an end, it’s time to reflect on the non-profit work …

BASE64 编码和解码

依赖jar: import org.apache.commons.codec.binary.Base64; BASE64和其他相似的编码算法通常用于转换二进制数据为文本数据&#xff0c;其目的是为了简化存储或传输。更具体地说&#xff0c;BASE64算法主要用于转换二进 制数据为ASCII字符串格式。Java语言提供了一个非常好的BA…

Android开发常用属性

1、android string.xml 文字中间加入空格 android string.xml前后加空格的技巧 <string name"password">密 码</string> &#160 这个就代表着空格 2、文字单行显示 android layout布局文件中TextView、EditView单行显示和输入 <TextView androi…

JS计算起点坐标到终点坐标的驾车距离和驾车时间

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 先上计算距离的简单demo&#xff1a; <!DOCTYPE html> <html><head><meta http-equiv"Content-Type" content"text/html; charsetutf-8"&…

css flexbox模型_5分钟内学习CSS Flexbox-初学者教程

css flexbox模型快速介绍流行的布局模块 (A quick introduction to the popular layout module) In this post, you’ll learn the basics of CSS Flexbox, which has become a must-have skill for web developers and designers in the last couple of years.在本文中&#x…

「linux网络管理」OSI模型

学习linux网络管理&#xff0c;笔记整理&#xff0c;促进记忆。 OSI&#xff08;开放系统互联模型&#xff09;包含七层&#xff0c;由应用层向物理层递进&#xff0c;分别有不同的协议和数据处理方式。 应用层--> 表示层--> 会话层--> 传输层--> 网络层--> 数据…

微信小程序下拉刷新和上拉加载的实现

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 一&#xff1a; 下拉刷新 下拉刷新两个步骤就能实现。 1.在要实现下拉刷新的页面的json配置文件里面加上 "enablePullDownRefresh": true, //开启下拉刷新"backgro…