尝试/捕获循环在IllegalStateException上未触发捕获语句。

huangapple go评论59阅读模式
英文:

Try/Catch Loop not triggering catch statement on IllegalStateException

问题

以下是您要翻译的内容:

我正在编写一个Spring Boot应用程序,其中有一个唯一的对象被持久化在数据库中,并可以通过以下CRUD Repository进行检索:

@Repository
public interface DefaultSurveyRepository extends CrudRepository<DefaultSurvey, UUID> {

    public static final UUID specialId = UUID.fromString("123e4567-e89b-12d3-a456-556642440000");

    default DefaultSurvey findSpecialInstance() {
        return findById(specialId).orElseThrow(() -> new IllegalStateException("No Default Survey Found"));
    }
}

在我的服务类构造函数中,我尝试查找对象的specialInstance,如果失败了我就创建一个新版本。代码如下:

@Service
@Transactional
public class AdminManagement {
    
    private final DefaultSurveyRepository repo;

    @Autowired
    public AdminManagement(DefaultSurveyRepository repo) {
        
        //检查defaultSurvey是否存在。
        try{
            repo.findSpecialInstance();
        }
        catch(IllegalStateException e){
            repo.save(new DefaultSurvey(UUID.fromString("123e4567-e89b-12d3-a456-556642440000"), "[]"));
        }

        this.repo = Objects.requireNonNull(repo);
    }
}

问题是它没有捕捉到IllegalStateException并崩溃,如果对象不存在的话。我已经尝试设置断点进行调试,但它在断点到达之前就崩溃了。也许我没有正确地进行调试,但我不明白为什么它不能正常工作。非常感谢您的所有帮助!

英文:

I am writing a spring boot application and have a unique object which is persisted in a database, and retrieved by the following CRUD Repository:

@Repository
public interface DefaultSurveyRepository extends CrudRepository &lt;DefaultSurvey, UUID&gt; {

    public static final UUID specialId = UUID.fromString(&quot;123e4567-e89b-12d3-a456-556642440000&quot;);


    default DefaultSurvey findSpecialInstance() {
        return findById(specialId).orElseThrow(() -&gt; new IllegalStateException(&quot;No Default Survey Found&quot;));
    }
}

In my service class constructor, I try to find the specialInstance of the object, and if this fails I create a new version. The code is the following:

@Service
@Transactional
public class AdminManagement {
    
    private final DefaultSurveyRepository repo;

    @Autowired
    public AdminManagement(DefaultSurveyRepository repo) {
        
        //Check if defaultSurvey exists.
        try{
            repo.findSpecialInstance();
        }
        catch(IllegalStateException e){
            repo.save(new DefaultSurvey(UUID.fromString(&quot;123e4567-e89b-12d3-a456-556642440000&quot;), &quot;[]&quot;));
        }

        this.repo = Objects.requireNonNull(repo);
    }
.
.
.

The problem is that it does not catch the IllegalStateException and crashes, if the object does not exist. I have tried setting breakpoints to debug, but it crashes before the breakpoint is reached. Maybe I am not debugging correctly, but I do not understand why it won't work.
Any and all help is appreciated!

答案1

得分: 0

正如评论中提到的,最好实现CommandLineRunner。另一个“简单”的选项是将有关存储库初始化详细信息留在@PostConstruct中:

private final DefaultSurveyRepository repo;

@Autowired
public AdminManagement(DefaultSurveyRepository repo) {
    this.repo = repo;
}

@PostConstruct
private void postConstruct() {
    try {
        repo.findSpecialInstance();
    } catch (IllegalStateException e) {
        repo.save(new DefaultSurvey(UUID.fromString("123e4567-e89b-12d3-a456-556642440000"), "[]"));
    }
    this.repo = Objects.requireNonNull(repo);
}

只需记住,Java EE注解在Java 9中已被弃用并在Java 11中被移除,因此您必须将其添加到Maven的pom.xml中:

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>
英文:

As mentioned in the comments, it would be better to implement CommandLineRunner. Another "simple" option is to leave the initialization details about your repo in @PostConstruct:

private final DefaultSurveyRepository repo;

@Autowired
public AdminManagement(DefaultSurveyRepository repo) {
    this.repo = repo;
}

@PostConstruct
private void postConstruct() {
    try{
        repo.findSpecialInstance();
    } catch(IllegalStateException e){
        repo.save(new DefaultSurvey(UUID.fromString(&quot;123e4567-e89b-12d3-a456-556642440000&quot;), &quot;[]&quot;));
}
    this.repo = Objects.requireNonNull(repo);
}

Just remember that Java EE annotations have been deprecated in Java9 and removed in Java 11 so you must add that to your Maven pom.xml:

&lt;dependency&gt;   
    &lt;groupId&gt;javax.annotation&lt;/groupId&gt;
    &lt;artifactId&gt;javax.annotation-api&lt;/artifactId&gt;
    &lt;version&gt;1.3.2&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年8月21日 01:34:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63510411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定