第一步:在mongodb的官网里面创建云服务器
点进去
这是免费的,由于是一个项目只可以创建一个,这里我已经创建好了
用本地的mongodb服务也是可以的
第二步:点击connect,下载连接mongodb的软件:MongoDBCompass
第三步:创建springboot项目
3.1 、pom依赖:
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.0.12-SNAPSHOT
dev.farhan
movies
0.0.1-SNAPSHOT
movies
a simple APIS relates to movies
17
org.springframework.boot
spring-boot-starter-data-mongodb
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
me.paulschwarz
spring-dotenv
2.5.4
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
false
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
false
3.2 配置文件:
spring.data.mongodb.database=${env.MONGO_DATABASE}
spring.data.mongodb.uri=mongodb+srv://${env.MONGO_USER}:${env.MONGO_PASSWORD}@${env.MONGO_CLUSTER}
.env文件:
MONGO_DATABASE=这个是你的那个数据库
MONGO_USER=账号
MONGO_PASSWORD=密码
MONGO_CLUSTER=服务器的节点
这些信息都是可以在服务器那边连接的时候看到
创建实体类:
package dev.farhan.movies.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.DocumentReference;
import java.util.List;
@Document(collection = "movies")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Movie {
@Id
private ObjectId id;
private String imdbId;
private String title;
private String releaseDate;
private String trailerLink;
private String poster;
private List backdrops;
private List genres;
@DocumentReference
private List reviews;
public Movie(String imdbId, String title, String releaseDate, String trailerLink, String poster, List backdrops, List genres) {
this.imdbId = imdbId;
this.title = title;
this.releaseDate = releaseDate;
this.trailerLink = trailerLink;
this.poster = poster;
this.backdrops = backdrops;
this.genres = genres;
}
}
sql:
[{
"_id": {
"$oid": "652a7733d3f2daf0d09eb498"
},
"imdbId": "tt3447590",
"title": "Roald Dahl's Matilda the Musical",
"releaseDate": "2022-11-25",
"trailerLink": "https://www.youtube.com/watch?v=lroAhsDr2vI",
"genres": [
"Fantasy",
"Family",
"Comedy"
],
"poster": "https://image.tmdb.org/t/p/w500/ga8R3OiOMMgSvZ4cOj8x7prUNYZ.jpg",
"backdrops": [
"https://image.tmdb.org/t/p/original/nWs0auTqn2UaFGfTKtUE5tlTeBu.jpg",
"https://image.tmdb.org/t/p/original/bPftMelR4N3jUg2LTlEblFz0gWk.jpg",
"https://image.tmdb.org/t/p/original/u2MLMkGEjJGQDs17Vmoej1RYFph.jpg",
"https://image.tmdb.org/t/p/original/jG52tsazn04F1fe8hPZfVv7ICKt.jpg",
"https://image.tmdb.org/t/p/original/4INEI7t7Vcg0cFvze7UIgwYCeSG.jpg",
"https://image.tmdb.org/t/p/original/krAu6znzW8c54NdJPneNi4bem1l.jpg",
"https://image.tmdb.org/t/p/original/6TUMppDMrYA4gzoaDUbbSnZFlxW.jpg",
"https://image.tmdb.org/t/p/original/hacV1h1SWrPlrerF3xpetvEdqT.jpg",
"https://image.tmdb.org/t/p/original/7iXsB1r9IK17ZFShqoxcHKQ7dLp.jpg",
"https://image.tmdb.org/t/p/original/dwiRYDLcFyDOkgkSc1JFtTr6Kdk.jpg"
],
"reviewIds": []
},
{
"_id": {
"$oid": "652a7733d3f2daf0d09eb499"
},
"imdbId": "tt9114286",
"title": "Black Panther: Wakanda Forever",
"releaseDate": "2022-11-11",
"trailerLink": "https://www.youtube.com/watch?v=_Z3QKkl1WyM",
"genres": [
"Action",
"Adventure",
"Science Fiction"
],
"poster": "https://image.tmdb.org/t/p/w500/cryEN3sWlgB2wTzcUNVtDGI8bkM.jpg",
"backdrops": [
"https://image.tmdb.org/t/p/original/yYrvN5WFeGYjJnRzhY0QXuo4Isw.jpg",
"https://image.tmdb.org/t/p/original/xDMIl84Qo5Tsu62c9DGWhmPI67A.jpg",
"https://image.tmdb.org/t/p/original/cs3LpA38BS2XDPfUzdgMB537XOo.jpg",
"https://image.tmdb.org/t/p/original/6SGMzCsaU094Mt32IHGkIYtIl06.jpg",
"https://image.tmdb.org/t/p/original/bty0TwJGsxMqYRptgyzn28Cxq5y.jpg",
"https://image.tmdb.org/t/p/original/h2jp3CSdTPc22mUqps9I8vXDPaN.jpg",
"https://image.tmdb.org/t/p/original/fSfWloWi5rmqbmC7GhO0HY2TMZW.jpg",
"https://image.tmdb.org/t/p/original/vZujZnmkYB5nGUC5d5llK9DbGLk.jpg",
"https://image.tmdb.org/t/p/original/8sMmAmN2x7mBiNKEX2o0aOTozEB.jpg",
"https://image.tmdb.org/t/p/original/geI3Mk7nehX1kvyIY3K5ajaiNfI.jpg"
],
"reviewIds": []
},
{
"_id": {
"$oid": "652a7733d3f2daf0d09eb492"
},
"imdbId": "tt3915174",
"title": "Puss in Boots: The Last Wish",
"releaseDate": "2022-12-21",
"trailerLink": "https://www.youtube.com/watch?v=tHb7WlgyaUc",
"genres": [
"Animation",
"Action",
"Adventure",
"Comedy",
"Family"
],
"poster": "https://image.tmdb.org/t/p/w500/1NqwE6LP9IEdOZ57NCT51ftHtWT.jpg",
"backdrops": [
"https://image.tmdb.org/t/p/original/r9PkFnRUIthgBp2JZZzD380MWZy.jpg",
"https://image.tmdb.org/t/p/original/faXT8V80JRhnArTAeYXz0Eutpv9.jpg",
"https://image.tmdb.org/t/p/original/pdrlEaknhta2wvE2dcD8XDEbAI4.jpg",
"https://image.tmdb.org/t/p/original/tGwO4xcBjhXC0p5qlkw37TrH6S6.jpg",
"https://image.tmdb.org/t/p/original/cP8YNG3XUeBmO8Jk7Skzq3vwHy1.jpg",
"https://image.tmdb.org/t/p/original/qLE8yuieTDN93WNJRmFSAEJChOg.jpg",
"https://image.tmdb.org/t/p/original/vNuHqmOJRQXY0PBd887DklSDlBP.jpg",
"https://image.tmdb.org/t/p/original/uUCc62M0I3lpZy0SiydbBmUIpNi.jpg",
"https://image.tmdb.org/t/p/original/2wPJIFrBhzzAP8oHDOlShMkERH6.jpg",
"https://image.tmdb.org/t/p/original/fnfirCEDIkxZnQEtEMMSgllm0KZ.jpg"
],
"reviewIds": []
},
{
"_id": {
"$oid": "652a7733d3f2daf0d09eb493"
},
"imdbId": "tt1630029",
"title": "Avatar: The Way of Water",
"releaseDate": "2022-12-16",
"trailerLink": "https://www.youtube.com/watch?v=d9MyW72ELq0",
"genres": [
"Science Fiction",
"Action",
"Adventure"
],
"poster": "https://image.tmdb.org/t/p/w500/t6HIqrRAclMCA60NsSmeqe9RmNV.jpg",
"backdrops": [
"https://image.tmdb.org/t/p/original/s16H6tpK2utvwDtzZ8Qy4qm5Emw.jpg",
"https://image.tmdb.org/t/p/original/evaFLqtswezLosllRZtJNMiO1UR.jpg",
"https://image.tmdb.org/t/p/original/198vrF8k7mfQ4FjDJsBmdQcaiyq.jpg",
"https://image.tmdb.org/t/p/original/zaapQ1zjKe2BGhhowh5pM251Gpl.jpg",
"https://image.tmdb.org/t/p/original/tQ91wWQJ2WRNDXwxuO7GCXX5VPC.jpg",
"https://image.tmdb.org/t/p/original/5gPQKfFJnl8d1edbkOzKONo4mnr.jpg",
"https://image.tmdb.org/t/p/original/2fS9cpar9rzxixwnRptg4bGmIym.jpg",
"https://image.tmdb.org/t/p/original/fkGR1ltNbvERk3topo4dP3gWsvR.jpg",
"https://image.tmdb.org/t/p/original/rb9IHprKNoSKqatP2vr25unUDSu.jpg",
"https://image.tmdb.org/t/p/original/37ZswIuRQcRBN7kHij5MBjzRMRt.jpg"
],
"reviewIds": []
},
{
"_id": {
"$oid": "652a7733d3f2daf0d09eb49a"
},
"imdbId": "tt10298840",
"title": "Strange World",
"releaseDate": "2022-11-23",
"trailerLink": "https://www.youtube.com/watch?v=bKh2G73gCCs",
"genres": [
"Family",
"Adventure",
"Science Fiction",
"Animation"
],
"poster": "https://image.tmdb.org/t/p/w500/kgJ8bX3zDQDM2Idkleis28XSVnu.jpg",
"backdrops": [
"https://image.tmdb.org/t/p/original/5wDBVictj4wUYZ31gR5WzCM9dLD.jpg",
"https://image.tmdb.org/t/p/original/zNIlXd7CAz0hHAInbs2nsFRc0xQ.jpg",
"https://image.tmdb.org/t/p/original/1rukJHAP5p6DNHe75Oo1D0m3SnR.jpg",
"https://image.tmdb.org/t/p/original/aKbe411WyjTZy1OZUVIdNDYVf21.jpg",
"https://image.tmdb.org/t/p/original/9RKvxz0IryD2ofLYyGpnE7HeWlR.jpg",
"https://image.tmdb.org/t/p/original/kFURsDklj7QGMMkGJVwDBaJJn05.jpg",
"https://image.tmdb.org/t/p/original/v6oBDkd7ogXzTQxIU0H5SXq0hOL.jpg",
"https://image.tmdb.org/t/p/original/fBshLiEJcjdfrU3qQBIINcePSsm.jpg",
"https://image.tmdb.org/t/p/original/3oie0kID8SNCjkqN6Raweg5dJa.jpg",
"https://image.tmdb.org/t/p/original/zgFldVKON1Nxp8ui7HVABGKDQKM.jpg"
],
"reviewIds": []
},
{
"_id": {
"$oid": "652a7733d3f2daf0d09eb49b"
},
"imdbId": "tt8093700",
"title": "The Woman King",
"releaseDate": "2022-09-15",
"trailerLink": "https://www.youtube.com/watch?v=3RDaPV_rJ1Y",
"genres": [
"Action",
"Drama",
"History"
],
"poster": "https://image.tmdb.org/t/p/w500/5O1GLla5vNuegqNxNhKL1OKE1lO.jpg",
"backdrops": [
"https://image.tmdb.org/t/p/original/gkseI3CUfQtMKX41XD4AxDzhQb7.jpg",
"https://image.tmdb.org/t/p/original/wSILunFEbvw00Ql2aaMHCSZf3cI.jpg",
"https://image.tmdb.org/t/p/original/xTsERrOCW15OIYl5aPX7Jbj38wu.jpg",
"https://image.tmdb.org/t/p/original/j06sSrtbqnZdSgG6yEduao95y48.jpg",
"https://image.tmdb.org/t/p/original/v4YV4ne1nwNni35iz4WmpZRZpCD.jpg",
"https://image.tmdb.org/t/p/original/6n5ln1vWGD3JyT6Ibt7ZxjSxY3v.jpg",
"https://image.tmdb.org/t/p/original/gi47WUUYVQWaLE5mJraS87ycdy6.jpg",
"https://image.tmdb.org/t/p/original/dTQOU5a32K3UPTIXHgipEqN41OM.jpg",
"https://image.tmdb.org/t/p/original/7zQJYV02yehWrQN6NjKsBorqUUS.jpg",
"https://image.tmdb.org/t/p/original/rdDL4y7BxGyXFEDJgAG4lz89bG2.jpg"
],
"reviewIds": []
},
{
"_id": {
"$oid": "652a7733d3f2daf0d09eb494"
},
"imdbId": "tt8760708",
"title": "M3GAN",
"releaseDate": "2023-01-06",
"trailerLink": "https://www.youtube.com/watch?v=BRb4U99OU80",
"genres": [
"Science Fiction",
"Horror",
"Comedy"
],
"poster": "https://image.tmdb.org/t/p/w500/xBl5AGw7HXZcv1nNXPlzGgO4Cfo.jpg",
"backdrops": [
"https://image.tmdb.org/t/p/original/5kAGbi9MFAobQTVfK4kWPnIfnP0.jpg",
"https://image.tmdb.org/t/p/original/dlxzUj7z1MqEcFiwvvrj0bvBKDY.jpg",
"https://image.tmdb.org/t/p/original/q2fY4kMXKoGv4CQf310MCxpXlRI.jpg",
"https://image.tmdb.org/t/p/original/pTxwFdsdDWzpCRYuk1QbggdaOlL.jpg",
"https://image.tmdb.org/t/p/original/1zuz2RgFoOjulkjjNHHFc3WiHGB.jpg",
"https://image.tmdb.org/t/p/original/7HqxI1IXMloT9VTSuDC8ikaj810.jpg",
"https://image.tmdb.org/t/p/original/vpK2rp3J5LiC01HoNM0j9DEHQ1T.jpg",
"https://image.tmdb.org/t/p/original/cNHXdmr4amP6EPCMa0dqD8rwzDV.jpg",
"https://image.tmdb.org/t/p/original/txQLFd6rfQrskQhFENkS1jElptt.jpg",
"https://image.tmdb.org/t/p/original/dC4tj1ONdlZ2TWv4XD2SA1KUnJN.jpg"
],
"reviewIds": []
},
{
"_id": {
"$oid": "652a7733d3f2daf0d09eb495"
},
"imdbId": "tt11116912",
"title": "Troll",
"releaseDate": "2022-12-01",
"trailerLink": "https://www.youtube.com/watch?v=AiohkY_XQYQ",
"genres": [
"Fantasy",
"Action",
"Adventure"
],
"poster": "https://image.tmdb.org/t/p/w500/9z4jRr43JdtU66P0iy8h18OyLql.jpg",
"backdrops": [
"https://image.tmdb.org/t/p/original/53BC9F2tpZnsGno2cLhzvGprDYS.jpg",
"https://image.tmdb.org/t/p/original/e9Qb2kmBnMXHCmNMI8NX1JbWhh1.jpg",
"https://image.tmdb.org/t/p/original/2WjOOOGUu6dp4r8VqR5n48DY7JG.jpg",
"https://image.tmdb.org/t/p/original/duIsyybgrC4S8kcCIVaxNOttV15.jpg",
"https://image.tmdb.org/t/p/original/3RS8runn9AfrYDzRVPWuGPmvXQf.jpg",
"https://image.tmdb.org/t/p/original/8wLRn2VvBlCu6cqYS4ypipnwosr.jpg",
"https://image.tmdb.org/t/p/original/zDqVVkmfvj47FBUE5lwE4rWnITu.jpg",
"https://image.tmdb.org/t/p/original/682Ui5DwZDdbIPzKAEOR7cJlMXa.jpg",
"https://image.tmdb.org/t/p/original/6jdlppcnGi3XuJamfs4Vl7HyxB.jpg",
"https://image.tmdb.org/t/p/original/uIq83ogs7QBEWi1aqmUrdDIH61m.jpg"
],
"reviewIds": []
},
{
"_id": {
"$oid": "652a7733d3f2daf0d09eb497"
},
"imdbId": "tt0499549",
"title": "Avatar",
"releaseDate": "2009-12-15",
"trailerLink": "https://www.youtube.com/watch?v=5PSNL1qE6VY",
"genres": [
"Fantasy",
"Action",
"Science Fiction",
"Adventure"
],
"poster": "https://image.tmdb.org/t/p/w500/jRXYjXNq0Cs2TcJjLkki24MLp7u.jpg",
"backdrops": [
"https://image.tmdb.org/t/p/original/o0s4XsEDfDlvit5pDRKjzXR4pp2.jpg",
"https://image.tmdb.org/t/p/original/8I37NtDffNV7AZlDa7uDvvqhovU.jpg",
"https://image.tmdb.org/t/p/original/2YLOjUiczXEgVZFDSIeH3iWB3Ub.jpg",
"https://image.tmdb.org/t/p/original/Yc9q6QuWrMp9nuDm5R8ExNqbEq.jpg",
"https://image.tmdb.org/t/p/original/jlQJDD0L5ZojjlS0KYnApdO0n19.jpg",
"https://image.tmdb.org/t/p/original/sfw4m2tOgQRzhF6VXxaXGfd1vX.jpg",
"https://image.tmdb.org/t/p/original/7ABsaBkO1jA2psC8Hy4IDhkID4h.jpg",
"https://image.tmdb.org/t/p/original/xMMrBziwJqrgjerqpNeQvwuwiUp.jpg",
"https://image.tmdb.org/t/p/original/chauy3iJaZtrMbTr72rgNmOZwo3.jpg",
"https://image.tmdb.org/t/p/original/mYJkJ7YxJsUNI1nAOOUOpRN2auC.jpg"
],
"reviewIds": []
},
{
"_id": {
"$oid": "652a7733d3f2daf0d09eb496"
},
"imdbId": "tt6443346",
"title": "Black Adam",
"releaseDate": "2022-10-19",
"trailerLink": "https://www.youtube.com/watch?v=JaV7mmc9HGw",
"genres": [
"Fantasy",
"Action",
"Science Fiction"
],
"poster": "https://image.tmdb.org/t/p/w500/pFlaoHTZeyNkG83vxsAJiGzfSsa.jpg",
"backdrops": [
"https://image.tmdb.org/t/p/original/bQXAqRx2Fgc46uCVWgoPz5L5Dtr.jpg",
"https://image.tmdb.org/t/p/original/9hNtTwY8P5v2MKnUeb7iuREI7Yb.jpg",
"https://image.tmdb.org/t/p/original/zplntIhzXyBiXFYWReETxh0uyFF.jpg",
"https://image.tmdb.org/t/p/original/yxkhM18dYwsRRffLnd9lz2d4i0v.jpg",
"https://image.tmdb.org/t/p/original/bgaBKREAfUtZgvd6zoV6RQRcIUt.jpg",
"https://image.tmdb.org/t/p/original/uqYxoj4hqwocwfBs2xxGyQT88Yk.jpg",
"https://image.tmdb.org/t/p/original/qBx97wytqlyPqXATHqRgIVFxJRU.jpg",
"https://image.tmdb.org/t/p/original/d6MhreFdMHONqX3iZlJGCF8UkIt.jpg",
"https://image.tmdb.org/t/p/original/9inNotReApz0n50WvWbrt0n1cbL.jpg",
"https://image.tmdb.org/t/p/original/pSOuqtJmdh7aI1yiK7M8e0PmbPC.jpg"
],
"reviewIds": []
}]
复制粘贴到一个json文件里面,然后导入:
创建MovieRepository类:
package dev.farhan.movies.repository;
import dev.farhan.movies.pojo.Movie;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface MovieRepository extends MongoRepository {
Optional findMovieByImdbId(String imdbId);
}
创建MovieService类:
package dev.farhan.movies.service;
import dev.farhan.movies.repository.MovieRepository;
import dev.farhan.movies.pojo.Movie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class MovieService {
@Autowired
private MovieRepository repository;
public List findAllMovies() {
return repository.findAll();
}
public Optional findMovieByImdbId(String imdbId) {
return repository.findMovieByImdbId(imdbId);
}
}
创建controller类,进行测试:
package dev.farhan.movies.controller;
import dev.farhan.movies.pojo.Movie;
import dev.farhan.movies.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/api/v1/movies")
public class MovieController {
@Autowired
private MovieService service;
@GetMapping
public ResponseEntity> getMovies() {
return new ResponseEntity>(service.findAllMovies(), HttpStatus.OK);
}
@GetMapping("/{imdbId}")
public ResponseEntity> getSingleMovie(@PathVariable String imdbId){
return new ResponseEntity>(service.findMovieByImdbId(imdbId), HttpStatus.OK);
}
}
启动,并访问:localhost:8080/api/v1/movies
结果:成功