Spring Get Mapping无效

时间:2018-02-28 12:34:00

标签: spring angular spring-boot httprequest

我正在使用带有Spring后端的Angular 4。而....我的帖子请求工作正常。但是get请求不起作用。看代码:

前端服务:

import { Injectable } from '@angular/core';
import 'rxjs/add/operator/toPromise';
import { Aluno } from '../models/aluno.model';
import {HttpClient} from "@angular/common/http";
import {Observable} from "rxjs/Observable";

@Injectable()
export class UserDataService {

   private url = 'http://localhost:8080/getAluno';

   constructor(private http: HttpClient){}

   getAluno():Observable<Aluno>{
      return this.http.get<Aluno>(this.url);
   }
}

我的春季后端提供商:

import com.google.gson.Gson;
import com.ifmg.tcc.TADs.Aluno;
import com.ifmg.tcc.TADs.LoginObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@CrossOrigin(origins = "http://localhost:4200")
@RestController
public class LoginProvider {

  Gson gson = new Gson();

  private boolean verify(LoginObject lo){
    if(lo.getCode().equals("lucas") && lo.getPassword().equals("12345")){
        return true;
    }else{
        return false;
    }
  }

  @PostMapping(value = "login")
  public ResponseEntity<?> verifyLogin(@RequestBody String login){
    System.out.println("LOGIN REALIZADO");
    LoginObject loginO = gson.fromJson(login, LoginObject.class);

    if(verify(loginO)){
        return new ResponseEntity<Boolean>(true,HttpStatus.OK);
    }else{
        return new ResponseEntity<Boolean>(false,HttpStatus.OK);
    }


  }

  @GetMapping(value = "/getAluno")
  public @ResponseBody ResponseEntity<?> getAluno() {
    System.out.println("PEGANDO ALUNO");
    Aluno aluno = new Aluno("0001","Lucas Alves de Faria","2018/1","10/12/1995",true,"luke@email.com","(37) 999597899","127.831.956-58","MG-19.319.265");

    return new ResponseEntity<Aluno>(aluno, HttpStatus.OK);
  }
}

该函数验证我的后端登录工作正常(我在其他服务中调用此函数)。但是fontend不会调用另一个函数(getAluno)

1 个答案:

答案 0 :(得分:0)

我不确定你说什么时的意思&#34;但是fontend不会调用另一个函数(getAluno)&#34;。您可以查看网络呼叫并验证呼叫。

在调用服务的前端组件中,您需要进行订阅,如下所示:

this.yourService.getAluno().subscribe(
           (res: ResponseEntity) => {
                this.aluno = res.json;
            },
            (res: ResponseEntity) => this.onError(res.json)
);

希望它对你有所帮助。