当前请求不是多部分请求。

3
我已经从Swagger API生成了一个Spring REST Web服务。以下是POST服务的代码:
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2016-10-24T09:36:32.738Z")

@Api(value = "addUser", description = "the addUser API")
public interface AddUserApi {

@ApiOperation(value = "Add an additional user", notes = "This service is used to add and additional user to the list of users.", response = Void.class, tags={  })
@ApiResponses(value = { 
    @ApiResponse(code = 200, message = "OK", response = Void.class),
    @ApiResponse(code = 200, message = "Unexpected error", response = Void.class) })
@RequestMapping(value = "/addUser",
    produces = { "application/json" }, 
    consumes = { "application/x-www-form-urlencoded" },
    method = RequestMethod.POST)
ResponseEntity<Void> addUserPost(


@ApiParam(value = "Unique id of the user to be added.", required=true ) @RequestPart(value="userId", required=true)  Integer userId,


@ApiParam(value = "Name of the user to be added.", required=true ) @RequestPart(value="name", required=true)  String name,


@ApiParam(value = "Profession of the user to be added.", required=true ) @RequestPart(value="profession", required=true)  String profession);

}

我正在使用内容类型“application/x-www-form-urlencoded”将数据发布到此服务。但我面临的问题是,当我发布时,我会收到下面给出的错误JSON作为响应。

{
  "timestamp": 1477401894250,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "org.springframework.web.multipart.MultipartException",
  "message": "The current request is not a multipart request",
  "path": "/UserManagement/rest/UserService/addUser"
}

据我所知,只有在从表单上传文件时才需要使用multipart,而我并没有上传文件。我已经搜索过了,每个结果都是基于文件上传的。我的Swagger YAML如下所示。

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
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.
      produces:
        - application/json
      consumes:
        - application/x-www-form-urlencoded
      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

请问有人能够帮我解决这个问题并告诉我原因和解决方法吗?如果需要其他细节来分析问题,请告知。

请求头如下:

{
  "Accept": "application/json"
}

编辑:

我尝试将 consumes 更改为 "application/json",并获得了以下响应:

{
  "timestamp": 1477464487595,
  "status": 415,
  "error": "Unsupported Media Type",
  "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
  "message": "Content type 'application/x-www-form-urlencoded' not supported",
  "path": "/UserManagement/rest/UserService/addUser"
}

以下是请求头信息

{
  "Accept": "application/json"
}

你能粘贴请求头吗? - Waqas
add your entire request plz - kuhajeyan
1个回答

1
您的映射包含 consumes = { "application/x-www-form-urlencoded" } ,这意味着它接受二进制内容类型,将其更改为以下以接受 JSON 数据类型。
@RequestMapping(value = "/addUser",
    produces = { "application/json" }, 
    consumes = { "application/json" },
    method = RequestMethod.POST)
ResponseEntity<Void> addUserPost(..)

我尝试更改这个,但是出现了“不支持的媒体类型”415错误。我有一个表单,我需要获取该表单的数据到Web服务中。您可以在Swagger中编写的yaml post参数中看到这一点。 - Manesh
我编辑了内容,添加了新的请求头和响应。 - Manesh
谢谢,我解决了这个问题。我必须同时更改方法参数才能获得正确的实现,并且我还将POST操作的参数更改为对象“User”(在定义中提到)并放入body中。 - Manesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接