gpt4 book ai didi

java - spring boot mongodb审计报错

转载 作者:可可西里 更新时间:2023-11-01 09:17:38 24 4
gpt4 key购买 nike

我正在尝试在我的 spring boot 应用程序中配置 mongodb 审计,但在尝试保留我的域类时出现此错误:

java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class com.example.hateoasapi.domain.Post!

来自这里的文档 https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#auditing说所有这些配置都足够了,但我不知道为什么它在我的项目中不起作用。有人可以帮助我吗?

我的 mongodb 配置类:

    package com.example.hateoasapi.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
import org.springframework.data.mongodb.core.MongoTemplate;

import com.mongodb.MongoClient;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.util.Collection;
import java.util.Collections;


@Configuration
@EnableMongoAuditing
@EnableMongoRepositories(value = "com.example.hateoasapi.repository")
public class MongoConfig extends AbstractMongoConfiguration {

@Value("${spring.data.mongodb.database}")
private String databaseName;

@Value("${spring.data.mongodb.host}")
private String databaseHost;

@Value("${spring.data.mongodb.port}")
private Integer databasePort;

@Override
protected String getDatabaseName() {
return this.databaseName;
}

@Bean
@Override
public MongoClient mongoClient() {
return new MongoClient(databaseHost, databasePort);
}

@Bean
public MongoTemplate mongoTemplate() {
return new MongoTemplate(mongoClient(), databaseName);
}

@Override
protected Collection<String> getMappingBasePackages() {
return Collections.singleton("com.example.hateoasapi.domain");
}
}

AuditorAware 实现:

package com.example.hateoasapi.config;


import com.example.hateoasapi.domain.User;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class SecurityAuditor implements AuditorAware<User> {

@Override
public Optional<User> getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

if (authentication == null || !authentication.isAuthenticated()) {
return null;
}

return Optional.of((User) authentication.getPrincipal());
}
}

还有我的领域类:

package com.example.hateoasapi.domain;

import javax.persistence.Id;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;

import org.joda.time.DateTime;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.hateoas.ResourceSupport;
import com.fasterxml.jackson.annotation.JsonCreator;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;

import java.io.Serializable;
import java.util.List;

import com.example.hateoasapi.controller.*;

@Getter
@Setter
@ToString
@Document
public class Post extends ResourceSupport implements Serializable {

@Id
@Field(value = "_id")
private String objectId;

@DBRef
private List<Comment> comments;

@DBRef
private User author;

@NotBlank
private String body;

@NotBlank
private String title;

private String categoryId;

@NotEmpty(message = "Tags cannot be empty")
private List<PostTag> tags;

@CreatedDate
private DateTime createdDate;

@LastModifiedDate
private DateTime lastModifiedDate;

@CreatedBy
private User createdBy;

private Long views;
private List<PostRating> likes;
private List<PostRating> dislikes;


@JsonCreator
public Post() {}

public Post(String title, String body) {
this.body = body;
this.title = title;
}

public Post(User author, String body, String title, String categoryId, List<PostTag> tags) {
this.author = author;
this.body = body;
this.title = title;
this.categoryId = categoryId;
this.tags = tags;
}

public void addLinks() {
this.add(linkTo(methodOn(PostController.class).getAllPosts(null)).withSelfRel());
}
}

最佳答案

我用下一个配置解决了这个问题:

@Configuration
@EnableMongoRepositories(basePackages = "YOUR.PACKAGE")
@EnableMongoAuditing
public class MongoConfig extends AbstractMongoConfiguration {

@Value("${spring.data.mongodb.host}")
private String host;

@Value("${spring.data.mongodb.port}")
private Integer port;

@Value("${spring.data.mongodb.database}")
private String database;

@Override
public MongoClient mongoClient() {
return new MongoClient(host, port);
}

@Override
protected String getDatabaseName() {
return database;
}

@Bean
public MongoTemplate mongoTemplate() throws Exception {
return new MongoTemplate(mongoDbFactory(), mappingMongoConverter());
}

@Bean
public MongoDbFactory mongoDbFactory() {
return new SimpleMongoDbFactory(mongoClient(), database);
}
}

只需使用 MongoTemplate(MongoDbFactory mongoDbFactory, @Nullable MongoConverter mongoConverter) 的构造函数为 MongoTemplate 添加 bean

关于java - spring boot mongodb审计报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46863819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com