angularjs - Angular $ http.delete + hapijs使用swagger + grails

标签 angularjs grails swagger hapijs

我的客户有棱角,粗口,hapijs和招摇。该客户端与在grails上运行的WS连接。该WS具有Swagger.json。

问题是我无法删除作者。我不知道怎么了

我的 Angular 工厂

(function () {
    'use strict';

    angular
        .module('qrbweb')
        .factory('AuthorFactory', ['$http', AuthorFactory]);

    function AuthorFactory($http) {

        var authors = [];

        return {
            list: function () {
                $http.get("http://localhost:3000/authors").then(function (response) {
                    authors = response.data;
                });
            },
            delete: function (id) {
                console.log(id)
                $http.delete("http://localhost:3000/authors/" + id)
                    .success(function (result) {
                        console.log(result);
                    }).error(function () {
                        console.log("error");
                    });
            },
            get: function () {
                return authors;
            }
        };
    }
})();

我的hapijs index.js
var hapi = require('hapi');
var server = new hapi.Server();
var intert = require('inert');

server.connection({port: 3000});

server.register(intert, function () {
});

server.route({
    method: 'GET',
    path: '/{param*}',
    handler: {
        directory: {
            path: 'src/client/main/',
            redirectToSlash: true,
            index: true
        }
    }
});

server.route({
    method: 'GET',
    path: '/author/{params*}',
    handler: {
        directory: {
            path: 'src/client/main/app/components/author/list/author.list.html'
        }
    }
});

server.route({
    method: 'GET',
    path: '/bower_components/{params*}',
    handler: {
        directory: {
            path: 'bower_components/'
        }
    }
});

server.route({
    method: 'GET',
    path: '/app/{params*}',
    handler: {
        directory: {
            path: 'src/client/main/app/'
        }
    }
});

/**
 * Dynamic routes
 */
var client = require('swagger-client');

server.route({
        method: 'GET',
        path: '/authors/{param*}',
        handler: function (request, reply) {
            var swagger = new client({
                url: 'http://localhost:8080/swagger.json',
                success: function () {
                    swagger.author.list({max: 10, offset: 0}, function (response) {
                        reply(response.data).type('application/json')
                    });
                }
            });
        }
    }
);

server.route({
        method: 'delete',
        path: '/authors/{id}',
        handler: function (request, reply) {
            var swagger = new client({
                url: 'http://localhost:8080/swagger.json',
                success: function () {
                    swagger.author.delete({authorId: request.params.id}, function (response) {
                        reply(response.data).type('application/json')
                    });
                }
            });
        }
    }
);

server.start(function () {
    console.log('Running server at ', server.info.uri);
});

Swagger.json
{
  "swagger": "2.0",
  "info": {
    "version": "1.0.0",
    "title": "Swagger QRBWS",
    "description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification",
    "termsOfService": "http://swagger.io/terms/",
    "contact": {
      "name": "Swagger API Team"
    },
    "license": {
      "name": "MIT"
    }
  },
  "host": "localhost:8080",
  "basePath": "/api",
  "schemes": [
    "http"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/author": {
      "get": {
        "tags": [
          "author"
        ],
        "operationId": "list",
        "description": "Returns all pets from the system that the user has access to",
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "max",
            "in": "query",
            "description": "The maximum number to list",
            "type": "integer",
            "default": 10,
            "minimum": 1,
            "maximum": 100
          },
          {
            "name": "offset",
            "in": "query",
            "description": "The offset from the first result to list from",
            "type": "integer",
            "default": 0,
            "minimum": 0
          }
        ],
        "responses": {
          "200": {
            "description": "A list of pets.",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/Author"
              }
            }
          }
        }
      },
      "delete": {
        "tags": [
          "author"
        ],
        "summary": "Delete purchase order by ID",
        "description": "For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors",
        "operationId": "delete",
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "in": "query",
            "name": "authorId",
            "description": "Pet id to delete",
            "required": true,
            "type": "test",
            "format": "int64"
          }
        ],
        "responses": {
          "400": {
            "description": "Invalid ID supplied"
          },
          "404": {
            "description": "Order not found"
          }
        }
      }
    }
  },
  "definitions": {
    "Author": {
      "type": "object",
      "required": [
        "id",
        "name",
        "notes"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        },
        "name": {
          "type": "string"
        },
        "notes": {
          "type": "string"
        }
      }
    }
  }
}

当我发送删除作者的请求时,将生成:
Request URL:http://localhost:3000/authors/9 
Request Headers Provisional headers are shown
Accept:application/json,text/plain, */*
Origin:http://localhost:3000 
Referer:http://localhost:3000/author 
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36

,这是错误:
{ url: 'http://localhost:8080/api/author?authorId=8',   method: 'DELETE',   headers:     { server: 'Apache-Coyote/1.1',
     'x-application-context': 'application:development',
     allow: 'HEAD, GET',
     'content-type': 'application/json;charset=UTF-8',
     'transfer-encoding': 'chunked',
     date: 'Thu, 20 Aug 2015 05:31:36 GMT',
     connection: 'close' },   obj:     { [Error: Method Not Allowed]
     original: null,
     response: 
      { domain: [Object],
        _events: {},
        _maxListeners: undefined,
        res: [Object],
        request: [Object],
        req: [Object],
        links: {},
        text: '{"timestamp":1440048697000,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method \'DELETE\' not supported","path":"/api/author"}',
        body: [Object],
        files: {},
        buffered: true,
        headers: [Object],
        header: [Object],
        statusCode: 405,
        status: 405,
        statusType: 4,
        info: false,
        ok: false,
        redirect: false,
        clientError: true,
        serverError: false,
        error: [Object],
        accepted: false,
        noContent: false,
        badRequest: false,
        unauthorized: false,
        notAcceptable: false,
        forbidden: false,
        notFound: false,
        charset: 'UTF-8',
        type: 'application/json',
        setEncoding: [Function],
        redirects: [] },
     status: 405 },   status: 405,   statusText: '{"timestamp":1440048697000,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method \'DELETE\' not supported","path":"/api/author"}',   data: '{"timestamp":1440048697000,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method \'DELETE\' not supported","path":"/api/author"}' }

最佳答案

好吧,我正在寻找解决方案,并为AngularJS 1.4+的Swagger Specification 2.0 Client创建了自己的集成器

您可以在此处了解更多信息:
https://github.com/olaferlandsen/angular-swagger2-client

一些功能:

  • 支持POST,PUT,GET,DELETE,PATCH和CONNECT请求。
  • 支持以下参数:查询,路径,formData和 header 。
  • 支持apit中具有安全性的
  • SecuritySchema(仅类型apiKey)。
  • 默认情况下,PUT和POST请求以内容类型发送:application / x-www-form-urlencoded; charset = utf-8。
  • 删除API定义中尚未建立的所有参数。
  • 为参数和支持的数据类型,格式和必需实现一个Pre-Validator。
  • 基于LocalStorage的全局静态和动态默认值。
  • 关于angularjs - Angular $ http.delete + hapijs使用swagger + grails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32110388/

    相关文章:

    javascript - 如何为通过 ng-view 指令或 ng-controller 指令创建的每个新范围设置监听器?

    grails - 在Groovy中生成音频波形

    java - 使用 maven 插件的离线 swagger 文档

    java - swagger-ui.html 400 错误请求

    angularjs - 在ajax请求和 session 超时期间,Grails没有响应401

    html - 使用 ng-repeat 时为表格中不存在的空格制作边框

    javascript - 在 Controller 中访问 ng-repeat 别名表达式

    hibernate - Grails 3-具有复合键的域对象并不总是保存

    grails - Grails index.gsp将操作视为 View 并引发错误

    swagger - 使用 Swagger 编辑器在 YAML 中定义一个带有数组的响应对象