从角度

时间:2015-10-27 19:33:08

标签: java angularjs rest java-ee-6

我正试图从Angular工厂调用JavaEE 6休息服务,但我遇到了问题。

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来使用相同的根调用来获取帐单列表。我遇到的问题是,即使使用queryFilter注释,我似乎也无法将任何参数提供给Consumes对象。我做了一些根本错误的事情吗?

2 个答案:

答案 0 :(得分:2)

首先,如果你的angular $资源有问题,你可以使用chrome插件Postman来检查你的休息服务是否正常工作。在我看来,您的请求中缺少标题“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();
    }
});

答案 1 :(得分:0)

因此,事实证明这是一个基础设施问题。我们一直在部署到Websphere Liberty服务器,它不包含JavaEE 6识别其余服务所需的整个WebProfile。它们可能是一些解决方法,但最快的短期解决方案是运行完整的Websphere服务器进行部署。

感谢您的帮助!