java - 当前请求不是多部分的

标签 java spring rest swagger

我已经从 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"
}

已编辑:

我尝试将 consume 更改为“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"
}

最佳答案

您的映射包含 consumes = { "application/x-www-form-urlencoded"} 表示它接受二进制内容类型,将其更改为以下以接受 json 数据类型

@RequestMapping(value = "/addUser",
    produces = { "application/json" }, 
    consumes = { "application/json" },
    method = RequestMethod.POST)
ResponseEntity<Void> addUserPost(..)

关于java - 当前请求不是多部分的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40253741/

相关文章:

java - 没有getter方法就不能使用对象变量

WebView 中的 Java Applet

java - RoboSpice:如何在 SpiceRequest 中触发 RequestListener.onRequestFailure 方法

java - 如何在java中无错误地将xml加载到ClassPathXmlApplicationContext?

java - Spring MVC 展示中如何使用 JSON 处理 HTTP 请求?一些需要理解的问题

java - 使用 Homebrew 安装适用于 Mac 的 OpenJDK 8(子版本 1.8.0.119)?

java - 将请求范围的 bean 注入(inject) session 范围的 bean

api - API 是否应该将所有工作委托(delegate)给其他服务?

ajax - spring 400 错误请求。我该如何修复它,或者至少看看是什么原因造成的?

rest - 如何在 Jekyll 中添加 RESTful 类型的路由