Как сделать запрос на исправление http в angular7 с помощью Spring-Data-Rest EndPoint? - PullRequest
0 голосов
/ 15 мая 2019

У меня есть две сущности "студент" и "учетная запись" с отношением @OneToOne, я сделал id_account внешний ключ в студенте стол. Я попытался изменить значение внешнего ключа с помощью запроса на исправление HTTP из приложения angular7, вызвав spring-data-rest @RepositoryRestResource endPoint, но он не обновляет значение внешнего ключа.

Сначала я попытался вызвать свою конечную точку с помощью Postman, и она работает нормально, используя простой вызов с телом JSON, например:

{
"account": "http://localhost:8080/accounts/1"
}

Но когда я попробовал с моим угловым приложением, оно не работает, и это не дает мне никакой ошибки. так как сделать такой запрос, используя httpClient запрос в angular7?

Вот мой код:

Студенческое юридическое лицо:

@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;


    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "account")
    private Account account;
   //Getters and Setters and Constructor

Субъект счета:

@Entity
@Table(name = "account")
public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @OneToOne(mappedBy = "account")
    private  Student student;

    //Getters and Setters and Constructor

Хранилище для студентов:

@RepositoryRestResource
public interface StudentRepository extends PagingAndSortingRepository<Student, Integer> {
}

На стороне клиента, используя angular7 student.service.ts

@Injectable({
  providedIn: 'root'
})
export class StudentsService  {

  constructor(private http: HttpClient) {}

  getAll() {
    return this.http.get('http://localhost:8080/students').pipe(
      map((response: any) => response)
    );
  }

  update(id, account) {

    return this.http.patch(`http://localhost:8080/students/${id}`, account).pipe(
      map((response) => response)
    );
  }
}

students.components.ts

@Component({
  selector: 'app-students',
  templateUrl: './students.component.html',
  styleUrls: ['./students.component.css']
})
export class studentsComponent implements OnInit {

//For testing
id = 2;
account = {id: 1, username: "test", password: "test"} ; 

 constructor(private serviceStudents: StudentsService) {}

  ngOnInit() {
  }

  update(){
 this.studentService.update(this.id, this.account).subscribe(
  result => console.log(result));
  }    
}

Кто-нибудь может мне помочь с этой проблемой?

...