问题描述
我有一个Spring Boot应用程序,我在其中添加了rest API。当我仅将其与“ Movies App”一起使用时,它的效果很好。但是,我创建了第二个应用程序,该应用程序需要使用其余API的两个端点。
- 在特定年份上映的电影
- 由特定导演执导的电影
我现在正在处理第一个,而较早时它遇到了未发现的错误,我已解决了这个问题,现在我只是遇到了whitelabel错误。它似乎并没有消耗数据。我不确定我刚才输入的内容有误还是我的整体方法不正确。
因此,当我与Movies项目同时运行消费者应用程序时,我应该能够进入浏览器并放入localhost:8080 / api / films {releaseYear},它应该返回所有电影。页面上的那一年。因此,例如,如果releaseYear是2015,我将放入localhost:8080 / api / films / 2015并查看数据库中存储的当年在Json发布的电影。
My domain (Film.class)
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Director {
private int directorId;
private String directorFirstName;
private String directorSurname;
private List<Film> films = new ArrayList<>();
}
其余消费者中的我的FilmController:
@Controller
@Slf4j
public class FilmController {
@GetMapping("/films/{releaseYear}")
public String showFilmsByYear(@PathVariable(name="releaseYear") int releaseYear,Model model)
{
try {
RestTemplate restTemplate = new RestTemplate();
String URL="http://localhost:8080/api/films/{releaseYear}";
ParameterizedTypeReference<List<Film>> listofFilms = new ParameterizedTypeReference<List<Film>>(){};
ResponseEntity<List<Film>> responseEntity = restTemplate.exchange(URL,HttpMethod.GET,null,listofFilms );
List<Film> films = responseEntity.getBody();
log.info(responseEntity.getHeaders().toString());
model.addAttribute("films",films);
return "films";
}
catch (HttpClientErrorException ex) {
model.addAttribute("exception",ex); //exception isnt handled because hitting whitelabel
return "notfound";
}
}
我的FormObject:
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class FilmForm {
@Size(min=2,max=20)
@NotEmpty
@NotBlank
private String filmName;
//add minimum year and max year (won't always be 2020,but can't allow films that are further then current year)
@PastOrPresent
@NotBlank
@Min(1888)
private int releaseYear;
@NotEmpty
private Director director;
}
我的html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<div th:insert="fragments.html :: head"></div>
<body>
<header>
<div th:insert="fragments.html :: menu"></div>
<h1><span th:text="#{films.heading}"></span></h1>
</header>
<main>
<table>
<tr>
<th th:text="#{films.id}"></th>
<th th:text="#{films.filmName}"></th>
<th th:text="#{films.directorFirstName}"></th>
<th th:text="#{films.directorSurname}"></th>
<th th:text="#{films.releaseYear}"></th>
</tr>
<tr th:each="film,iterationCount : ${films}" th:class="${iterationCount.even}? 'even'" th:object="${film}" >
<td th:text="*{filmId}"></td>
<td th:text="*{filmName}"></td>
<td th:text="*{director.directorFirstName}"></td>
<td th:text="*{director.directorSurname}"></td>
<th th:text="*{releaseYear}"></th>
</tr>
</table>
</main>
</body>
<footer th:insert="fragments.html :: footer">
</footer>
</html>
它不需要表单,因为它不使用html,但我只是想为了清楚起见就包括在内。
我想在Movies项目中访问的控制器是:
@RestController
@RequestMapping("/api")
public class RestControllerDir{
@Autowired
FilmService filmService;
@Autowired
DirectorService directorService;
@GetMapping("directors")
public List<Director> myRestDirectors(){
return directorService.listInAlphabeticalOrder();
}
@GetMapping("filmsByDirector/{id}")
public List<Film> myRestFilms(@PathVariable("id") int id)
{
return directorService.findDirector(id).getFilms(); //returns the films directed by a particular director
}
@GetMapping("viewFilmsByYear/{releaseYear}")
public List<Film> myRestFilmsYear(@PathVariable("releaseYear") int releaseYear)
{
return filmService.findFilmsByReleaseYear(releaseYear); //returns the films released in a particular year
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)