职场取经Spring Boot与kotlin实战我爱编程

Spring Boot与Kotlin使用Spring Data

2018-01-26  本文已影响740人  全科

REST web服务已成为在web上应用程序集成的首选方式。在其核心中,REST定义了系统由客户端交互的资源组成。这些资源以超媒体驱动的方式实现。Spring MVC为构建这些服务提供了坚实的基础。但是,对于多域对象系统,即使实施REST web服务的最简单原则也可能相当乏味,并且导致大量样板代码。

Spring Data REST构建在Spring Data repositories之上,并自动将其导出为REST资源。它利用超媒体来允许客户端查找存储库暴露的功能,并将这些资源自动集成到相关的超媒体功能中。

将Spring Data REST添加到Spring Boot项目

dependencies {
    ...
    compile "org.springframework.boot:spring-boot-starter-data-rest:$spring_boot_version"
    ...
}

完整的build.gradle文件

group 'name.quanke.kotlin'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.2.10'
    ext.spring_boot_version = '1.5.4.RELEASE'
    ext.springfox_swagger2_version = '2.7.0'
    ext.mysql_version = '5.1.21'
    ext.mybatis_version = '1.1.1'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version")

//        Kotlin整合SpringBoot的默认无参构造函数,默认把所有的类设置open类插件
        classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version")
        classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version")
    }
}

apply plugin: 'kotlin'
apply plugin: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#kotlin-spring-compiler-plugin
apply plugin: 'org.springframework.boot'
apply plugin: "kotlin-jpa"  //https://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell
jar {
    baseName = 'chapter11-6-7-service'
    version = '0.1.0'
}
repositories {
    mavenCentral()
}


dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
    compile("org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}")

    compile "org.springframework.boot:spring-boot-starter-data-jpa:$spring_boot_version"
    compile "org.springframework.boot:spring-boot-starter-data-rest:$spring_boot_version"
    compile "mysql:mysql-connector-java:$mysql_version"


    testCompile "org.springframework.boot:spring-boot-starter-test:$spring_boot_version"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"

}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

创建对象User


import javax.persistence.*

/**
 * Created by http://quanke.name on 2018/1/10.
 */

@Entity
data class User(
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        var id: Long = -1,
        @Column(nullable = false)
        var username: String = "",

        var password: String = ""


)

创建UserRepository


import name.quanke.kotlin.chaper11_6_7.entity.User
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.data.rest.core.annotation.RepositoryRestResource

@RepositoryRestResource(path="user")
interface UserRepository : JpaRepository<User, Long> {

    fun findByUsername(username: String): List<User>

    fun findByUsernameAndPassword(username: String, password: String?): User

    @Query("from User u where u.username=:username")
    fun findUser(@Param("username") username: String): User

}

配置

application.yml文件中增加

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

启动

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication


/**
 * Created by http://quanke.name on 2018/1/9.
 */

//参考:http://blog.csdn.net/soul_code/article/details/54108105

@SpringBootApplication
class Application

fun main(args: Array<String>) {
    SpringApplication.run(Application::class.java, *args)
}

测试

访问 127.0.0.1:8080/user

返回:

{
    "_embedded": {
        "users": [
            {
                "username": "http://gof.quanke.name",
                "password": "1111",
                "_links": {
                    "self": {
                        "href": "http://127.0.0.1:8083/user/73"
                    },
                    "user": {
                        "href": "http://127.0.0.1:8083/user/73"
                    }
                }
            },
            {
                "username": "java.quanke.name",
                "password": "12",
                "_links": {
                    "self": {
                        "href": "http://127.0.0.1:8083/user/74"
                    },
                    "user": {
                        "href": "http://127.0.0.1:8083/user/74"
                    }
                }
            },
            {
                "username": "test.quanke.name",
                "password": "aa",
                "_links": {
                    "self": {
                        "href": "http://127.0.0.1:8083/user/75"
                    },
                    "user": {
                        "href": "http://127.0.0.1:8083/user/75"
                    }
                }
            },
            {
                "username": "es.quanke.name",
                "password": "12",
                "_links": {
                    "self": {
                        "href": "http://127.0.0.1:8083/user/76"
                    },
                    "user": {
                        "href": "http://127.0.0.1:8083/user/76"
                    }
                }
            },
            {
                "username": "as.quanke.name",
                "password": "12",
                "_links": {
                    "self": {
                        "href": "http://127.0.0.1:8083/user/77"
                    },
                    "user": {
                        "href": "http://127.0.0.1:8083/user/77"
                    }
                }
            },
            {
                "username": "vertx.quanke.name",
                "password": "12",
                "_links": {
                    "self": {
                        "href": "http://127.0.0.1:8083/user/78"
                    },
                    "user": {
                        "href": "http://127.0.0.1:8083/user/78"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://127.0.0.1:8083/user{?page,size,sort}",
            "templated": true
        },
        "profile": {
            "href": "http://127.0.0.1:8083/profile/user"
        },
        "search": {
            "href": "http://127.0.0.1:8083/user/search"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 6,
        "totalPages": 1,
        "number": 0
    }
}

访问 127.0.0.1:8083/user/73

注意: 73 是user id 根据自己的实际情况测试

返回:

{
    "username": "http://gof.quanke.name",
    "password": "1111",
    "_links": {
        "self": {
            "href": "http://127.0.0.1:8083/user/73"
        },
        "user": {
            "href": "http://127.0.0.1:8083/user/73"
        }
    }
}

Spring Data REST 能做的事情很多,这篇文章先介绍到这里,先在这里埋个坑,之后会出更加详细的文章说Spring Data REST。

更多Spring Boot 和 kotlin相关内容

欢迎关注《Spring Boot 与 kotlin 实战》

参考

上一篇下一篇

猜你喜欢

热点阅读