在Kotlin中使用Spring Boot怎么实现一个RESTful服务

发布时间:2020-11-20 15:18:55 作者:Leah
来源:亿速云 阅读:167

今天就跟大家聊聊有关在Kotlin中使用Spring Boot怎么实现一个RESTful服务,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Spring太复杂了,配置这个东西简直就是浪费生命。尤其在没有什么并发压力,随便搞一个RESTful服务,让整个业务跑起来先的情况下,更是么有必要纠结在一堆的XML配置上。显然这么想的人是很多的,于是就有了Spring Boot。又由于Java 8太墨迹于是有了Kotlin。

数据源使用MySql。通过Spring Boot这个基本不怎么配置的,不怎么微的微框架的Spring Data JPA和Hibernate来访问数据。

处理依赖

这里使用Gradle来处理依赖。

首先下载官网给的初始项目:

git clone https://github.com/spring-guides/gs-accessing-data-jpa.git

然后跳转到gs-accessing-data-jpa/initial目录下。

用IntelliJ IDEA打开这个项目,选择使用Gradle管理依赖。

之后Gradle会自动下载依赖项。这会花一点时间。你可以去和妹子聊一会儿了。。

如果你觉得这样很麻烦的话,可以建立一个Gradle项目。之后根据上面的例子建立一个目录:

└── src
  └── main
    └── java
      └── hello

但是无论是用上面的哪种方式,最后都需要在Gradle文件中添加依赖项。这个Gradle文件是build.gradle。添加完依赖项
 之后是这样的:

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
  }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
  baseName = 'gs-spring-boot'
  version = '0.1.0'
}

repositories {
  mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
  // tag::jetty[]
  compile("org.springframework.boot:spring-boot-starter-web") {
    exclude module: "spring-boot-starter-tomcat"
  }
  compile("org.springframework.boot:spring-boot-starter-jetty")
  // end::jetty[]
  // tag::actuator[]
  compile("org.springframework.boot:spring-boot-starter-actuator")
  // end::actuator[]
  compile('org.springframework.boot:spring-boot-starter-data-jpa:1.3.3.RELEASE')

  compile('mysql:mysql-connector-java:5.1.13')

  testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
  gradleVersion = '2.3'
}

配置文件

在目录src/main/resources/application.properties下编辑配置文件。默认是没有这个文件和相应的目录的,自行创建。

spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
#spring.datasource.driverClassName = com.mysql.jdbc.Driver

# Specify the DBMS
spring.jpa.database = MYSQL

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

无需java的配置类,或者什么XML配置文件。

使用配置项hibernate.ddl-auto = true,项目所需的数据库和相关表、列会自动根据定义的实体类创建。点击这里,查看更多配置的说明。

创建一个简单地实体类

这里定义一个简单地实体类,并声明为JPA实体。这个类的文件存放在目录src\main\java\hello\Entities\下。

package hello.Entities

import javax.validation.constraints.NotNull
import java.io.Serializable;
import javax.persistence.*;

/**
 * Created by Bruce on 2016/3/9.
 */
@Entity
@Table(name = "user")
data class User(@Id @GeneratedValue(strategy = GenerationType.AUTO) var id: Long? = 0,
        @Column(nullable = false) var name: String? = null,
        @Column(nullable = false) var email: String? = null) : Serializable {

  protected constructor() : this(id = null, name = null, email = null) {
  }
}

这里使用了Kotlin里的data class。data class最大的优点就是省去了定义getter和setter,以及toString()的时间。这些都已经默认实现。所以,在使用data class的对象的时候直接可以使用nameemail当然还有id这样的属性直接访问。

无参数的构造函数是给JPA用的,所以访问级别设定为protected。主构造函数是用来创建和数据库操作相关的对象的。

整个的整个类被@Entity修饰,说明整个类是一个JPA的实体类。@Table声明用来表明整个类对应的数据库表是哪一个。
@Id修饰的User的属性id,会被JPA认为的对象的ID。同时@GeneratedValue(strategy = GenerationType.AUTO)
的修饰说明这个ID是自动生成的。

另外的两个属性nameemail@Column(nullable = false)修饰。说明两个列都是不可以为空的,同时说明两个列的名字和属性的名字是相同的。如果不同可以这样@Column(nullable = false, name="XXXXXX")。

创建简单地查询,或者说Dao类

这个就更加的简单了。JPA会自动在运行时创建数据库需要的增删改查的实现。这个实现可以是根据我们给出的Repository
来实现的。

根据User类,我们来实现一个UserDao(Repository):

package hello.Entities

import org.springframework.data.repository.CrudRepository
import org.springframework.transaction.annotation.Transactional

@Transactional
interface UserDao : CrudRepository<User, Long> {
  fun findByEmail(email: String): User&#63;
}

泛型的类型参数分别是user和user的id的类型:User, Long。我们可以定义增删改查之外的Query。比如在上面的代码里我们定义了一个findByEmail()方法。具体的自定义查询时的命名规则可以查看这里。

用Controller测试一下

数据库,Rest服务和书库的连接都已经搞定。那么,我们就来测试一下。

我们在目录src\main\java\hello\Controllers创建一个UserController类来测试和数据库的数据存取。

package hello.Controllers

import hello.Entities.User
import hello.Entities.UserDao
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody

/**
 * Created by Bruce on 2016/3/9.
 */

@RestController
class UserController {
  @Autowired
  private var userDao: UserDao&#63; = null

  @RequestMapping("/create")
  @ResponseBody
  public fun create(name: String, email: String): User&#63; {
    try {
      var newUser = User(name = name, email = email)
      userDao&#63;.save(newUser)

      return newUser
    } catch(e: Exception) {
      return null
    }
  }

  @RequestMapping("/delete")
  @ResponseBody
  public fun delete(id: Long): String {
    try {
      var user = User(id)
      userDao&#63;.delete(user)

      return id.toString() + "deleted"
    } catch(e: Exception) {
      return "delete error " + e.message.toString()
    }
  }

  @RequestMapping("/get-by-email")
  @ResponseBody
  public fun getByEmail(email: String): User&#63; {
    try {
      var user = userDao&#63;.findByEmail(email)
      if (user != null) {
        return user
      } else {
        return null
      }
    } catch(e: Exception) {
      return null
    }
  }

  @RequestMapping("/update")
  @ResponseBody
  public fun updateUser(id: Long, name: String, email: String): User&#63; {
    try {
      var user: User&#63; = userDao&#63;.findOne(id) &#63;: return null

      user&#63;.name = name
      user&#63;.email = email
      userDao&#63;.save(user)

      return user
    } catch(e: Exception) {
      return null
    }
  }
}

测试URL可以是这样的:

  1. /create&#63;name=Jack&email=hello@234.com,使用指定的用户名和邮箱在数据库里生成一个新的user,id是自动生成的。
  2. /delete&#63;id=3, 删除id值为3的user。
  3. /get-by-email&#63;email=hello@234.com,注意Controller用到的UserDao.findByEmail()只返回一个user,所以如果有多个 返回值的话会报错。
  4. /update&#63;id=1&email=what@123.com&name=Bruce,更新id为1的user。

看完上述内容,你们对在Kotlin中使用Spring Boot怎么实现一个RESTful服务有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

推荐阅读:
  1. 构建RESTful服务(使用Spring Data JPA)
  2. 使用spring boot怎么实现前后端传参

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

kotlin spring boot restful

上一篇:怎么在Java项目中创建一个多线程

下一篇:利用Java怎么样实现网络通信功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》