简体   繁体   English

如何使用springboot创建mongodb通用rest api?

[英]How to create mongodb generic rest api with springboot?

I'm creating an application to display different intresting data.我正在创建一个应用程序来显示不同的有趣数据。 I've been trying create generics REST controllers for several days.几天来,我一直在尝试创建 generics REST 控制器。 I need generic mongodb repository, which is used in generic service, which is used in generic controller:) You can see that schema on picture.我需要通用 mongodb 存储库,用于通用服务,用于通用 controller :) 您可以在图片上看到该架构。

包模式

Value.java值.java

    @Data
    @NoArgsConstructor
    public class Value implements Serializable {

    @Id
    String id;
    String description;
    Integer value;

    public Value(int value, String description) {
        this.description = description;
        this.value = value;
     }
}

SpeedValue.java速度值.java

@Data
@EqualsAndHashCode(callSuper = true)

@Document(collection = "speed_values")
public class SpeedValue extends Value {

    public SpeedValue(int value, String description) {
        super(value, description);
    }

}

CurrencyValue.java CurrencyValue.java

@EqualsAndHashCode(callSuper = true)
@Data

@Document(collection = "currency_values")
public class CurrencyValue extends Value {

    public CurrencyValue(int value, String description) {
        super(value, description);
    }
}

My generic repository:我的通用存储库:

@NoRepositoryBean
public interface GenericValueRepository<T extends Value> extends MongoRepository<T, String> {
}

SpeedValueRepository.java SpeedValueRepository.java

@Repository
public interface SpeedValueRepository extends GenericValueRepository<SpeedValue> {
}

And finally GenericControler.java最后是 GenericControler.java

@Controller
public class GenericController<T extends Value> {

    private GenericServiceImpl<T> service;

    public GenericController(GenericServiceImpl<T> speedValueService) {
        this.service = speedValueService;
    }

    @GetMapping
    public List<T> getCurrencyValue() {
        return service.findAll();
    }

    @PostMapping
    public String create(@RequestBody T json) {

        T created = this.service.save(json);
        return "Added to DB: \n" + created.toString();
    }

and controller which extends GenericController和扩展 GenericController 的 controller

@RestController
@RequestMapping("api/values/speed")
public class SpeedValueControllerREST extends GenericController<SpeedValue>{

    @Autowired
    public SpeedValueControllerREST(SpeedValueService speedValueService) {
        super(speedValueService);
    }
}

This code doesn't work, I've been trying with many versions, but result was NPE or code didn't compile.这段代码不起作用,我一直在尝试很多版本,但结果是 NPE 或代码没有编译。 I have to create many models, repositories, services like these above.我必须像上面那样创建许多模型、存储库、服务。 Can you resolve that problem?你能解决那个问题吗? Maybe I shoud do it in other way?也许我应该以其他方式做到这一点?

Finally, I solved this problem by:最后,我通过以下方式解决了这个问题:

https://stackoverflow.com/a/53406444/10474557 https://stackoverflow.com/a/53406444/10474557

That was exactly what i was looking for这正是我想要的

I did not understood your code, you are talking about generic, what i am assuming here is you are trying to create generic API for getting any/generic request (possibly JSON), save into MongoDB, Should be able to fetch the data with generic search query, update the data with generic selection criteria etc..我不明白你的代码,你在谈论通用,我在这里假设你正在尝试创建通用 API 以获取任何/通用请求(可能是 JSON),保存到 MongoDB,应该能够使用通用获取数据搜索查询,使用通用选择标准更新数据等。

I am not sure how @MongoRepository will help in this regards, I will explan how i have achieved this generic behavior for our MongoCrud service - Pseudo code, rest you have to learn and implement -我不确定@MongoRepository 在这方面将如何提供帮助,我将解释我如何为我们的 MongoCrud 服务实现这种通用行为 - 伪代码 rest 你必须学习和实施 -

  1. Create connection and get the MongoClient for CRUD Operation - Docs https://api.mongodb.com/java/3.0/com/mongodb/MongoClient.html为 CRUD 操作创建连接并获取 MongoClient - Docs https://api.mongodb.com/java/3.0/com/mongodb/MongoClient.ZFC35FDC70D5FC69D269883A822C7A3EZ

  2. Write Controller method to accept Request Body as String variable ( value would be json while making request ) to save, And validate if Document document = Document.parse(payload);编写 Controller 方法以接受请求正文作为字符串变量(发出请求时的值将是 json )保存,并验证Document document = Document.parse(payload); if a document, then Save to particular collection using the MongoClient instance.如果是文档,则使用 MongoClient 实例保存到特定集合。

  3. Write Controller method to accept Request Body as String variable ( value would be json while making request ), And validate it, and use MongoClient instance to query.编写 Controller 方法接受请求正文作为字符串变量(发出请求时的值为 json ),并验证它,并使用 MongoClient 实例进行查询。 Link for creating query json https://docs.mongodb.com/manual/tutorial/query-documents/用于创建查询的链接 json https://docs.mongodb.com/manual/tutorial/query-documents/

I hope this will help to reach your objective.我希望这将有助于实现您的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM