0

I have a REST service with end point http://localhost:8080/hero-rest-service/getheroes. Response from this service is

[{"id":100,"name":"Test1"},{"id":200,"name":"Test2"},{"id":300,"name":"Test3"},{"id":400,"name":"Test4"},{"id":500,"name":"Test5"}]

I am trying to call this from Angular2 with the below class

import { Injectable }     from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Hero }           from '../beans/hero';
@Injectable()
export class HeroSearchService {
    constructor(private http: Http) {}
    getHeroes(): Observable<Hero[]> {
        return this.http
            .get(`http://localhost:8080/hero-rest-service/getheroes`)
            .map((r: Response) => r.json().data as Hero[])
            .catch(this._serverError);
    }
    private _serverError(err: any) {
        console.log('Sever error:', err);
        if(err instanceof Response) {
            return Observable.throw(err.json().error || 'backend server error');
        }
        return Observable.throw(err || 'backend server error');
    }
}

But its failing and the console error is

Sever error: 
error: "Collection 'getheroes' not found"
status: 404
statusText: "Not Found"
type: null
url: null

Hero class is

export class Hero {
    id: number;
    name: string;
}

This is my Rest Controller

@RestController
public class HeroRestController {

    @RequestMapping("/")
    public String welcome() {
        return "Welcome to Angular2 Heroes Spring REST Service.";
    }

    @ResponseBody
    @RequestMapping("/getheroes")
    @CrossOrigin(origins = "http://localhost:3000")
    public List<Message> getHeroes() {
        System.out.println("Inside getHeroes");
        List<Message> msgs = new ArrayList<>();
        Message msg1 = new Message(100, "Test1");
        Message msg2 = new Message(200, "Test2");
        Message msg3 = new Message(300, "Test3");
        Message msg4 = new Message(400, "Test4");
        Message msg5 = new Message(500, "Test5");
        msgs.add(msg1);
        msgs.add(msg2);
        msgs.add(msg3);
        msgs.add(msg4);
        msgs.add(msg5);
        return msgs;
    }
}

Can any one please help me?

Ani
  • 722
  • 9
  • 27

0 Answers0