【发布时间】:2017-01-01 03:59:47
【问题描述】:
所以我刚刚从 1.x 升级到 Swagger 2,并且遇到了一个奇怪的问题。我的一个 API 将不正确的内容类型注入到标头中,我不知道从哪里来,您可以在下面的 SwaggerJSON 中看到,DELETE 函数甚至说它消耗应用程序/json,但是 CURL(从检查面板复制) 因为它发送 'Content-Type: text/plain;charset=UTF-8'。我提供了 CREATE 函数作为一个粗略的示例,以表明类似的 api 可以正常工作。我认为这是 swagger-js-codegen 的一个问题,因为如果我将相同的请求放入 api-docs 它工作正常,或者当然是我的 Java,或者以某种方式设置了我的内容类型标头,但我没有想法如何或在哪里。我错过了什么吗?无论我的 JAVA 是否声明 'consume = "application/json"',API 的行为都是一样的。
Swagger JSON
{
"/api/entities/{id}/labels": {
"delete": {
"consumes": [
"application/json"
],
"operationId": "deleteEntityLabel",
"parameters": [
{
"default": "16_appiniteDev",
"description": "The id of the entity to be edited",
"in": "path",
"name": "id",
"required": true,
"type": "string"
},
{
"description": "The label/labels to be deleted",
"in": "body",
"name": "labels",
"required": true,
"schema": {
"items": {
"type": "string"
},
"type": "array"
}
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ResponseEntity"
}
},
"204": {
"description": "No Content"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
}
},
"summary": "Deletes Labels for Entities",
"tags": [
"entity-resource"
]
},
"post": {
"consumes": [
"application/json"
],
"operationId": "createEntityLabel",
"parameters": [
{
"default": "16_appiniteDev",
"description": "The id of the entity to be edited",
"in": "path",
"name": "id",
"required": true,
"type": "string"
},
{
"description": "The array of labels to be set",
"in": "body",
"name": "labels",
"required": true,
"schema": {
"items": {
"type": "string"
},
"type": "array"
}
}
],
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/ResponseEntity"
}
},
"201": {
"description": "Created"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"summary": "Creates Labels for Entities",
"tags": [
"entity-resource"
]
}
}
}
DELETE
JAVA/SPRING
/**
* DELETE /entities/{id}/labels -> delete labels for an entity
*
* @param id
* @param labels
* @throws ServiceException
*/
@RequestMapping(value = "/entities/{id}/labels", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
@ApiOperation(value = "Deletes Labels for Entities", nickname = "deleteEntityLabel", consumes = "application/json")
public ResponseEntity deleteLabels(
@ApiParam(value = "The id of the entity to be edited", required = true, defaultValue = "16_appiniteDev") @PathVariable final String id,
@ApiParam(value = "The label/labels to be deleted", required = true, defaultValue = "[\"label1\",\"label2\",\"label3\"]") @NotNull final @RequestBody String[] labels
) throws ServiceException {
boolean isAppEntity = false;
User user = userService.getUserWithAuthorities();
String[] type = id.split("_");
if (StringUtils.isNumeric(type[0]) && !type[1].startsWith(type[0])) {
isAppEntity = true;
}
entityService.deleteTags(id, labels, user, isAppEntity);
Map<String, String> response = new LinkedHashMap<>();
response.put(Constants.RESPONSE_ENTITY_HEADER_MESSAGE,
"Labels deleted successfully");
return new ResponseEntity(response, HttpStatus.OK);
}
CURL FROM APP
curl 'http://localhost:8080//api/entities/52_QA42/labels?cacheBuster=1472070679337' -X DELETE -H 'Pragma: no-cache' -H 'Origin: http://localhost:8080' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36' -H 'Content-Type: text/plain;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Cache-Control: no-cache' -H 'x-auth-token: admin:1472097365429:8e6524e252e2aebb786b7738c44fe385' -H 'Referer: http://localhost:8080/' -H 'Cookie: NG_TRANSLATE_LANG_KEY=%22en%22' -H 'Connection: keep-alive' -H 'DNT: 1' --data-binary '["Bug13124"]' --compressed
CREATE
JAVA/SPRING
/**
* POST /entities/{id}/labels -> add labels for a non hadoop entity
*
* @param id
* @param labels
* @throws ServiceException
*/
@RequestMapping(value = "/entities/{id}/labels", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
@ApiOperation(value = "Creates Labels for Entities", nickname = "createEntityLabel")
public ResponseEntity addLabels(
@ApiParam(value = "The id of the entity to be edited", required = true, defaultValue = "16_appiniteDev") @PathVariable final String id,
@ApiParam(value = "The array of labels to be set", required = true, defaultValue = "[\"label1\",\"label2\",\"label3\"]") @NotNull final @RequestBody String[] labels)
throws ServiceException {
boolean isAppEntity = false;
User user = userService.getUserWithAuthorities();
String[] type = id.split("_");
if (StringUtils.isNumeric(type[0]) && !type[1].startsWith(type[0])) {
isAppEntity = true;
}
entityService.addTags(id, labels, user, isAppEntity);
Map<String, String> response = new LinkedHashMap<>();
response.put(Constants.RESPONSE_ENTITY_HEADER_MESSAGE,
"Labels added successfully");
return new ResponseEntity(response, HttpStatus.OK);
}
CURL FROM APP
curl 'http://localhost:8080//api/entities/52_QA42/labels?cacheBuster=1472071851544' -H 'Pragma: no-cache' -H 'Origin: http://localhost:8080' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36' -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Cache-Control: no-cache' -H 'x-auth-token: admin:1472097365429:8e6524e252e2aebb786b7738c44fe385' -H 'Referer: http://localhost:8080/' -H 'Cookie: NG_TRANSLATE_LANG_KEY=%22en%22' -H 'Connection: keep-alive' -H 'DNT: 1' --data-binary '["test"]' --compressed
有什么想法吗?
【问题讨论】:
-
你能澄清一下什么是“应用程序”吗?你是说swagger-ui吗?对于 swagger-js-codegen,您是在谈论从 swagger-codegen 项目产生的东西,还是在谈论 swagger-js?
-
swagger-js-codegen 是 swagger-js 的 swagger-codegen。 github.com/wcandillon/swagger-js-codegen
-
@trudesign 您可以考虑使用swagger codegen 来生成 JS 客户端(或 Typescript、PHP、Ruby、Java 等中的 API 客户端)。
-
@wing328 老实说,我无法理解 swagger-codegen 是如何工作的,而 swagger-js-codegen 是一个经过批准的插件,可以为我进行角度 swagger-codegen 转换。我需要弄清楚如何让 swagger-js-codegen 实际编写请求内容类型,它似乎忽略了 swagger json 文件的 'consumes' 属性。
-
@trudesign 感谢您的反馈。安装 java、mvn 等以使用 swagger codegen 对非 Java 开发人员来说可能会很痛苦。另一种方法是使用editor.swagger.io 或生成SDK online。如果您需要进一步的帮助,请告诉我。
标签: angularjs swagger swagger-2.0