Angular POST请求到休息服务器显示为空,MySql数据库显示正确的数据

时间:2019-06-02 22:33:28

标签: mysql angular rest http glassfish

我想将数据从Angular前端发送到我的Glassfish Rest服务器,再通过其余服务器发送到我的数据库。当我提交POST请求时,数据将正确发送到数据库,但是当我检查我的REST服务器时,我看到的只是“空”。我该如何解决?

我将提供一些我在评论中看到的图像。

前端:

 public codeValue: string;

  codeList = [
    { restaurantId: 1, restaurantName: 'Mcdonalds', restaurantAdres: 'Kalverstraat 5' },
    { restaurantId: 2, restaurantName: 'Kentucky Fried Chicken', restaurantAdres: 'Kalverstraat 4' },
    { restaurantId: 3, restaurantName: 'Burger King', restaurantAdres: 'Kalverstraat 3' },
    { restaurantId: 4, restaurantName: 'Dominos pizza', restaurantAdres: 'Kalverstraat 2' },
    { restaurantId: 5, restaurantName: 'New York Pizza', restaurantAdres: 'Kalverstraat 1' }
  ];


  @ViewChild('f') form: NgForm;
  restaurant = {
    restaurantId: " ",
    restaurantName: " ",
    restaurantAdres: " "
  };
  httpOptions = {
    headers: new HttpHeaders({
      'Content-Type':  'application/json',
      'Authorization': 'my-auth-token'
    })
  };
  private string1: string;


  constructor(private http: HttpClient) {
  }

  ngOnInit() {
  }

  public saveCode(e): void {
    let restaurantName = e.target.value;
    let list = this.codeList.filter(x => x.restaurantName === restaurantName)[0];


    this.restaurant.restaurantId = list.restaurantId.toString();
    this.restaurant.restaurantName = list.restaurantName;
    this.restaurant.restaurantAdres = list.restaurantAdres;

    console.log(list.restaurantId);
    console.log(list.restaurantName);
    console.log(list.restaurantAdres);



    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json'
      })
    };

    const data = {
      restaurantId: list.restaurantId.toString(),
      restaurantName: list.restaurantName,
      restaurantAdres: list.restaurantAdres
    };

    console.log(data)


    this.http.post('http://localhost:8080/aquadine-jee/resources/restaurant',
      JSON.parse(JSON.stringify(data)) , httpOptions)


      .subscribe( // subscribe to observable http.post
        res => {
          console.log("response" + " " + res); // log results otherwise log error
        },
        err => {
          console.log('Error occured');
        }
      );
  }

后端:

package nl.aquadine.model;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;

/**
 * @author Fethi
 */

@Entity
@Table(name = "restaurant")
@NamedQueries({
        @NamedQuery(name = "Restaurant.findOne", query = "select m from Restaurant m where m.id = :id"),
        @NamedQuery(name = "Restaurant.getAll", query = "select m from Restaurant m")
})
public class Restaurant implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;



    //    @NotBlank
    String restaurantName;


    //    @NotBlank
    String restaurantAdres;


    int restaurantId;

    public Restaurant(){

    }


    public Restaurant(int restaurantId, String restaurantName, String restaurantAdres) {
        this.restaurantId = restaurantId;
        this.restaurantName = restaurantName;
        this.restaurantAdres = restaurantAdres;
    }


    public Integer getId() {
        return id;
    }

    public String getRestaurantName() {
        return restaurantName;
    }

    public String getRestaurantAdres() {
        return restaurantAdres;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setRestaurantName(String restaurantName) {
        this.restaurantName = restaurantName;
    }

    public void setRestaurantAdres(String restaurantAdres) {
        this.restaurantAdres = restaurantAdres;
    }


    public int getRestaurantId() {
        return restaurantId;
    }

    public void setRestaurantId(int restaurantId) {
        this.restaurantId = restaurantId;
    }
}

存储库:


public List<Restaurant> getAllRestaurants() {
        EntityManager em = entityManagerFactory.createEntityManager();
        List<Restaurant> restaurants = em.createQuery("SELECT r FROM Restaurant r").getResultList();
        em.close();
        return restaurants;
    }

    @Override
    public Restaurant find(Integer id) {
        EntityManager em = getEntityManager();
        Restaurant restaurant = em.find(Restaurant.class, id);
        em.close();
        return restaurant;
    }

    @Override
    public Restaurant save(Restaurant restaurant) {
        EntityManager em = getEntityManager();

        em.getTransaction().begin();
        em.persist(restaurant);
        em.getTransaction().commit();
        em.close();

        return restaurant;


    }

    @Override
    public Restaurant update(Restaurant restaurant) {
return null;
    }

    @Override
    public Restaurant delete(Restaurant restaurant) {
return null;
    }

1 个答案:

答案 0 :(得分:0)

您可能在这行代码中做错了

this.http.post('http://localhost:8080/aquadine-jee/resources/restaurant',
      JSON.parse(JSON.stringify(data)) , httpOptions)

您应该将代码更改为

this.http.post('http://localhost:8080/aquadine-jee/resources/restaurant',
      data) , httpOptions)

或使用JSON.stringify,但您已解析为服务器中的json字符串

this.http.post('http://localhost:8080/aquadine-jee/resources/restaurant',
      JSON.stringify(data)) , httpOptions)