• springboot操作nosql的mongodb,或者是如何在mongodb官网创建服务器并进行操作


    第一步:在mongodb的官网里面创建云服务器

    点进去

    这是免费的,由于是一个项目只可以创建一个,这里我已经创建好了

    用本地的mongodb服务也是可以的

    第二步:点击connect,下载连接mongodb的软件:MongoDBCompass

    第三步:创建springboot项目

    3.1 、pom依赖:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <parent>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-parent</artifactId>
    8. <version>3.0.12-SNAPSHOT</version>
    9. <relativePath/> <!-- lookup parent from repository -->
    10. </parent>
    11. <groupId>dev.farhan</groupId>
    12. <artifactId>movies</artifactId>
    13. <version>0.0.1-SNAPSHOT</version>
    14. <name>movies</name>
    15. <description>a simple APIS relates to movies</description>
    16. <properties>
    17. <java.version>17</java.version>
    18. </properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.boot</groupId>
    22. <artifactId>spring-boot-starter-data-mongodb</artifactId>
    23. </dependency>
    24. <dependency>
    25. <groupId>org.springframework.boot</groupId>
    26. <artifactId>spring-boot-starter-web</artifactId>
    27. </dependency>
    28. <dependency>
    29. <groupId>org.springframework.boot</groupId>
    30. <artifactId>spring-boot-devtools</artifactId>
    31. <scope>runtime</scope>
    32. <optional>true</optional>
    33. </dependency>
    34. <dependency>
    35. <groupId>org.projectlombok</groupId>
    36. <artifactId>lombok</artifactId>
    37. <optional>true</optional>
    38. </dependency>
    39. <dependency>
    40. <groupId>org.springframework.boot</groupId>
    41. <artifactId>spring-boot-starter-test</artifactId>
    42. <scope>test</scope>
    43. </dependency>
    44. <!-- https://mvnrepository.com/artifact/me.paulschwarz/spring-dotenv -->
    45. <dependency>
    46. <groupId>me.paulschwarz</groupId>
    47. <artifactId>spring-dotenv</artifactId>
    48. <version>2.5.4</version>
    49. </dependency>
    50. </dependencies>
    51. <build>
    52. <plugins>
    53. <plugin>
    54. <groupId>org.springframework.boot</groupId>
    55. <artifactId>spring-boot-maven-plugin</artifactId>
    56. <configuration>
    57. <excludes>
    58. <exclude>
    59. <groupId>org.projectlombok</groupId>
    60. <artifactId>lombok</artifactId>
    61. </exclude>
    62. </excludes>
    63. </configuration>
    64. </plugin>
    65. </plugins>
    66. </build>
    67. <repositories>
    68. <repository>
    69. <id>spring-milestones</id>
    70. <name>Spring Milestones</name>
    71. <url>https://repo.spring.io/milestone</url>
    72. <snapshots>
    73. <enabled>false</enabled>
    74. </snapshots>
    75. </repository>
    76. <repository>
    77. <id>spring-snapshots</id>
    78. <name>Spring Snapshots</name>
    79. <url>https://repo.spring.io/snapshot</url>
    80. <releases>
    81. <enabled>false</enabled>
    82. </releases>
    83. </repository>
    84. </repositories>
    85. <pluginRepositories>
    86. <pluginRepository>
    87. <id>spring-milestones</id>
    88. <name>Spring Milestones</name>
    89. <url>https://repo.spring.io/milestone</url>
    90. <snapshots>
    91. <enabled>false</enabled>
    92. </snapshots>
    93. </pluginRepository>
    94. <pluginRepository>
    95. <id>spring-snapshots</id>
    96. <name>Spring Snapshots</name>
    97. <url>https://repo.spring.io/snapshot</url>
    98. <releases>
    99. <enabled>false</enabled>
    100. </releases>
    101. </pluginRepository>
    102. </pluginRepositories>
    103. </project>

    3.2 配置文件:

    1. spring.data.mongodb.database=${env.MONGO_DATABASE}
    2. spring.data.mongodb.uri=mongodb+srv://${env.MONGO_USER}:${env.MONGO_PASSWORD}@${env.MONGO_CLUSTER}

    .env文件:

    1. MONGO_DATABASE=这个是你的那个数据库
    2. MONGO_USER=账号
    3. MONGO_PASSWORD=密码
    4. MONGO_CLUSTER=服务器的节点

    这些信息都是可以在服务器那边连接的时候看到

    创建实体类:

    1. package dev.farhan.movies.pojo;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.NoArgsConstructor;
    5. import org.bson.types.ObjectId;
    6. import org.springframework.data.annotation.Id;
    7. import org.springframework.data.mongodb.core.mapping.Document;
    8. import org.springframework.data.mongodb.core.mapping.DocumentReference;
    9. import java.util.List;
    10. @Document(collection = "movies")
    11. @Data
    12. @AllArgsConstructor
    13. @NoArgsConstructor
    14. public class Movie {
    15. @Id
    16. private ObjectId id;
    17. private String imdbId;
    18. private String title;
    19. private String releaseDate;
    20. private String trailerLink;
    21. private String poster;
    22. private List backdrops;
    23. private List genres;
    24. @DocumentReference
    25. private List reviews;
    26. public Movie(String imdbId, String title, String releaseDate, String trailerLink, String poster, List backdrops, List genres) {
    27. this.imdbId = imdbId;
    28. this.title = title;
    29. this.releaseDate = releaseDate;
    30. this.trailerLink = trailerLink;
    31. this.poster = poster;
    32. this.backdrops = backdrops;
    33. this.genres = genres;
    34. }
    35. }

    sql:

    1. [{
    2. "_id": {
    3. "$oid": "652a7733d3f2daf0d09eb498"
    4. },
    5. "imdbId": "tt3447590",
    6. "title": "Roald Dahl's Matilda the Musical",
    7. "releaseDate": "2022-11-25",
    8. "trailerLink": "https://www.youtube.com/watch?v=lroAhsDr2vI",
    9. "genres": [
    10. "Fantasy",
    11. "Family",
    12. "Comedy"
    13. ],
    14. "poster": "https://image.tmdb.org/t/p/w500/ga8R3OiOMMgSvZ4cOj8x7prUNYZ.jpg",
    15. "backdrops": [
    16. "https://image.tmdb.org/t/p/original/nWs0auTqn2UaFGfTKtUE5tlTeBu.jpg",
    17. "https://image.tmdb.org/t/p/original/bPftMelR4N3jUg2LTlEblFz0gWk.jpg",
    18. "https://image.tmdb.org/t/p/original/u2MLMkGEjJGQDs17Vmoej1RYFph.jpg",
    19. "https://image.tmdb.org/t/p/original/jG52tsazn04F1fe8hPZfVv7ICKt.jpg",
    20. "https://image.tmdb.org/t/p/original/4INEI7t7Vcg0cFvze7UIgwYCeSG.jpg",
    21. "https://image.tmdb.org/t/p/original/krAu6znzW8c54NdJPneNi4bem1l.jpg",
    22. "https://image.tmdb.org/t/p/original/6TUMppDMrYA4gzoaDUbbSnZFlxW.jpg",
    23. "https://image.tmdb.org/t/p/original/hacV1h1SWrPlrerF3xpetvEdqT.jpg",
    24. "https://image.tmdb.org/t/p/original/7iXsB1r9IK17ZFShqoxcHKQ7dLp.jpg",
    25. "https://image.tmdb.org/t/p/original/dwiRYDLcFyDOkgkSc1JFtTr6Kdk.jpg"
    26. ],
    27. "reviewIds": []
    28. },
    29. {
    30. "_id": {
    31. "$oid": "652a7733d3f2daf0d09eb499"
    32. },
    33. "imdbId": "tt9114286",
    34. "title": "Black Panther: Wakanda Forever",
    35. "releaseDate": "2022-11-11",
    36. "trailerLink": "https://www.youtube.com/watch?v=_Z3QKkl1WyM",
    37. "genres": [
    38. "Action",
    39. "Adventure",
    40. "Science Fiction"
    41. ],
    42. "poster": "https://image.tmdb.org/t/p/w500/cryEN3sWlgB2wTzcUNVtDGI8bkM.jpg",
    43. "backdrops": [
    44. "https://image.tmdb.org/t/p/original/yYrvN5WFeGYjJnRzhY0QXuo4Isw.jpg",
    45. "https://image.tmdb.org/t/p/original/xDMIl84Qo5Tsu62c9DGWhmPI67A.jpg",
    46. "https://image.tmdb.org/t/p/original/cs3LpA38BS2XDPfUzdgMB537XOo.jpg",
    47. "https://image.tmdb.org/t/p/original/6SGMzCsaU094Mt32IHGkIYtIl06.jpg",
    48. "https://image.tmdb.org/t/p/original/bty0TwJGsxMqYRptgyzn28Cxq5y.jpg",
    49. "https://image.tmdb.org/t/p/original/h2jp3CSdTPc22mUqps9I8vXDPaN.jpg",
    50. "https://image.tmdb.org/t/p/original/fSfWloWi5rmqbmC7GhO0HY2TMZW.jpg",
    51. "https://image.tmdb.org/t/p/original/vZujZnmkYB5nGUC5d5llK9DbGLk.jpg",
    52. "https://image.tmdb.org/t/p/original/8sMmAmN2x7mBiNKEX2o0aOTozEB.jpg",
    53. "https://image.tmdb.org/t/p/original/geI3Mk7nehX1kvyIY3K5ajaiNfI.jpg"
    54. ],
    55. "reviewIds": []
    56. },
    57. {
    58. "_id": {
    59. "$oid": "652a7733d3f2daf0d09eb492"
    60. },
    61. "imdbId": "tt3915174",
    62. "title": "Puss in Boots: The Last Wish",
    63. "releaseDate": "2022-12-21",
    64. "trailerLink": "https://www.youtube.com/watch?v=tHb7WlgyaUc",
    65. "genres": [
    66. "Animation",
    67. "Action",
    68. "Adventure",
    69. "Comedy",
    70. "Family"
    71. ],
    72. "poster": "https://image.tmdb.org/t/p/w500/1NqwE6LP9IEdOZ57NCT51ftHtWT.jpg",
    73. "backdrops": [
    74. "https://image.tmdb.org/t/p/original/r9PkFnRUIthgBp2JZZzD380MWZy.jpg",
    75. "https://image.tmdb.org/t/p/original/faXT8V80JRhnArTAeYXz0Eutpv9.jpg",
    76. "https://image.tmdb.org/t/p/original/pdrlEaknhta2wvE2dcD8XDEbAI4.jpg",
    77. "https://image.tmdb.org/t/p/original/tGwO4xcBjhXC0p5qlkw37TrH6S6.jpg",
    78. "https://image.tmdb.org/t/p/original/cP8YNG3XUeBmO8Jk7Skzq3vwHy1.jpg",
    79. "https://image.tmdb.org/t/p/original/qLE8yuieTDN93WNJRmFSAEJChOg.jpg",
    80. "https://image.tmdb.org/t/p/original/vNuHqmOJRQXY0PBd887DklSDlBP.jpg",
    81. "https://image.tmdb.org/t/p/original/uUCc62M0I3lpZy0SiydbBmUIpNi.jpg",
    82. "https://image.tmdb.org/t/p/original/2wPJIFrBhzzAP8oHDOlShMkERH6.jpg",
    83. "https://image.tmdb.org/t/p/original/fnfirCEDIkxZnQEtEMMSgllm0KZ.jpg"
    84. ],
    85. "reviewIds": []
    86. },
    87. {
    88. "_id": {
    89. "$oid": "652a7733d3f2daf0d09eb493"
    90. },
    91. "imdbId": "tt1630029",
    92. "title": "Avatar: The Way of Water",
    93. "releaseDate": "2022-12-16",
    94. "trailerLink": "https://www.youtube.com/watch?v=d9MyW72ELq0",
    95. "genres": [
    96. "Science Fiction",
    97. "Action",
    98. "Adventure"
    99. ],
    100. "poster": "https://image.tmdb.org/t/p/w500/t6HIqrRAclMCA60NsSmeqe9RmNV.jpg",
    101. "backdrops": [
    102. "https://image.tmdb.org/t/p/original/s16H6tpK2utvwDtzZ8Qy4qm5Emw.jpg",
    103. "https://image.tmdb.org/t/p/original/evaFLqtswezLosllRZtJNMiO1UR.jpg",
    104. "https://image.tmdb.org/t/p/original/198vrF8k7mfQ4FjDJsBmdQcaiyq.jpg",
    105. "https://image.tmdb.org/t/p/original/zaapQ1zjKe2BGhhowh5pM251Gpl.jpg",
    106. "https://image.tmdb.org/t/p/original/tQ91wWQJ2WRNDXwxuO7GCXX5VPC.jpg",
    107. "https://image.tmdb.org/t/p/original/5gPQKfFJnl8d1edbkOzKONo4mnr.jpg",
    108. "https://image.tmdb.org/t/p/original/2fS9cpar9rzxixwnRptg4bGmIym.jpg",
    109. "https://image.tmdb.org/t/p/original/fkGR1ltNbvERk3topo4dP3gWsvR.jpg",
    110. "https://image.tmdb.org/t/p/original/rb9IHprKNoSKqatP2vr25unUDSu.jpg",
    111. "https://image.tmdb.org/t/p/original/37ZswIuRQcRBN7kHij5MBjzRMRt.jpg"
    112. ],
    113. "reviewIds": []
    114. },
    115. {
    116. "_id": {
    117. "$oid": "652a7733d3f2daf0d09eb49a"
    118. },
    119. "imdbId": "tt10298840",
    120. "title": "Strange World",
    121. "releaseDate": "2022-11-23",
    122. "trailerLink": "https://www.youtube.com/watch?v=bKh2G73gCCs",
    123. "genres": [
    124. "Family",
    125. "Adventure",
    126. "Science Fiction",
    127. "Animation"
    128. ],
    129. "poster": "https://image.tmdb.org/t/p/w500/kgJ8bX3zDQDM2Idkleis28XSVnu.jpg",
    130. "backdrops": [
    131. "https://image.tmdb.org/t/p/original/5wDBVictj4wUYZ31gR5WzCM9dLD.jpg",
    132. "https://image.tmdb.org/t/p/original/zNIlXd7CAz0hHAInbs2nsFRc0xQ.jpg",
    133. "https://image.tmdb.org/t/p/original/1rukJHAP5p6DNHe75Oo1D0m3SnR.jpg",
    134. "https://image.tmdb.org/t/p/original/aKbe411WyjTZy1OZUVIdNDYVf21.jpg",
    135. "https://image.tmdb.org/t/p/original/9RKvxz0IryD2ofLYyGpnE7HeWlR.jpg",
    136. "https://image.tmdb.org/t/p/original/kFURsDklj7QGMMkGJVwDBaJJn05.jpg",
    137. "https://image.tmdb.org/t/p/original/v6oBDkd7ogXzTQxIU0H5SXq0hOL.jpg",
    138. "https://image.tmdb.org/t/p/original/fBshLiEJcjdfrU3qQBIINcePSsm.jpg",
    139. "https://image.tmdb.org/t/p/original/3oie0kID8SNCjkqN6Raweg5dJa.jpg",
    140. "https://image.tmdb.org/t/p/original/zgFldVKON1Nxp8ui7HVABGKDQKM.jpg"
    141. ],
    142. "reviewIds": []
    143. },
    144. {
    145. "_id": {
    146. "$oid": "652a7733d3f2daf0d09eb49b"
    147. },
    148. "imdbId": "tt8093700",
    149. "title": "The Woman King",
    150. "releaseDate": "2022-09-15",
    151. "trailerLink": "https://www.youtube.com/watch?v=3RDaPV_rJ1Y",
    152. "genres": [
    153. "Action",
    154. "Drama",
    155. "History"
    156. ],
    157. "poster": "https://image.tmdb.org/t/p/w500/5O1GLla5vNuegqNxNhKL1OKE1lO.jpg",
    158. "backdrops": [
    159. "https://image.tmdb.org/t/p/original/gkseI3CUfQtMKX41XD4AxDzhQb7.jpg",
    160. "https://image.tmdb.org/t/p/original/wSILunFEbvw00Ql2aaMHCSZf3cI.jpg",
    161. "https://image.tmdb.org/t/p/original/xTsERrOCW15OIYl5aPX7Jbj38wu.jpg",
    162. "https://image.tmdb.org/t/p/original/j06sSrtbqnZdSgG6yEduao95y48.jpg",
    163. "https://image.tmdb.org/t/p/original/v4YV4ne1nwNni35iz4WmpZRZpCD.jpg",
    164. "https://image.tmdb.org/t/p/original/6n5ln1vWGD3JyT6Ibt7ZxjSxY3v.jpg",
    165. "https://image.tmdb.org/t/p/original/gi47WUUYVQWaLE5mJraS87ycdy6.jpg",
    166. "https://image.tmdb.org/t/p/original/dTQOU5a32K3UPTIXHgipEqN41OM.jpg",
    167. "https://image.tmdb.org/t/p/original/7zQJYV02yehWrQN6NjKsBorqUUS.jpg",
    168. "https://image.tmdb.org/t/p/original/rdDL4y7BxGyXFEDJgAG4lz89bG2.jpg"
    169. ],
    170. "reviewIds": []
    171. },
    172. {
    173. "_id": {
    174. "$oid": "652a7733d3f2daf0d09eb494"
    175. },
    176. "imdbId": "tt8760708",
    177. "title": "M3GAN",
    178. "releaseDate": "2023-01-06",
    179. "trailerLink": "https://www.youtube.com/watch?v=BRb4U99OU80",
    180. "genres": [
    181. "Science Fiction",
    182. "Horror",
    183. "Comedy"
    184. ],
    185. "poster": "https://image.tmdb.org/t/p/w500/xBl5AGw7HXZcv1nNXPlzGgO4Cfo.jpg",
    186. "backdrops": [
    187. "https://image.tmdb.org/t/p/original/5kAGbi9MFAobQTVfK4kWPnIfnP0.jpg",
    188. "https://image.tmdb.org/t/p/original/dlxzUj7z1MqEcFiwvvrj0bvBKDY.jpg",
    189. "https://image.tmdb.org/t/p/original/q2fY4kMXKoGv4CQf310MCxpXlRI.jpg",
    190. "https://image.tmdb.org/t/p/original/pTxwFdsdDWzpCRYuk1QbggdaOlL.jpg",
    191. "https://image.tmdb.org/t/p/original/1zuz2RgFoOjulkjjNHHFc3WiHGB.jpg",
    192. "https://image.tmdb.org/t/p/original/7HqxI1IXMloT9VTSuDC8ikaj810.jpg",
    193. "https://image.tmdb.org/t/p/original/vpK2rp3J5LiC01HoNM0j9DEHQ1T.jpg",
    194. "https://image.tmdb.org/t/p/original/cNHXdmr4amP6EPCMa0dqD8rwzDV.jpg",
    195. "https://image.tmdb.org/t/p/original/txQLFd6rfQrskQhFENkS1jElptt.jpg",
    196. "https://image.tmdb.org/t/p/original/dC4tj1ONdlZ2TWv4XD2SA1KUnJN.jpg"
    197. ],
    198. "reviewIds": []
    199. },
    200. {
    201. "_id": {
    202. "$oid": "652a7733d3f2daf0d09eb495"
    203. },
    204. "imdbId": "tt11116912",
    205. "title": "Troll",
    206. "releaseDate": "2022-12-01",
    207. "trailerLink": "https://www.youtube.com/watch?v=AiohkY_XQYQ",
    208. "genres": [
    209. "Fantasy",
    210. "Action",
    211. "Adventure"
    212. ],
    213. "poster": "https://image.tmdb.org/t/p/w500/9z4jRr43JdtU66P0iy8h18OyLql.jpg",
    214. "backdrops": [
    215. "https://image.tmdb.org/t/p/original/53BC9F2tpZnsGno2cLhzvGprDYS.jpg",
    216. "https://image.tmdb.org/t/p/original/e9Qb2kmBnMXHCmNMI8NX1JbWhh1.jpg",
    217. "https://image.tmdb.org/t/p/original/2WjOOOGUu6dp4r8VqR5n48DY7JG.jpg",
    218. "https://image.tmdb.org/t/p/original/duIsyybgrC4S8kcCIVaxNOttV15.jpg",
    219. "https://image.tmdb.org/t/p/original/3RS8runn9AfrYDzRVPWuGPmvXQf.jpg",
    220. "https://image.tmdb.org/t/p/original/8wLRn2VvBlCu6cqYS4ypipnwosr.jpg",
    221. "https://image.tmdb.org/t/p/original/zDqVVkmfvj47FBUE5lwE4rWnITu.jpg",
    222. "https://image.tmdb.org/t/p/original/682Ui5DwZDdbIPzKAEOR7cJlMXa.jpg",
    223. "https://image.tmdb.org/t/p/original/6jdlppcnGi3XuJamfs4Vl7HyxB.jpg",
    224. "https://image.tmdb.org/t/p/original/uIq83ogs7QBEWi1aqmUrdDIH61m.jpg"
    225. ],
    226. "reviewIds": []
    227. },
    228. {
    229. "_id": {
    230. "$oid": "652a7733d3f2daf0d09eb497"
    231. },
    232. "imdbId": "tt0499549",
    233. "title": "Avatar",
    234. "releaseDate": "2009-12-15",
    235. "trailerLink": "https://www.youtube.com/watch?v=5PSNL1qE6VY",
    236. "genres": [
    237. "Fantasy",
    238. "Action",
    239. "Science Fiction",
    240. "Adventure"
    241. ],
    242. "poster": "https://image.tmdb.org/t/p/w500/jRXYjXNq0Cs2TcJjLkki24MLp7u.jpg",
    243. "backdrops": [
    244. "https://image.tmdb.org/t/p/original/o0s4XsEDfDlvit5pDRKjzXR4pp2.jpg",
    245. "https://image.tmdb.org/t/p/original/8I37NtDffNV7AZlDa7uDvvqhovU.jpg",
    246. "https://image.tmdb.org/t/p/original/2YLOjUiczXEgVZFDSIeH3iWB3Ub.jpg",
    247. "https://image.tmdb.org/t/p/original/Yc9q6QuWrMp9nuDm5R8ExNqbEq.jpg",
    248. "https://image.tmdb.org/t/p/original/jlQJDD0L5ZojjlS0KYnApdO0n19.jpg",
    249. "https://image.tmdb.org/t/p/original/sfw4m2tOgQRzhF6VXxaXGfd1vX.jpg",
    250. "https://image.tmdb.org/t/p/original/7ABsaBkO1jA2psC8Hy4IDhkID4h.jpg",
    251. "https://image.tmdb.org/t/p/original/xMMrBziwJqrgjerqpNeQvwuwiUp.jpg",
    252. "https://image.tmdb.org/t/p/original/chauy3iJaZtrMbTr72rgNmOZwo3.jpg",
    253. "https://image.tmdb.org/t/p/original/mYJkJ7YxJsUNI1nAOOUOpRN2auC.jpg"
    254. ],
    255. "reviewIds": []
    256. },
    257. {
    258. "_id": {
    259. "$oid": "652a7733d3f2daf0d09eb496"
    260. },
    261. "imdbId": "tt6443346",
    262. "title": "Black Adam",
    263. "releaseDate": "2022-10-19",
    264. "trailerLink": "https://www.youtube.com/watch?v=JaV7mmc9HGw",
    265. "genres": [
    266. "Fantasy",
    267. "Action",
    268. "Science Fiction"
    269. ],
    270. "poster": "https://image.tmdb.org/t/p/w500/pFlaoHTZeyNkG83vxsAJiGzfSsa.jpg",
    271. "backdrops": [
    272. "https://image.tmdb.org/t/p/original/bQXAqRx2Fgc46uCVWgoPz5L5Dtr.jpg",
    273. "https://image.tmdb.org/t/p/original/9hNtTwY8P5v2MKnUeb7iuREI7Yb.jpg",
    274. "https://image.tmdb.org/t/p/original/zplntIhzXyBiXFYWReETxh0uyFF.jpg",
    275. "https://image.tmdb.org/t/p/original/yxkhM18dYwsRRffLnd9lz2d4i0v.jpg",
    276. "https://image.tmdb.org/t/p/original/bgaBKREAfUtZgvd6zoV6RQRcIUt.jpg",
    277. "https://image.tmdb.org/t/p/original/uqYxoj4hqwocwfBs2xxGyQT88Yk.jpg",
    278. "https://image.tmdb.org/t/p/original/qBx97wytqlyPqXATHqRgIVFxJRU.jpg",
    279. "https://image.tmdb.org/t/p/original/d6MhreFdMHONqX3iZlJGCF8UkIt.jpg",
    280. "https://image.tmdb.org/t/p/original/9inNotReApz0n50WvWbrt0n1cbL.jpg",
    281. "https://image.tmdb.org/t/p/original/pSOuqtJmdh7aI1yiK7M8e0PmbPC.jpg"
    282. ],
    283. "reviewIds": []
    284. }]

    复制粘贴到一个json文件里面,然后导入:

    创建MovieRepository类:

    1. package dev.farhan.movies.repository;
    2. import dev.farhan.movies.pojo.Movie;
    3. import org.bson.types.ObjectId;
    4. import org.springframework.data.mongodb.repository.MongoRepository;
    5. import org.springframework.stereotype.Repository;
    6. import java.util.Optional;
    7. @Repository
    8. public interface MovieRepository extends MongoRepository {
    9. Optional findMovieByImdbId(String imdbId);
    10. }

    创建MovieService类:

    1. package dev.farhan.movies.service;
    2. import dev.farhan.movies.repository.MovieRepository;
    3. import dev.farhan.movies.pojo.Movie;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Service;
    6. import java.util.List;
    7. import java.util.Optional;
    8. @Service
    9. public class MovieService {
    10. @Autowired
    11. private MovieRepository repository;
    12. public List findAllMovies() {
    13. return repository.findAll();
    14. }
    15. public Optional findMovieByImdbId(String imdbId) {
    16. return repository.findMovieByImdbId(imdbId);
    17. }
    18. }

    创建controller类,进行测试:

    1. package dev.farhan.movies.controller;
    2. import dev.farhan.movies.pojo.Movie;
    3. import dev.farhan.movies.service.MovieService;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.http.HttpStatus;
    6. import org.springframework.http.ResponseEntity;
    7. import org.springframework.web.bind.annotation.*;
    8. import java.util.List;
    9. import java.util.Optional;
    10. @RestController
    11. @RequestMapping("/api/v1/movies")
    12. public class MovieController {
    13. @Autowired
    14. private MovieService service;
    15. @GetMapping
    16. public ResponseEntity> getMovies() {
    17. return new ResponseEntity>(service.findAllMovies(), HttpStatus.OK);
    18. }
    19. @GetMapping("/{imdbId}")
    20. public ResponseEntity> getSingleMovie(@PathVariable String imdbId){
    21. return new ResponseEntity>(service.findMovieByImdbId(imdbId), HttpStatus.OK);
    22. }
    23. }

    启动,并访问:localhost:8080/api/v1/movies

    结果:成功

  • 相关阅读:
    OpenCV(十六):高斯图像金字塔
    秋招挂麻了,就差去送外卖了,10w字Java八股啃完,春招必拿下
    8 个关于提高你 Flutter 移动程序安全性的建议
    Cmasher颜色包--共53种--全平台可用
    Vue框架快速上手
    SLAM从入门到精通(launch文件学习)
    【车载AI音视频电脑】高清车载摄像头,车载云台摄像头
    多台电脑之间共享、传输文件数据:不借助数据线与软件的方法
    软考中级软件设计师--考试准备
    Ubuntu sh文件编写,开多终端,自动读取密码
  • 原文地址:https://blog.csdn.net/m0_63251896/article/details/134041369