简体   繁体   English

使用 Angular 2 和 Spring Boot 发送 post 请求

[英]Send post request with Angular 2 and Spring Boot

I use Angular 2 and Spring Boot.我使用 Angular 2 和 Spring Boot。 From an html form I want to send data to a local mongo db.从 html 表单我想将数据发送到本地 mongo db。 The form has a Submit Function which should send the data to the PersonController Java file.表单有一个提交函数,它应该将数据发送到 PersonController Java 文件。 But the post request does not work.但是post请求不起作用。 I hope you can help me.我希望你能帮助我。 Thanks in advance.提前致谢。

onSubmit() Function onSubmit() 函数

    private serverUrl = 'http://localhost:8080/saveUser';

onSubmit() {

    let headers = new Headers({'Content-Type': 'application/json'});
    this.http.post(this.serverUrl,
        {firstName: 'name.firstname',lastName: 'name.lastname'},
        {headers:headers})
        .map((res: Response) => res.json());

    this.name = {
        firstname: '',
        lastname: ''
    }

HTML Code: HTML代码:

 <form (ngSubmit)="onSubmit()" #nameForm="ngForm" >
  <div class="form-group">
    <label for="Firstname">Firstname</label>
    <input type="text" class="form-control" id="firstname"
    required
    [(ngModel)]="name.firstname" name="firstname">
  </div>

  <div class="form-group">
    <label for="Lastname">Lastname</label>
    <input type="text" class="form-control" id="lastname"
    [(ngModel)]="name.lastname" name="lastname">
  </div>
  <button type="submit" class="btn btn-default" >Submit</button>
</form>

PersonController个人控制器

@Autowired
PersonRepository personRepository;

@RequestMapping("/saveUser")
public String personForm(){
    Person person1 = new Person("Hans", "Meiser");
    personRepository.save(person1);
    return "saved";
}

sa, in your spring code your @RequestMapping("/saveUser") is bydefault mapped as GET . sa,在您的弹簧代码中,您的@RequestMapping("/saveUser")默认映射为GET so you still add one property as given bellow.所以你仍然添加一个属性,如下所示。

can you pass method property in this mapping and can check it.你可以在这个映射中传递方法属性并可以检查它。

@RequestMapping(value="/saveUser" , method=RequestMethod.POST @RequestMapping(value="/saveUser" , method=RequestMethod.POST

you have to set the request type.您必须设置请求类型。 also you have to set how you will recieve these input data.您还必须设置如何接收这些输入数据。 if you are sending them as a request body then you have to recieve them as the following as you are casting the input data to person to be saved directly.如果您将它们作为请求正文发送,那么您必须按照以下方式接收它们,因为您将输入数据转换为要直接保存的人。

@RequestMapping(value="/saveUser", method=RequestMethod.POST)
public String personForm(@RequestBody Person person){
    personRepository.save(person);
    return "saved";
}

Your Spring request is a GET one and you are trying to call a POST method from angular.您的 Spring 请求是 GET 请求,而您正尝试从 angular 调用 POST 方法。 And in angular you need to subscribe in order for the http call to be fired.在 angular 中,您需要订阅才能触发 http 调用。

Change to @PostMapping('/users') When creating CRUD operation is a good naming practice to have the method type and the name is the name of the domain on plural.更改为@PostMapping('/users')在创建 CRUD 操作时,使用方法类型和名称是复数形式的域名称是一个很好的命名实践。

Spring春天

@PostMapping('/users')
public Person saveUser(@RequestBody Person person) {
 return personRepository.save(person);
}

Angular

private baseUrl= 'http://localhost:8080';
const user = {firstName: this.name.firstname, lastName: this.name.lastname};
this.http.post(`${this.baseUrl}/user}`, user).subscribe(value => this.user = user);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM