如何使用RestTemplate来检索对象

huangapple go评论70阅读模式
英文:

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[]

huangapple
  • 本文由 发表于 2020年10月20日 02:26:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64433171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定