本文介绍了spring-data-jpa @RestController @Repository可选的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的@RestController

My @RestController

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

我的projectRepository是

My projectRepository is

@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}}时,它返回结果.但是,它在日志中显示.

When i do GET {{host}}/projects/{{projId1}}, it return the result. However, in the log, it show .

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 .

返回数据后如何执行ElseThrow?

How is it orElseThrow always got executed, after the data has been returned?

推荐答案

修复您的Util

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

仅在实际抛出的情况下才需要登录.

You need to log only in case you actually throw it.

这篇关于spring-data-jpa @RestController @Repository可选的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 18:01