简体   繁体   中英

PUT request to MongoDB via Java (with spring boot) inserts, instead of updates. Why does this happen?

I am not an expert in this so forgive any bad coding you may see in my project.

I building an app that lets the user create, read, update and delete ingredients and recipes which can then be used to make grocery lists. I am using Java with Spring Boot as the back end, MongoDB as the database and AngularJS as the front end. Creating, reading and deleting works just fine, but updating is not working for the recipe collection in the DB.

When I run the back end server and use postman to make a PUT request to update a recipe, the recipe in the body of the request gets inserted as a new document even though I specify the ObjectID of an existing recipe. I don't have this problem with the ingredient collection. I suspect it has something to do with the fact that the recipe documents are nested JSON objects, while the ingredient documents are not, but I may be wrong.

I have uploaded the Java back end of the project to gitHub: https://github.com/firo8/grocerylists/tree/master/grocery-list/src/main/java/com/firo/grocerylist .

I have searched everywhere and can't find anyone else that has this problem with an answer I can understand. Eg the following question seems to be similar, but the answer is not helpful: PUT makes POST instead of updating value in Spring Boot

what can I do to fix this?

Thanks in advance

Please refer to below docs on spring mongo db support page. https://docs.spring.io/spring-data/mongodb/docs/3.0.3.RELEASE/reference/html/#mongo.core

I see that you need to connect the database by creating a mongo client and use mongo template to insert/update/get/delete from mongo db.

Follow the docs to get it working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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