Last active
January 21, 2022 06:26
-
-
Save CodingLink/e10ba05607b854aa2a5f8e4bc1220819 to your computer and use it in GitHub Desktop.
SpringBoot通过TMDB查询电影封面
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @author palm | |
| * 解析json获取电影背景图片 | |
| */ | |
| public class Image { | |
| /** | |
| * api接口及token | |
| * api_key: TMDB api key | |
| */ | |
| private final String api_key="{your api key}"; | |
| private final String tmdbUrl="https://api.themoviedb.org/3/movie/"; | |
| /** | |
| * 获取图片url | |
| * @return String url | |
| */ | |
| public String getImagesPath(int movieId){ | |
| //发送请求 | |
| String url=tmdbUrl+movieId+"/images?api_key="+api_key+"&language=en"; | |
| RestTemplate template=new RestTemplate(); | |
| template.setErrorHandler(new MyRestErrorHandler()); | |
| HttpHeaders headers = new HttpHeaders(); | |
| headers.add("user-agent", "Application"); | |
| headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); | |
| headers.setContentType(MediaType.APPLICATION_JSON); | |
| HttpEntity<String> request= new HttpEntity<String>(headers); | |
| ResponseEntity<String> response=template.exchange(url, HttpMethod.GET,request,String.class); | |
| System.out.println(response.getStatusCodeValue()); | |
| if(response.getStatusCodeValue()!=404) { | |
| ImageJson image = JSON.parseObject(response.getBody(), ImageJson.class); | |
| if (image != null && image.getPosters().size() != 0) { | |
| return "https://www.themoviedb.org/t/p/w1280" + image.getPosters().get(0).getFile_path(); | |
| } | |
| } | |
| //404图片 | |
| return "{your 404 image}"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.List; | |
| /** 解析TMDB网站电影信息api接口 | |
| * @author palm | |
| */ | |
| public class ImageJson { | |
| private List<String> backdrops; | |
| private int id; | |
| private List<String> logos; | |
| private List<Posters> posters; | |
| public List<String> getBackdrops() { | |
| return backdrops; | |
| } | |
| public void setBackdrops(List<String> backdrops) { | |
| this.backdrops = backdrops; | |
| } | |
| public int getId() { | |
| return id; | |
| } | |
| public void setId(int id) { | |
| this.id = id; | |
| } | |
| public List<String> getLogos() { | |
| return logos; | |
| } | |
| public void setLogos(List<String> logos) { | |
| this.logos = logos; | |
| } | |
| public List<Posters> getPosters() { | |
| return posters; | |
| } | |
| public void setPosters(List<Posters> posters) { | |
| this.posters = posters; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.springframework.http.HttpStatus; | |
| import org.springframework.http.client.ClientHttpResponse; | |
| import org.springframework.web.client.ResponseErrorHandler; | |
| import java.io.IOException; | |
| /** | |
| * @author palm | |
| * http错误处理接口 | |
| */ | |
| public class MyRestErrorHandler implements ResponseErrorHandler { | |
| /** | |
| * 判断返回结果response是否是异常结果 | |
| * 主要是去检查response 的HTTP Status | |
| * 仿造DefaultResponseErrorHandler实现即可 | |
| */ | |
| @Override | |
| public boolean hasError(ClientHttpResponse response) throws IOException { | |
| int rawStatusCode = response.getRawStatusCode(); | |
| HttpStatus statusCode = HttpStatus.resolve(rawStatusCode); | |
| return (statusCode != null ? statusCode.isError(): hasError(rawStatusCode)); | |
| } | |
| protected boolean hasError(int unknownStatusCode) { | |
| HttpStatus.Series series = HttpStatus.Series.resolve(unknownStatusCode); | |
| return (series == HttpStatus.Series.CLIENT_ERROR || series == HttpStatus.Series.SERVER_ERROR); | |
| } | |
| @Override | |
| public void handleError(ClientHttpResponse response) throws IOException { | |
| // 里面可以实现你自己遇到了Error进行合理的处理 | |
| //TODO 将接口请求的异常信息持久化 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @author palm | |
| */ | |
| public class Posters { | |
| private double aspect_ratio; | |
| private int height; | |
| private String iso_639_1; | |
| private String file_path; | |
| private int vote_average; | |
| private int vote_count; | |
| private int width; | |
| public double getAspect_ratio() { | |
| return aspect_ratio; | |
| } | |
| public void setAspect_ratio(double aspect_ratio) { | |
| this.aspect_ratio = aspect_ratio; | |
| } | |
| public int getHeight() { | |
| return height; | |
| } | |
| public void setHeight(int height) { | |
| this.height = height; | |
| } | |
| public String getIso_639_1() { | |
| return iso_639_1; | |
| } | |
| public void setIso_639_1(String iso_639_1) { | |
| this.iso_639_1 = iso_639_1; | |
| } | |
| public String getFile_path() { | |
| return file_path; | |
| } | |
| public void setFile_path(String file_path) { | |
| this.file_path = file_path; | |
| } | |
| public int getVote_average() { | |
| return vote_average; | |
| } | |
| public void setVote_average(int vote_average) { | |
| this.vote_average = vote_average; | |
| } | |
| public int getVote_count() { | |
| return vote_count; | |
| } | |
| public void setVote_count(int vote_count) { | |
| this.vote_count = vote_count; | |
| } | |
| public int getWidth() { | |
| return width; | |
| } | |
| public void setWidth(int width) { | |
| this.width = width; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment