Ionic中的Post方法返回404

时间:2015-12-25 22:52:23

标签: angularjs post ionic

我在基于REST架构的服务中实现post方法时遇到问题。 所有get方法都正常工作,但是当我运行post时,我得到状态404.

我想发送整数列表。 我正在使用工厂提供方法并在控制器中使用它们

服务文件

 angular.module('groups.services', ['ngResource', 'ngRoute', 'constants'])
    .factory('Group', function ($resource, MAIN) {
      return {
        getGroupStudents: function () {
          return $resource(MAIN.url + '/student?groupId=:groupId');
        },
        saveTimesheet: function () {
          return $resource(MAIN.url + '/timesheet', null, {
            save: {
              method: 'POST',
              isArray: true
            }
          }, {headers: {"Content-Type": 'application/json'}});
        }
      }
    });

控制器

 angular.module('groups.controller', ['groups.services'])
     .controller('GroupCtrl', function ($scope, $http, $state, $stateParams, Group) {
           var presence = [];

           $scope.$on('$ionicView.beforeEnter', function () {
               presence = [];
           });

           $scope.students = Group.getGroupStudents().query({groupId: $stateParams.groupId});

           $scope.checkTimesheet = function () {
              $state.transitionTo('app.timesheet', {'groupId': $stateParams.groupId});
           };

           $scope.addToPresenceList = function (student) {
              if (student.checked) {
                 presence.push(student.id);
              } else {
                 var index = presence.indexOf(student.id);
                 presence.splice(index, 1);
              }
           };

           $scope.saveTimesheet = function () {
                 Group.saveTimesheet().save({ "studentIds": presence});
           };

       });
服务器端的

api(java + jersey)

@Path(TimesheetWebApi.BASE_PATH)
public interface TimesheetWebApi {

    public static final String BASE_PATH = "/timesheet";

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response saveTimesheet(@NotNull List<Integer> studentIds);
}

请求有效负载

{studentIds: [1, 2]}

请求

Request URL:http://localhost:8100/app/timesheet
Request Method:POST
Status Code:404 Not Found
Remote Address:127.0.0.1:8100

任何提示?我做错了什么?

1 个答案:

答案 0 :(得分:1)

问题是服务器端缺少bean注册。

@Component
@ApplicationPath(JerseyConfig.APPLICATION_PATH)
public class JerseyConfig extends ResourceConfig {
    static final String APPLICATION_PATH = "/app";

    public JerseyConfig() {
        register(TimesheetWebApiImpl.class);
    }
}