I started my first spring boot project. That’s why it’s not enough.
Already looked for stack overflows and googling.
I’d appreciate your help.
**tmdb api Query For Details **
'https://api.themoviedb.org/3/movie/343611?api_key=87bad1aa...~'
movie_id : 343611
Controller
import com.example.joyit.service.MovieSearchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("moviesearch")
@RequiredArgsConstructor
public class MovieSearchController {
private final MovieSearchService movieSearchService;
/*
* localhost:8080/moviesearch/detail
*
* @param movieid
* @return ResponseEntity
*/
@GetMapping("/detail/{movieid}")
public ResponseEntity<?> searchDetail(@PathVariable int movieid) {
log.info("[Request] search detail");
return new ResponseEntity<>(movieSearchService.MovieSearchDetailApi(movieid), HttpStatus.OK);
}
}
Service
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@Service
public class MovieSearchService {
@Value("${tmdb.api}")
private String tmdbkey;
private String movieDetailUrl="https://api.themoviedb.org/3/movie";
public MovieSearchDetailDto MovieSearchDetailApi(int movieid) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Authorization", "TMDB_API_KEY" + tmdbkey); //Authorization 설정
HttpEntity<String> httpEntity = new HttpEntity<>(httpHeaders);
URI targetUrl = UriComponentsBuilder
.fromUriString(movieDetailUrl)
.port(movieid)
.queryParam("api_key", tmdbkey)
.queryParam("language", "ko-KR")
.build()
.encode(StandardCharsets.UTF_8)
.toUri();
ResponseEntity<MovieSearchDetailDto> result = restTemplate.exchange(targetUrl, HttpMethod.GET, httpEntity, MovieSearchDetailDto.class);
return result.getBody();
}
}
MovieSearchDetailDto
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class MovieSearchDetailDto {
List<Results> results = new ArrayList<>();
static class Results{
public String title;
public int moive_id;
public String poster_path;
public String release_date;
public String overview;
}
}
Using ‘@PathVariable’ in Controller copied the code through Googling. But I don’t know how to make Uri in Service to utilize movie_id. Movie_id used port() because it is int type. But it doesn’t run on postman.
expectation
{
"id": 76600,
"poster_path": "/t6HIqrRAclMCA60NsSmeqe9RmNV.jpg",
"release_date": "2022-12-14",
"title": "Avatar: The Way of Water",
"overview": "Set more than a decade after the events of the first film, learn the story of the Sully family (Jake, Neytiri, and their kids), the trouble that follows them, the lengths they go to keep each other safe, the battles they fight to stay alive, and the tragedies they endure.",
}
2
Answers
Based on your current one implementation i think you should construct the
URI
something like this:You dont need to pass
movieid
as a URIport
.Also
Results
class:Can you try this one builder ? and give me feedback if it is ok so.
it seems that the issue is with constructing the URL
check out this code snippet