該賞金到期in 4天。回答這個問題有資格獲得 50聲望獎勵。 2dor想引起更多人對這個問題的關注:
請幫忙回答這個問題
我有一個包含其他 DTO 和多部分檔案串列的 DTO。我正在嘗試處理該 DTO,但似乎無法讀取請求。
class TeacherDTO {
private SpecializationDto specializationDto;
private List<MultipartFile> files;
}
@PostMapping(consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<Object> saveNewTeacher(@ModelAttribute @Valid TeacherDTO teacherDto){
//process request
}
從 Swagger UI 創建示例請求時,出現以下例外:
type 'java.lang.String' to required type 'SpecializationDto' for property 'specializationDto': no matching editors or conversion strategy found
如果我把 @RequestBody 而不是 @ModelAttribute 那么我得到
Content type 'multipart/form-data;boundary=----WebKitFormBoundaryVEgYwEbpl1bAOjAs;charset=UTF-8' not supported]
Swagger 依賴項:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-data-rest</artifactId>
<version>1.5.2</version>
</dependency>
OpenAPI3.0 配置:
@Configuration
public class OpenApi30Config {
private final String moduleName;
private final String apiVersion;
public OpenApi30Config(
@Value("${spring.application.name}") String moduleName,
@Value("${api.version}") String apiVersion) {
this.moduleName = moduleName;
this.apiVersion = apiVersion;
}
@Bean
public OpenAPI customOpenAPI() {
final var securitySchemeName = "bearerAuth";
final var apiTitle = String.format("%s API", StringUtils.capitalize(moduleName));
return new OpenAPI()
.addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
.components(
new Components()
.addSecuritySchemes(securitySchemeName,
new SecurityScheme()
.name(securitySchemeName)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
)
)
.info(new Info().title(apiTitle).version(apiVersion));
}
}
uj5u.com熱心網友回復:
這似乎是 springdoc-openapi-ui 如何構建表單資料請求的問題。我能夠重現這個并注意到它發送了一個多部分請求(通過瀏覽器的開發工具攔截):
-----------------------------207598777410513073071314493349
Content-Disposition: form-data; name="specializationDto"\r\n\r\n{\r\n "something": "someValue"\r\n}
-----------------------------207598777410513073071314493349
Content-Disposition: form-data; name="files"; filename="somefile.txt"
Content-Type: application/octet-stream
<content>
-----------------------------207598777410513073071314493349
Content-Disposition: form-data; name="files"; filename="somefile.txt"
Content-Type: application/octet-stream
<content>
使用該負載,Spring 無法反序列化specializationDto,從而導致您觀察到的“找不到匹配的編輯器或轉換策略”例外。但是,如果您通過郵遞員或 curl 發送請求(注意 specializationDto 物件的點符號)
curl --location --request POST 'http://localhost:8080/upload' \
--form 'files=@"/path/to/somefile"' \
--form 'files=@"/path/to/somefile"' \
--form 'specializationDto.something="someValue"'
然后 Spring 能夠正確決議它。這是我的休息映射,它將按預期記錄以下內容:
@RequestMapping(value = "/upload", method = RequestMethod.POST,
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public void upload(@ModelAttribute TeacherDto requestDto) {
System.out.println(requestDto);
}
// logs:
TeacherDto(specializationDto=SpecializationDto(something=someValue), files=[org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@78186ea6, org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@461c9cbc])
我建議你在他們的 github頁面上打開一個 bug 。
uj5u.com熱心網友回復:
您是否嘗試過使用 @RequestMapping 而不是 @PostMapping ?像這樣的東西:
@RequestMapping(value = "/ex/foos", method = RequestMethod.POST)
public ResponseEntity<Object> saveNewTeacher(@Valid @RequestBody TeacherDTO teacherDTO) throws Exception {
//process instance
}
class TeacherDTO {
@Valid
private SpecializationDto specializationDto;
private List<MultipartFile> files;
}
//supposing your nested DTO is something like that
class SpecializationDto {
@NotNull
@NotEmpty
String something;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/397829.html
