Spring 休息 Web 服务返回 415
Spring Rest Web service returning 415
我有 Spring 以下网络服务。 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 中将应用程序设置为 运行,没有任何问题。我现在面临的问题是,在访问获取 Web 服务“/users”时,我从该服务中收到错误消息。
我尝试调试 spring 应用程序,我发现甚至没有命中预期的服务。服务代码如下。
@javax.annotation.Generated(值="class io.swagger.codegen.languages.SpringCodegen",日期=“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();
要么删除 consumes 键,要么在 GET 请求中支持 Content-Type。
编辑:删除 consumes 键也许是更好的选择。我不认为在 GET 请求中使用任何 json 是个好主意。
我有 Spring 以下网络服务。 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 中将应用程序设置为 运行,没有任何问题。我现在面临的问题是,在访问获取 Web 服务“/users”时,我从该服务中收到错误消息。
我尝试调试 spring 应用程序,我发现甚至没有命中预期的服务。服务代码如下。 @javax.annotation.Generated(值="class io.swagger.codegen.languages.SpringCodegen",日期=“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();
要么删除 consumes 键,要么在 GET 请求中支持 Content-Type。
编辑:删除 consumes 键也许是更好的选择。我不认为在 GET 请求中使用任何 json 是个好主意。