Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

Error in linking spring boot and postgreSQL(kotlin)

I want to pass a table from intellij to postgreSQL using spring boot. The language used is Kotlin and it is written in gradle.

Information arrives until postman, but not postgreSQL. Also, no error occurs, so I don't know the cause any more.

Below is my code and dependency.

application.properties

spring.datasource.url=jdbc:postgresql://localhost:5432/spring
spring.datasource.username=postgres
spring.datasource.password=1234

UserInfo.kt

import javax.persistence.*
@Entity
data class UserInfo(
    @Id val id: String,
    var pw: String,
    var name: String,
    var birth: String
)

Usercontroller.kt

import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
import org.springframework.web.bind.annotation.*
import javax.annotation.PostConstruct

@RestController
@EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class])
class UserController {

    private val userMap: MutableMap<String, UserInfo> = mutableMapOf()

    @PostConstruct
    fun init() {
   

        userMap["id1"] = UserInfo("id1", "password1", "Alice", "1001")
        userMap["id2"] = UserInfo("id2", "password2", "Rabit", "1101")
        userMap["id3"] = UserInfo("id3", "password3", "Card", "1201")
    }


    @GetMapping(path = ["/user/{id}"])
    fun getUserInfo(@PathVariable("id") id: String) = userMap[id]

    @GetMapping(path = ["user/all"])
    fun getUserInfoAll() = ArrayList<UserInfo>(userMap.values)


    @PutMapping(path = ["/user/{id}"])
    fun putUserInfo(@PathVariable("id") id: String, @RequestParam("pw") pw: String,
                    @RequestParam("name") name: String, @RequestParam("birth") birth: String) {

        val userInfo = UserInfo(id,pw, name, birth)
        userMap[id] = userInfo
    }


    @PostMapping(path = ["/user/{id}"])
    fun postUserInfo(@PathVariable("id") id: String, @RequestParam("pw") pw: String,
                     @RequestParam("name") name: String, @RequestParam("birth") birth: String){
        val userInfo = userMap[id]
        userInfo?.pw = pw
        userInfo?.name = name
        userInfo?.birth = birth
    }

    @DeleteMapping(path = ["/user/{id}"])
    fun deleteUserInfo(@PathVariable("id") id: String) = userMap.remove(id)

}

UserRepository.kt

import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Repository


@Repository
interface UserRepository : ReactiveCrudRepository<UserInfo, String

gradle is in the attachment

I think there may be a problem in UserRepository.kt, but I can't find it because there is no error.

Thank you for reading the article. I wish you a nice day.

enter image description here

question from:https://stackoverflow.com/questions/65849943/error-in-linking-spring-boot-and-postgresqlkotlin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems that you don't actually call save() method of your UserRepository

Try injecting it first:

@Autowired
private lateinit var userRepository: UserRepository

And inside putUserInfo()

    val userInfo = UserInfo(id,pw, name, birth)
    userRepository.save(userInfo)

Pay attention, that you will recieve Mono as a result of save()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...