2015-10-27 25 views
1

我想從Angular工廠調用JavaEE 6休息服務,並且遇到問題。從角度調用Java EE休息服務

java rest服務是由其他人創建的,我試圖解決這個問題,而且我對JavaEE還不是很熟悉,所以如果你需要更多的信息,我會在能夠。

@Path("bills") 
public class BillResource { 

@EJB 
@Resource 
private BillableEventBean billableEventBean; 

private final BillableEventMapper billableEventMapper = new BillableEventMapper(); 

BillService implementation not working 
@POST 
@Consumes(MediaType.APPLICATION_JSON) 
@Produces(MediaType.APPLICATION_JSON) 
@Path("query") 
public List<BillableEventDuplicate> listBillableEvents(BillableEventQueryFilter queryFilter) { 
    List<BillableEvent> ejbRet = billableEventBean.listBillableEvents(queryFilter.getUserID(), 
      queryFilter.getBillingTeamID(), queryFilter.getBillStatus(), null); 
    List<BillableEventDuplicate> ret = billableEventMapper.toBillableEventDuplicateList(ejbRet); 
    return ret; 
} 

@GET 
@Produces(MediaType.APPLICATION_JSON) 
@Path("{billableEventI}") 
public BillableEventDuplicate getBillableEvent(@PathParam("billableEventI") String id) { 
    BillableEvent ejbRet = billableEventBean.getBillableEvent(id); 
    BillableEventDuplicate ret = billableEventMapper.toBillableEventDuplicate(ejbRet); 
    return ret; 
} 
} 

我的角度工廠服務看起來是這樣的:

'use strict'; 

var aumBills = angular.module('appBills', ['ngResource']); 

appBills.factory('Bills', ['$resource', 
    function($resource) 
    { 
     return $resource('/ua_appcore/api/v1/bills/:billNumber', 
     { 
      billNumber:'@billNumber' 
     }, 
     { 
      getList: {method: 'POST', params: {'userID':'ABC123'}, url: '/ua_appcore/api/v1/bills/query/'} 
     }); 
    }]); 

從控制器正是如此調用工廠:

'use strict'; 

angular.module('app.bills', ['ngRoute','appBills']) 

.config(['$routeProvider', function($routeProvider) 
{ 
    $routeProvider. 
    when('/bills', 
    { 
     templateUrl: 'bills/bill-list.html', 
     controller: 'BillListCtrl' 
    }). 
    when('/bills/:billId', 
    { 
     templateUrl: 'bills/bill-detail.html', 
     controller: 'BillDetailCtrl' 
    }). 
    when('/billsearch', 
    { 
     templateUrl: 'bills/bill-search.html', 
     controller: 'BillSearchCtrl' 
    }); 
}]) 

.controller('BillListCtrl', ['$scope', '$http', '$routeParams', 'Bills', 
function($scope, $http, $routeParams, Bills) 
{ 
    Bills.getList({},{}).$promise.then(function(billList) 
    { 
    $scope.billList = billList; 
    }); 
}]) 

.controller('BillDetailCtrl', ['$scope', '$http', '$routeParams', 'Bills', 
    function($scope, $http, $routeParams, Bills) 
    { 
    Bills.get({},{billNumber: $routeParams.billNumber }).$promise.then(function(bill) 
    { 
     $scope.bill = bill; 
    }); 
    }]); 

在我的理解,我需要創建一個自定義在工廠中使用URL選項,因爲有POST可以使用相同的根呼叫返回賬單清單。我遇到的問題是,即使使用Consumes註釋,我似乎也無法將任何參數送入queryFilter對象。我在做一些根本性錯誤?

回答

0

所以,事實證明,這是一個基礎設施問題的全部時間。我們一直在部署到Websphere Liberty服務器,它不包含JavaEE 6需要識別剩餘服務的整個WebProfile。他們可能需要一些解決方法,但最快的短期解決方案是運行完整的Websphere服務器進行部署。

感謝您的幫助!

2

首先,您可以使用Chrome插件Postman檢查您的其餘服務是否正常工作,如果爲true,那麼您的angular $資源存在問題。在我看來,請求中缺少標題「Content-Type = application/json」。我從來沒有以這種方式使用的角度,你可以嘗試這樣的創建服務(this教程應該也很有幫助)


app.service("billsService", function ($resource) { 

    var bills = $resource("/ua_appcore/api/v1/bills/query"); 

    this.getBills = function() { 
     var billsResource = new bills(); 
     billsResource.sampleVar = 'value'; // here you can place your vars 
     return billsResource.$save(); 
    } 
});