Manesh Manesh - 1 month ago 19
reST (reStructuredText) Question

The current request is not multipart

I have generated a Spring REST web service from Swagger API. The code for the POST Service is given below

@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);

}


I'm posting the data to this service using the content type "application/x-www-form-urlencoded". But the issue that I'm facing is that on posting I'm getting an error JSON as response which is given below.

{
"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"
}


According to my knowledge multipart is only needed when we are uploading a file from the form, I'm not uploading a file. I have searched and every result is based on file upload. My swagger YAML is given below.

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


Can someone pleas assist me and tell me why I'm facing this issue and how to solve this. Please inform if you require any other details to analyse the problem.

Request headers are as follows

{
"Accept": "application/json"
}


Edited:

I tried changing the consumes to "application/json" and I got the response as given below

{
"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"
}


Request header is given below

{
"Accept": "application/json"
}

Answer

your mapping contains consumes = { "application/x-www-form-urlencoded" } means it accepts content type that are binarym, change that to following to accept json data type

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