使用angular2使用post方法添加数据时出现Rest-MongoDB错误

时间:2017-07-05 05:58:00

标签: java mongodb angular jersey

我正在为我的项目使用angular2,Rest和mongodb。我正在尝试使用post方法将数据发布到服务器。现在问题是我能够使用Postman将数据发布到服务器。数据正在变得越来越多但是当我尝试从前端添加数据时,我从服务器获得响应,但数据没有被添加到数据库中。我不明白为什么,因为如果使用邮递员添加数据,我不明白为什么不从前端添加数据

以下是我的代码

Angular Post方法

const url:string="http://localhost:8080/JarvisBackend/Jarvis/addLeave";
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(url,JSON.stringify(data),options).map(
  res=>{
    console.log(res);
    return res;
  }

休息服务

@POST
    @Path("/addLeave")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public String getUserById(LeaveApplyBean l) {
        System.out.println(l.getNum());
        int k = new leaveApplyDao().applyLeave(l);
        return "{}";
    }

DAO

public class leaveApplyDao {
    public int applyLeave(LeaveApplyBean lb) {
        try {


            MongoDatabase md = new MongoConnector().getConnection();
            Document details = new        FindApproverDao().finDetails(lb.getNum(), md);

            Document task = new Document();

            task.append("start_date", lb.getStartDate());
            task.append("end_date", lb.getEndDate());
            task.append("desciption", lb.getDescription());

            Document obj = new Document();
            obj.append("type", "Leave");
            obj.append("creator", lb.getNum());


            obj.append("approver", details.get("manager"));
            obj.append("cftid", details.get("cftid"));

            obj.append("status", "pending");

            obj.append("task", task);

            MongoCollection mc = md.getCollection("tasks");

            mc.insertOne(obj);

        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
  }

Helper Dao

public class FindApproverDao {

    public Document finDetails(String num,MongoDatabase md)
    {

        ArrayList list =null;
        FindIterable cursor = null;
        Document d=null;
        MongoCollection mc;
        try {
            BasicDBObject criteria =new BasicDBObject();
            criteria.append("num",num);

            mc = md.getCollection("employee");
             d = (Document) mc
                        .find(new BasicDBObject("num", num))
                         .projection(Projections.fields(Projections.include("manager","cftid"), Projections.excludeId())).first();

        } catch (Exception e) {
            e.printStackTrace();
        }
    System.out.println(d.get("manager"));
        return d;
    }
    public static void main(String[] args)
    {
         System.out.println(new FindApproverDao().finDetails("AHSH", new MongoConnector().getConnection()).get("cftid"));
    }


}

和bean类

public class LeaveApplyBean {

    private String num;
    private String startDate;
    private String endDate;
    private String description;

    public String getNum() {
        return num;
    }
    public void setNum(String num) {
        this.num = num;
    }
    public String getStartDate() {
        return startDate;
    }
    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }
    public String getEndDate() {
        return endDate;
    }
    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

以及服务器在控制台中的响应

  

回复{_body:" {}",状态:200,ok:true,statusText:"确定",标题:标题...}

请帮助。我被困住了。非常感谢

0 个答案:

没有答案