Spring Data Mongo找不到Enum的PersistentEntity [英] Spring Data Mongo cannot find PersistentEntity for Enum

查看:1272
本文介绍了Spring Data Mongo找不到Enum的PersistentEntity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我在此处找到了一个相关问题,但仅有2个答案彼此矛盾,并且没有足够的信息来解决我的用例。

I found a related question here, but the only 2 answers contradict each other, and there was not enough information to address my use case.

我正在尝试使用Spring Data Mongo从集合中加载记录。这些记录中的字段之一是枚举,其定义如下:

I am trying to use Spring Data Mongo to load records from a collection. One of the fields within those records is an Enum, defined as such:

@AllArgsConstructor
@Getter
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum Action {
    APPROVED("Approved"),
    SAVED("Saved"),
    CORRECTED("Corrected");

    private String name;

    @JsonCreator
    static Action findValue(@JsonProperty("name") String name) {
        return Arrays.stream(Action.values()).filter(v -> v.name.equals(name)).findFirst().get();
    }
}

这应根据以下定义定义要序列化和反序列化的枚举JSON表示形式: { name: Saved}

This should define enums to be serialized and deserialized according to a JSON representation: {"name": "Saved"} for example.

Jackson似乎工作正常,因为我抛出了一个API调用,并告诉它期望 Action 类型,并且它读取枚举没有任何问题。 $ b

Jackson seems to be working fine, since I threw an API call at it and told it to expect an Action type, and it read the enum without any issues.

public void save(@RequestBody @Valid Action action) {
    System.out.println(action.getName());
} // successfully prints the name of whatever Action I give

但是,当我尝试使用Spring Data Mongo读取带有Action字段的对象,我得到以下信息:

However, when I try to read an object with an Action field using Spring Data Mongo, I get the following:

Expected to read Document Document{{name=Corrected}} into type class package.structure.for.some.proprietary.stuff.constants.Action but didn't find a PersistentEntity for the latter!

所以我在想Spring Data Mongo不能为这些枚举做任何正面或反面原因。但是我不确定如何帮助它将其注册为PersistentEntity。我的Spring Boot应用程序的主类位于软件包 package.structure.for.some.proprietary.stuff 中,并带有以下注释:

So I'm thinking Spring Data Mongo just can't make heads or tails of these enums for whatever reason. But I'm not sure how to help it register that as a PersistentEntity. The main class of my Spring Boot app is in package package.structure.for.some.proprietary.stuff and is annotated as such:

@ComponentScan("package.structure")
@EnableTransactionManagement
@EnableAutoConfiguration
@SpringBootApplication

我要读取的对象特别是由此POJO定义的:

The object in particular I'm trying to read is defined by this POJO:

import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import lombok.Data;
import lombok.NonNull;

import package.structure.for.some.proprietary.stuff.constants.Action;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "timeStamp",
    "action",
})
@Data
@Document(collection = "sample_coll")
public class Sample {
    @Id
    @JsonIgnore
    private String id = null;

    @JsonProperty("timeStamp")
    @NonNull
    private Date timeStamp;
    @JsonProperty("action")
    @NonNull
    private Action action;
}

,并从集合中使用MongoRepository进行查询:

and is queried from the collection with a MongoRepository:

public interface SampleRepository extends MongoRepository<Sample, String> {

}

使用 SampleRepository.findAll( );

所以我的大问题是,如何让Spring Data Mongo将该枚举Action识别为PersistentEntity?

So my big question is, how do I get Spring Data Mongo to recognize this enum Action as a PersistentEntity?

推荐答案

尝试@Enumerated

Try @Enumerated

@Enumerated
@JsonProperty("action")
@NonNull
private Action action;

这篇关于Spring Data Mongo找不到Enum的PersistentEntity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆