java - 使用 spring boot 使用 api
问题描述
我是 Spring 新手,我正在尝试使用通过使用 Student Repository 中的注释 @RestRepositoryResources 创建的 API,如下所示:
@RepositoryRestResource
public interface StudentRepository extends JpaRepository<Student, Long> {
}
所以,我想从另一个项目中使用 API,如下所示:
@RestController
public class StudentController {
@Autowired
private RestTemplate restTemplate;
@GetMapping
public List<Student> getStudents(){
ListStudent response=restTemplate.getForObject("http://localhost:8080/student-service/students", ListStudent.class);
System.out.println(response);
return response.getStudents();
}
}
当然这不起作用,因为响应的格式
{
"_embedded": {
"students": []
},
"_links": {
"self": {
"href": "http://localhost:8081/students"
},
"profile": {
"href": "http://localhost:8081/profile/students"
}
},
"page": {
"size": 20,
"totalElements": 0,
"totalPages": 0,
"number": 0
}
}
解决方案
我不确定是什么ListStudent
,但您的类需要匹配 JSON 响应,尽管它可以省略您不需要的任何内容。你需要一个StudentResponse
类之类的东西,其中包含一个List<Student>
.
推荐阅读
- node.js - Node.js 测试在 toLocaleString 上失败
- swift - 如果我想稍后重构为异步/等待,我应该使用回调还是 PromiseKit?
- python - 如何在 cdk python 中为 Artifacts 使用预先存在的 Amazon S3 存储桶?
- crystal-reports - 打印托盘标签时,在 Crystal 中循环“超过最大值”
- python - TensorFlow 数据集元素在指定范围内随机播放
- kotlin - 为什么没有“if”语句该函数不起作用?
- angular - 刷新后角度路由数据为空或在浏览器中输入直接 url
- json - 将 JSON 从 Unity 应用程序传递到 Firebase 函数,onCall 结果未定义
- reactjs - Redux saga 在 DOM 事件中按顺序调度多个动作
- android - 自 9 月 1 日以来的 android 10 `SSL 握手超时`