簡體   English   中英

Spring Rest Web服務返回415

[英]Spring Rest Web service returning 415

我有以下的Spring Web服務。 敏捷的JSON

{
"swagger": "2.0",
"info": {
    "version": "1.0.0",
    "title": "Extended User Management API",
    "description": "This is communicate with an extended user management webservice to test the swagger API for learning"
},
"schemes": [
    "http"
],
"basePath": "/UserManagement/rest/UserService",
"host": "127.0.0.1:8089",
"produces": [
    "application/json"
],
"consumes": [
    "application/json"
],
"paths": {
    "/users": {
        "get": {
            "responses": {
                "200": {
                    "description": "An array of users",
                    "schema": {
                        "type": "array",
                        "items": {
                            "$ref": "#/definitions/User"
                        }
                    }
                },
                "default": {
                    "description": "Unexpected error",
                    "schema": {
                        "$ref": "#/definitions/Error"
                    }
                }
            }
        }
    },
    "/addUser": {
        "post": {
            "summary": "Add an additional user",
            "description": "This service is used to add and additional user to the list of users.",
            "parameters": [
                {
                    "name": "user_id",
                    "in": "query",
                    "description": "Unique id of the user to be added.",
                    "required": true,
                    "type": "integer",
                    "format": "int32"
                },
                {
                    "name": "name",
                    "in": "query",
                    "description": "Name of the user to be added.",
                    "required": true,
                    "type": "string"
                },
                {
                    "name": "profession",
                    "in": "query",
                    "description": "Profession of the user to be added.",
                    "required": true,
                    "type": "string"
                }
            ],
            "responses": {
                "200": {
                    "description": "OK"
                },
                "default": {
                    "description": "Unexpected error",
                    "schema": {
                        "$ref": "#/definitions/Error"
                    }
                }
            }
        }
    }
},
"definitions": {
    "User": {
        "type": "object",
        "properties": {
            "user_id": {
                "type": "integer",
                "format": "int32",
                "description": "This is unique id that is assigned to each user."
            },
            "name": {
                "type": "string",
                "description": "This is the name of the user"
            },
            "profession": {
                "type": "string",
                "description": "This is the profession that the user holds"
            }
        }
    },
    "Error": {
        "type": "object",
        "properties": {
            "code": {
                "type": "integer",
                "format": "int32"
            },
            "message": {
                "type": "string"
            },
            "fields": {
                "type": "string"
            }
        }
    }
}
}

我生成了代碼,並解決了項目中的所有錯誤。 我讓應用程序在Spring boot main中運行,沒有任何問題。 我現在面臨的問題是訪問get Web服務“ / users”時,該服務出現錯誤。 產生錯誤

我嘗試調試spring應用程序,結果發現預期的服務沒有得到實現。 服務代碼如下。 @ javax.annotation.Generated(value =“ class io.swagger.codegen.languages.SpringCodegen”,date =“ 2016-10-24T09:36:32.738Z”)

@Api(value = "users", description = "the users API")
public interface UsersApi {

@ApiOperation(value = "", notes = "", response = User.class, responseContainer = "List", tags={  })
@ApiResponses(value = { 
    @ApiResponse(code = 200, message = "An array of users", response = User.class),
    @ApiResponse(code = 200, message = "Unexpected error", response = User.class) })
@RequestMapping(value = "/users",
    produces = { "application/json" }, 
    consumes = { "application/json" },
    method = RequestMethod.GET)
ResponseEntity<List<User>> usersGet();

}

下面給出了該接口的實現

@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2016-10-24T09:36:32.738Z")
@Controller
public class UsersApiController implements UsersApi {

UserDao udao = new UserDao();

public ResponseEntity<List<User>> usersGet() {
    return new ResponseEntity<List<User>>(udao.getAllUsers(), HttpStatus.OK);
}
}

能否請一個人告訴我我犯了什么錯誤,以便我可以解決這個問題。

好吧,您使用的是不受支持的媒體類型,如異常所示。 看看您的@RequestMapping批注:

@RequestMapping(value = "/users",
produces = { "application/json" }, 
consumes = { "application/json" },
method = RequestMethod.GET)
ResponseEntity<List<User>> usersGet(); 

您可以刪除消耗密鑰,或者在GET請求中支持Content-Type。

編輯:也許將是一個更好的選擇,以刪除消耗鍵。 我認為在GET請求中使用任何json不是一個好主意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM