Andrew Karstaedt
Andrew Karstaedt

Reputation: 165

Calling Java EE rest service from angular

I'm trying to call a JavaEE 6 rest service from an Angular factory and I am running into issues.

The java rest service was created by someone else and I'm trying to work off of it and I'm not super-well versed in JavaEE yet, so if you need more info, I'll chip in where I can.

@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;
}
}

My angular factory for the service looks like this:

'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/'}
        });
    }]);

The factory in invoked from the controller thusly:

'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;
    });
  }]);

In my understanding, I needed to create a custom action in the factory to make use of the URL option since there is a POST to get a list of bills back using the same root call. The problem I'm having is that I can't seem to be able to feed any parameters into the queryFilter object even with the Consumes annotation. Am I doing something fundamentally wrong?

Upvotes: 1

Views: 739

Answers (2)

Andrew Karstaedt
Andrew Karstaedt

Reputation: 165

So, it turns out that it was an infrastructure issue the entire time. We had been deploying to a Websphere Liberty server, and it does not contain the entire WebProfile that JavaEE 6 needs to recognize the rest services. Their may be some work-arounds for it, but the quickest short-term solution is to run a full Websphere server for deployments.

Thanks for the help!

Upvotes: 0

burovmarley
burovmarley

Reputation: 644

First of all you can use chrome plugin Postman to check if your rest service is working correctly if true there is problem with your angular $resource. In my opinion there is missing header "Content-Type=application/json" in your request. I've never use angular in that way you can try to create service like that (this tutorial should be also helpful)


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();
    }
});

Upvotes: 2

Related Questions