java - spring-data-jpa @RestController @Repository 可选的奇怪行为

标签 java spring-boot option-type spring-restcontroller spring-repositories

我的@RestController

@GetMapping("/projects/{project_id}")
public Project getProjectById(@PathVariable(value = "project_id") UUID projectId) {
    return projectRepository.findById(projectId).orElseThrow(Util.notFound(projectId));
}

我的项目存储库是

@Repository
public interface ProjectRepository extends PagingAndSortingRepository<Project, UUID> {
}
public class Util {

    static Supplier<ResourceNotFoundException> notFound(UUID msg) {
        log.error(msg + " not found");
        return () -> new ResourceNotFoundException(msg + " not found");
    }
}

当我执行 GET {{host}}/projects/{{projId1}} 时,它会返回结果。然而,在日志中,它显示。

org.hibernate.SQL: select project0_.id as id1_4_0_, proje...  
o.h.type.descriptor.sql.BasicBinder: binding parameter [1] as [OTHER]  
ERROR: projectId not found . 

为什么orElseThrow总是在数据返回后执行?

最佳答案

修复你的Util

public class Util {
    static Supplier<ResourceNotFoundException> notFound(UUID msg) {
        return () -> {
            log.error(msg + " not found");
            return new ResourceNotFoundException(msg + " not found");
        };
    }
}

只有在实际抛出时才需要记录。

关于java - spring-data-jpa @RestController @Repository 可选的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56180844/

相关文章:

swift - 在 Swift 中调用可选函数

c# - 如果我在源代码上插入注释,它的二进制文件 MD5 会被更改吗?

java - 已建立连接但没有互联网的 UnknownHostException 超时

java - 包中的 Jacoco 类与事务方法不匹配

json - Spring 4 mvc REST XML 和 JSON 响应

ios - init! 的目的是什么?失败的初始化器?

java - 如何在谷歌应用引擎中执行大型查询?

java - 未显示 XML header

java - 实体数据类型必须与 JPA 存储库中的参数相同吗?

java - 泛型方法返回 'nothing'但不能返回null时返回什么?