From 97c666e8325cbdea487ba4d7e1251261a9e82ca7 Mon Sep 17 00:00:00 2001 From: Chantha Date: Fri, 8 May 2020 17:16:54 +0700 Subject: [PATCH] error URI templete ID --- .../kotlin/com/chantha/mini/controller/UserController.kt | 9 ++++++++- mini/src/main/kotlin/com/chantha/mini/dto/User.kt | 2 ++ mini/src/main/kotlin/com/chantha/mini/repo/UserRepo.kt | 2 ++ .../main/kotlin/com/chantha/mini/service/UserService.kt | 8 ++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/mini/src/main/kotlin/com/chantha/mini/controller/UserController.kt b/mini/src/main/kotlin/com/chantha/mini/controller/UserController.kt index c9274e0..735d524 100644 --- a/mini/src/main/kotlin/com/chantha/mini/controller/UserController.kt +++ b/mini/src/main/kotlin/com/chantha/mini/controller/UserController.kt @@ -2,6 +2,7 @@ package com.chantha.mini.controller import com.chantha.mini.dto.UserDto import com.chantha.mini.service.UserService +import com.fasterxml.jackson.annotation.JsonRootName import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Controller import org.springframework.web.bind.annotation.GetMapping @@ -11,14 +12,20 @@ import org.springframework.web.bind.annotation.RestController @RestController class UserController @Autowired constructor(private val userService: UserService){ - @GetMapping("/api/user/{firstName}/{lastName}") + @GetMapping("/api/user/name/{firstName}/{lastName}") fun findByFirstNameAndLastName(@PathVariable firstName:String ?="",@PathVariable lastName:String?=""):UserDto{ println(lastName+" "+firstName) return userService.findByFirstNameAndLastName(firstName,lastName) } + @GetMapping("/api/user") fun findAllRecord():List{ return userService.findAllRecord().orEmpty() } + + @GetMapping("/api/user/id/{id}") + fun findById(@PathVariable Id:Long ):UserDto{ + return userService.findById(Id) + } } \ No newline at end of file diff --git a/mini/src/main/kotlin/com/chantha/mini/dto/User.kt b/mini/src/main/kotlin/com/chantha/mini/dto/User.kt index 66f99e8..459112a 100644 --- a/mini/src/main/kotlin/com/chantha/mini/dto/User.kt +++ b/mini/src/main/kotlin/com/chantha/mini/dto/User.kt @@ -1,6 +1,7 @@ package com.chantha.mini.dto import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.annotation.JsonRootName import com.fasterxml.jackson.databind.annotation.JsonDeserialize import org.intellij.lang.annotations.Identifier import java.util.* @@ -10,6 +11,7 @@ import javax.persistence.* @Entity @Table(name="tb_user") +@JsonRootName("Person") data class UserDto ( @Id diff --git a/mini/src/main/kotlin/com/chantha/mini/repo/UserRepo.kt b/mini/src/main/kotlin/com/chantha/mini/repo/UserRepo.kt index 4300db9..5b27ca7 100644 --- a/mini/src/main/kotlin/com/chantha/mini/repo/UserRepo.kt +++ b/mini/src/main/kotlin/com/chantha/mini/repo/UserRepo.kt @@ -12,4 +12,6 @@ interface UserRepo : JpaRepository { fun findByFirstnameAndLastname(firstName:String?="",lastName:String?=""):UserDto @Query("SELECT * FROM user_dto",nativeQuery = true) fun findAllRecord():List + @Query("SELECT * FROM user_dto WHERE id=?1",nativeQuery = true) + fun findById(Id :Long ?= 0):UserDto } \ No newline at end of file diff --git a/mini/src/main/kotlin/com/chantha/mini/service/UserService.kt b/mini/src/main/kotlin/com/chantha/mini/service/UserService.kt index ed0a8ca..fe2ace9 100644 --- a/mini/src/main/kotlin/com/chantha/mini/service/UserService.kt +++ b/mini/src/main/kotlin/com/chantha/mini/service/UserService.kt @@ -2,8 +2,11 @@ package com.chantha.mini.service import com.chantha.mini.dto.UserDto import com.chantha.mini.repo.UserRepo +import com.fasterxml.jackson.annotation.JsonFilter +import com.fasterxml.jackson.annotation.JsonRootName import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Service +import java.util.* @Service class UserService @Autowired constructor(private val userRepo: UserRepo) { @@ -11,7 +14,12 @@ class UserService @Autowired constructor(private val userRepo: UserRepo) { fun findByFirstNameAndLastName(firstName: String? = "", lastName: String? = ""):UserDto { return userRepo.findByFirstnameAndLastname(firstName,lastName) } + fun findAllRecord():List{ return userRepo.findAllRecord() } + + fun findById(Id : Long? = 0): UserDto { + return userRepo.findById(Id) + } } \ No newline at end of file