角度为7

时间:2019-05-21 11:25:58

标签: angular rest http glassfish

我试图将两个数据发送到在glassfish上运行的RESTful服务器。用户必须在数据列表中选择一家餐馆。我想发送餐厅的ID和名称。使用console.log可以打印出id和name的值,但是我的响应返回null。知道为什么会这样以及如何解决吗?

ts文件

export class OrganizeComponent implements OnInit {

  public codeValue: string;

  codeList = [
    { id: 1, name: 'Mcdonalds' },
    { id: 2, name: 'Kentucky Fried Chicken' },
    { id: 3, name: 'Burger King' },
    { id: 4, name: 'Domino`s pizza' },
    { id: 5, name: 'New York Pizza' }
  ];


  @ViewChild('f') form: NgForm;
  restaurant = {
    id: 0,
    name: ''
  };



  constructor(private http: HttpClient) {
  }

  ngOnInit() {
  }

  public saveCode(e): void {
    let name = e.target.value;
    let list = this.codeList.filter(x => x.name === name)[0];
    // console.log(list.id);
    // console.log(list.name)


    this.restaurant.id =  list.id;
    this.restaurant.name = list.name;

console.log(list.id);
console.log(list.name);

    this.http.post('http://localhost:8080/aquadine-jee/resources/restaurant', {
      id: list.id,
      name: list.name
    })


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

HTML文件

<label for="codes">Choose a restaurant:</label>
    <form
      (ngSubmit)="onOrganize(f)"
      #f="ngForm">
      <input type="text"  list="codes"  [(ngModel)]=codeValue [ngModelOptions]="{standalone: true}" (change)="saveCode($event)">
      <datalist id="codes">
        <option *ngFor="let c of codeList" [value]="c.name" >{{c.name}}</option>
      </datalist>
    </form>

后端代码:

@Stateless
@Path("restaurant")
public class RestaurantResource {

    @Inject
    private RepositoryService repositoryService;

    @GET
    @Path("restaurant")
    @Produces("application/json")
    public Response all(){
        List<Restaurant> all = repositoryService.getAllRestaurants();
        return Response
                .status(200)
                .header("Access-Control-Allow-Origin", "*")
                .entity(all)
                .build();
    }


    @GET
    @Produces("application/json")
    public List<Restaurant> getAllRestaurants(){
        return repositoryService.getAllRestaurants();

    }

    @POST
    @Consumes("application/json")
    public Response save(Restaurant restaurant){
        repositoryService.save(restaurant);
        return Response
                .status(201)
                .build();
    }
public class Restaurant {

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



    public Restaurant(Integer id,Integer restaurantId , String naam) {
        this.id = id;
        this.restaurantId = restaurantId;
        this.naam = naam;
    }

后端服务错误:

  

内部服务器错误500。   EJB RepositoryService上的调用,方法:public void   nl.aquadine.service.RepositoryService.save(nl.aquadine.model.Restaurant)]]   和javax.ejb.TransactionRolledbackLocalException:引发异常   由bean引起,由:org.hibernate.PersistentObjectException:   传递给持久对象的独立实体:位于nl.aquadine.model.Restaurant   org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124)   在org.hibernate.event.internal.DefaultPersis等处。

2 个答案:

答案 0 :(得分:0)

在代码中添加以下内容

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1


#processManagement:

security:
  authorization: "enabled"

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

答案 1 :(得分:-2)

  1. 首先用POSTMAN或其他任何收费方式进行测试,以测试后端响应(如果其工作正常),那么您可以专注于前端。