Search code examples
angularangular7angular-services

Status 404 when replacing In memory services with Back end services


I created simple angular 7 app where i am displaying the user. First i was able to successfully displaying the users with In memory api services which I am trying to replace with Rest services. Rest service works fine as i am getting response in rest client. As I replace services with back end service it gives me status 404.

export class UserService {
    private usersUrl = 'api/users';
    baseUrl: string = 'http://localhost:8080/listUsers';
    constructor(private http: HttpClient) {}

    /* getUsers() : Observable<User[]>{
      return this.http.get<User[]>(this.usersUrl);
    }*/

    getUsers(): Observable < User[] > {
        return this.http.get < User[] > (this.baseUrl);
    }
}

export class DisplayUsersComponent implements OnInit {

    users: User[];
    selectedUser: User;
    constructor(private userService: UserService) {}
    user: User = {
        userId: 1,
        userName: 'Jordan'
    };
    ngOnInit() {
        this.getUsers();
    }
    getUsers(): void {
        this.userService.getUsers().subscribe(users => this.users = users);

    }
    onSelect(user: User): void {
        this.selectedUser = user;
    }
}

DisplayUsersComponent calls userservice . it worked for Api services but not working for actual back end services.Please guide me how can resolve this issue. Jordan


Solution

  • An error 404 indicates that the URL you are trying to visit isn't found. If you are sure that the URL is correct, try after restarting your backend server & then visit your http://localhost:8080/listUsers to ensure that it is accessible.

    Suggestion: You could use POSTMAN to test your APIs.