Код: Выделить всё
curl -X 'POST' \
'http://localhost:8080/test/third' \
-H 'accept: */*' \
-H 'Content-Type: multipart/form-data' \
-F 'file=@test_file.docx;type=multipart/form-data' \
-F 'dto={"name":"string","description":"string"};type=application/json' \
-F 'str=some_test_value;type=text/plain' \
контроллер:
Код: Выделить всё
package org.example.springbased.web;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Encoding;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/test")
public class TestController {
@PostMapping(value = "/second", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity test(
@RequestBody(content = @Content(encoding = @Encoding(name = "dto", contentType = MediaType.APPLICATION_JSON_VALUE)))
@Parameter(description = "An extra JSON payload sent with file")
@RequestPart("dto")
DTO dto,
@RequestBody(content = @Content(encoding = @Encoding(name = "file", contentType = MediaType.MULTIPART_FORM_DATA_VALUE)))
@Parameter(required = true)
@RequestPart("file") MultipartFile file){
return ResponseEntity.ok("Request Accepted: " + dto.toString());
}
@PostMapping(value ="/first", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity uploadMultipartWithBody(
@RequestBody(content = @Content(encoding = @Encoding(name = "file", contentType = MediaType.MULTIPART_FORM_DATA_VALUE)))
@Parameter(required = true)
@RequestPart("file") MultipartFile file,
@RequestBody(content = @Content(encoding = @Encoding(name = "dto", contentType = MediaType.APPLICATION_JSON_VALUE)))
@Parameter(description = "An extra JSON payload sent with file")
@RequestPart("dto")
DTO dto){
return ResponseEntity.ok("Request Accepted: " + dto.toString());
}
@PostMapping(value ="/third", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity threeParams(
@RequestBody(content = @Content(encoding = @Encoding(name = "file", contentType = MediaType.MULTIPART_FORM_DATA_VALUE)))
@Parameter(required = true)
@RequestPart("file") MultipartFile file,
@RequestBody(content = @Content(encoding = @Encoding(name = "dto", contentType = MediaType.APPLICATION_JSON_VALUE)))
@Parameter(description = "An extra JSON payload sent with file")
@RequestPart("dto")
DTO dto,
@RequestBody(content = @Content(encoding = @Encoding(name = "str", contentType = MediaType.TEXT_PLAIN_VALUE)))
@RequestPart("str")
String str){
return ResponseEntity.ok("Request Accepted: " + dto.toString());
}
}
Код: Выделить всё
{
"openapi": "3.1.0",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost:8080",
"description": "Generated server url"
}
],
"paths": {
"/test/third": {
"post": {
"tags": [
"test-controller"
],
"operationId": "threeParams",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
},
"dto": {
"$ref": "#/components/schemas/DTO"
},
"str": {
"type": "string"
}
},
"required": [
"dto",
"file",
"str"
]
},
"encoding": {
"file": {
"contentType": "multipart/form-data"
}
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/test/second": {
"post": {
"tags": [
"test-controller"
],
"operationId": "test",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"dto": {
"$ref": "#/components/schemas/DTO"
},
"file": {
"type": "string",
"format": "binary"
}
},
"required": [
"dto",
"file"
]
},
"encoding": {
"dto": {
"contentType": "application/json"
}
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/test/first": {
"post": {
"tags": [
"test-controller"
],
"operationId": "uploadMultipartWithBody",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
},
"dto": {
"$ref": "#/components/schemas/DTO"
}
},
"required": [
"dto",
"file"
]
},
"encoding": {
"file": {
"contentType": "multipart/form-data"
}
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"DTO": {
"type": "object",
"description": "An extra JSON payload sent with file",
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string"
}
}
}
}
}
}
< /code>
Как вы можете видеть, даже если я укажу < /p>
@RequestBody(content = @Content(encoding = @Encoding(name = "...", contentType = MediaType....))
< /code>
Для каждого параметра требуется только первый, который указан для первого параметра. < /p>
Почему? Что мне нужно сделать, чтобы это сработало?dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.6'
implementation 'org.springframework.boot:spring-boot-devtools'
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... ller-level