Spring boot ClassCastException 到 Jackson

问题描述 投票:0回答:1

我使用 MongoDB 制作简单的 Spring Boot 应用程序,并在我的控制器类中面临这个问题:

org.springframework.beans.factory.BeanCreationException:创建名称为“userController”的bean时出错

引起:org.springframework.beans.BeanInstantiationException:无法实例化[com.pypek.testtaskapi.controller.UserController]:构造函数抛出异常

引起:org.codehaus.groovy.runtime.typehandling.GroovyCastException:无法将类“com.fasterxml.jackson.databind.ObjectMapper”的对象“com.fasterxml.jackson.databind.ObjectMapper@5f4d427e”转换为类“com.fasterxml.jackson.databind.ObjectMapper@5f4d427e”。 pypek.testtaskapi.service.UserService' 2023-10-27T18:25:15.334876863Z 在 org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:424) ~[groovy-4.0.15.jar!/:4.0.15] 2023-10-27T18:25:15.334879707Z 在 org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:335) ~[groovy-4.0.15.jar!/:4.0.15] 2023-10-27T18:25:15.334882393Z 在 org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:255) ~[groovy-4.0.15.jar!/:4.0.15] 2023-10-27T18:25:15.335049101Z 在 org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321) ~[groovy-4.0.15.jar!/:4.0.15]

我的代码控制器:

package com.pypek.testtaskapi.controller

import com.pypek.testtaskapi.model.User
import com.pypek.testtaskapi.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/user")
class UserController {

@Autowired
UserService service

UserController(UserService service) {
    this.service = service
}

@GetMapping("/{id}")
def getUser(@PathVariable String id) {
    return service.getUser(id)
}

@PatchMapping
def updateUser(@RequestBody User user){
    service.updateUser(user)
}

@PostMapping
def addUser(@RequestBody User user){
    service.addUser(user)
}

@DeleteMapping("/{id}")
def deleteUser(@PathVariable String id){
    service.deleteUser(id)
}
}

服务:

package com.pypek.testtaskapi.service

import com.pypek.testtaskapi.exceptions.UserNotFoundExceptions
import com.pypek.testtaskapi.model.User
import com.pypek.testtaskapi.repository.UserRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class UserService {

    UserRepository repository

    @Autowired
    UserService(UserRepository repository) {
        this.repository = repository
    }

    def getUser(String id) {
        return repository.findById(id).orElseThrow(() -> new UserNotFoundExceptions(id))
    }

    String addUser(User user) {
        return repository.save(user).getId()
    }

    def updateUser(User user) {
        def id = user.getId()
        if (checkIfUserExist(id)){
            return repository.save(user)
        } else throw new UserNotFoundExceptions(id)
    }

    def deleteUser(String id) {
        if (checkIfUserExist(id)) {
            repository.deleteById(id)
        } else throw new UserNotFoundExceptions(id)
    }

    def checkIfUserExist(String id) {
        return repository.existsById(id)
    }
}

知道为什么 Spring boot 尝试转换它吗???

spring spring-boot groovy jackson
1个回答
0
投票

只需从依赖项列表中删除“org.springframework.boot:spring-boot-devtools”即可。谢谢来自 ClassCastException: with unnamed module of loader 'app': Spring Data Redis

© www.soinside.com 2019 - 2024. All rights reserved.