英文:
How to use Rest template for Retrieve Object
问题
我创建了两个小型微服务,一个是产品服务,另一个是目录服务。只有产品服务与数据库交互。产品服务与数据库的连接工作正常。但是我无法通过目录服务访问产品对象。
我收到以下错误提示:
>无法从 START_ARRAY 令牌中反序列化 Entity.Course
实例,位置:[来源:(PushbackInputStream);行:1,列:1]
我该如何解决?
@GetMapping("/catelog")
public Course getCourseObject() {
String CourseUrl = "http://localhost:9090/courses";
Course course;
RestTemplate rest = new RestTemplate();
course = rest.getForObject(CourseUrl, Course.class);
return course;
}
英文:
I create 2 small micro-services, one is a product service and other one is a catalog service. only the product service deals with the database. Product service is working perfectly with the database. but I cannot access the product Object via the catalog service.
I get the following error:
>Cannot deserialize instance of Entity.Course
out of START_ARRAY token at [Source:(PushbackInputStream); line: 1, column: 1]
How can I fix this?
@GetMapping("/catelog")
public Course getCourseObject() {
String CourseUrl="http://localhost:9090/courses";
Course course;
RestTemplate rest=new RestTemplate();
course =rest.getForObject(CourseUrl, Course.class);
return course;
}
答案1
得分: 1
我猜你的调用会返回一个数组...
return rest.getForObject(CourseUrl, Course[].class);
当然还要将返回类型从Course更改为Course[]。
英文:
I guess your call returns an array...so
return rest.getForObject(CourseUrl, Course[].class);
of course also edit the return type from Course to Course[]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论