Spring Boot + MongoDB Id 查询 [英] Spring Boot + MongoDB Id query

查看:200
本文介绍了Spring Boot + MongoDB Id 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结合 MongoDB 的 Spring Boot 应用程序作为持久层.我有以下结构:

I have a Spring Boot application combined with MongoDB as the persistance layer. I have the following structure:

public class Resource {

@Id
public String Id;
...
}

我也有一个 ResourceRepository:

I also have a ResourceRepository:

@RepositoryRestResource(collectionResourceRel = "resources", path = "resources")
public interface ResourceRepository extends MongoRepository<Resource, String> {
     Resource findById(@Param("Id")String Id);
}

我在网上找到了一种在执行 GET 请求时在 JSON 中返回 id 属性的方法,例如 http://localhost:8080/resources/ 就是把id属性改成Id(大写的i).事实上,如果属性是小写的,我不会得到一个 id 字段,但是如果我将它更改为大写,那么我就会得到它.出于某种原因,我需要取回 id 属性,所以我使用了大写的 i.到目前为止,一切都很好.

I found online that a way to have the id property returned in the JSON when you perform a GET request like http://localhost:8080/resources/ is to change the id property to Id (uppercase i). Indeed, if the property is lowercase, I don't get back an id field but if I change it to uppercase then I get it. For a reason, I need to get back the id property so I used the uppercase i. So far, so good.

但是,当我尝试执行存储库中包含的查询 findById 时,出现异常:

However, when I tried to execute the query findById included in my repository I get an exception:

org.springframework.data.mapping.context.InvalidPersistentPropertyPath: No property id found on app.model.Resource!

如果我将 Id 属性更改为 id(小写 i),我可以成功执行/resources/search/findById?id=... GET 请求.

If I change the Id property to id (lowercase i) I can execute successfully the /resources/search/findById?id=... GET request.

我尝试使用查询创建自定义控制器,该查询根据给定的 id 查找并返回资源:

I tried creating a custom controller with a query that finds and returns a Resource based on the id that is given:

@Controller
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    MongoOperations mongoOperations;

    @RequestMapping(value="/findById/{resourceId}/", method= RequestMethod.GET)
    @ResponseBody
    public Resource findByResourceId(@PathVariable("resourceId") String resourceId) {
        Resource resource = mongoOperations.findOne(query(Criteria.where("Id").is(resourceId)), Resource.class,"DOJ");
    }
}

但我收到同样的错误:

org.springframework.data.mapping.context.InvalidPersistentPropertyPath: No property id found on app.model.Resource!

关于如何在 JSon 中显示 id 属性并能够 findById 有什么想法吗?

Any idea on how to both have the id property displyed in the JSon and be able to findById?

推荐答案

好吧,我自己找到了答案.切换回小写 id 以便 findById 工作并将以下类添加到项目中:

Well, I found the answer myself. Switch back to lowercase id so findById works and add the following class to the project:

@Configuration
public class SpringDataRestConfiguration extends RepositoryRestConfigurerAdapter  {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(Resource.class);
    }
}

顾名思义,此配置使 Resource 类对象在 JSON 中公开其 id.

As the name of the method suggests, this configuration makes Resource class objects to expose their ids in JSON.

UPDATE:如果你使用的是最新或相对最新版本的spring-boot,RepositoryRestConfigurerAdapter类已被弃用,java-doc建议使用接口RepositoryRestConfigurer代码>直接.

UPDATE: If you are using the latest or relatively latest version of spring-boot, the RepositoryRestConfigurerAdapter class has been deprecated, and the java-doc suggests to use the interface RepositoryRestConfigurer directly.

所以你的代码应该是这样的:

So your code should look like this:

@Configuration
public class SpringDataRestConfiguration implements RepositoryRestConfigurer  
...

这篇关于Spring Boot + MongoDB Id 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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