春季启动-添加自定义MongoDB Converter

时间:2018-07-24 11:08:25

标签: java mongodb spring-boot jersey converter

我已经一遍又一遍地搜索,发现没有一种解决方案对我有用,或者也许我太笨了,因为我刚刚开始使用Spring。

我正在将Jersey与Spring Boot配合使用。

我有以下域类:

@Document(collection = "Videos")
@Getter @ToString @Builder
public class Video {
    @Id
    private ObjectId id;
    private String titulo;
    private String url;
    private String miniatura;
}


@Document(collection = "Categorias")
@Getter() 
@ToString
public class Categoria {
    private String nombre;
    private Video[] videos;
}

现在,我的数据库中有这样的文档:

{
    "_id" : ObjectId("5b56e9fbb8a2f4ec7805c73e"),
    "nombre" : "Tus dudas de salud",
    "videos" : [ 
        ObjectId("5b56e556b8a2f4ec7805c52b"), 
        ObjectId("5b56e556b8a2f4ec7805c52b"), 
        ObjectId("5b56e556b8a2f4ec7805c52b")
    ]
}

我的存储库是空的,因为目前我只需要执行.findAll()

package es.sanitas.hos.portalpaciente.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import es.sanitas.hos.portalpaciente.service.vo.Categoria;

@Repository
public interface CategoriasRepository extends MongoRepository<Categoria, String> {
}

我对Mongo的配置在一个application.yml文件中:

spring:

data:
    mongodb:
      uri: mongodb://localhost:27017/test

我的pom.xml很有用:

<?xml version="1.0" encoding="utf-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>es.sanitas.hos</groupId>
    <artifactId>PortalPacienteRSHQ</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

现在的问题是,当我尝试从数据库中获取Categorias时。它给了我一个例外,说no converter found capable of converting String to Video

我知道这是因为数据是从数据库中检索的,是具有类似JSON格式的字符串,并且无法将其分配给Video的{​​{1}}属性。

我无法使任何Converters示例都可以解决其他问题。

有人可以帮我成功实现covnerter,以便我可以处理视频字符串吗?

谢谢。

0 个答案:

没有答案
相关问题