我的客户有棱角,粗口,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
一些功能:
关于angularjs - Angular $ http.delete + hapijs使用swagger + grails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32110388/