当我向此服务器代码发布请求时 - everething工作正常:
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json" })
public ResponseEntity<String> addQuestion(@RequestBody String dtoObject) { ... }
但如果我将请求更改为“multipart / form-data” - Spring返回错误400“错误请求”:
@RequestMapping(method = RequestMethod.POST, consumes = { "multipart/form-data" })
public ResponseEntity<String> addQuestion(@RequestBody String dtoObject) { ... }
为什么呢? 可能是我应该创建一些额外的bean?
PS:我需要“multipart / form-data”来与json对象一起发送文件。
答案 0 :(得分:4)
我认为您无法将文件反序列化为请求正文中的dtoObject。你将需要使用@RequestPart来做到这一点。
@RequestMapping(method = RequestMethod.POST, consumes = { "multipart/form-data" })
public ResponseEntity<String> addQuestion2(@RequestPart("question") QuestionPostDto dtoObject, @RequestPart("file") MultiPartFile file) { ... }
您的请求需要为formdata:包含您要上传的文件和json格式文件question.json
这是来自post man的我的有效载荷示例
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="QLbLFIR.gif"
Content-Type: image/gif
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="question"; filename="question.json"
Content-Type: application/json
------WebKitFormBoundary7MA4YWxkTrZu0gW--
或者如果您不想传递json格式文件,可以使用普通字符串传递
@RequestMapping(method = RequestMethod.POST, consumes = { "multipart/form-data" })
public ResponseEntity<String> addQuestion2(String question, @RequestPart("file") MultiPartFile file) {
QuestionPostDto dtoObject = new ObjectMapper().readValue(request, QuestionPostDto.class);
// do sth
}
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="QLbLFIR.gif"
Content-Type: image/gif
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="request"
{
"key": "value"
}
------WebKitFormBoundary7MA4YWxkTrZu0gW--
有关详细信息,请参阅此主题: Spring MVC Multipart Request with JSON
答案 1 :(得分:0)
我解决了这个问题。
1)我创建了HttpMessageConverter,它将json转换为我的自定义类型QuestionPostDto:
public class QuestionPostDtoHttpMessageConverter implements HttpMessageConverter<QuestionPostDto> {
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
return QuestionPostDto.class == clazz;
}
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return QuestionPostDto.class == clazz;
}
@Override
public List<MediaType> getSupportedMediaTypes() {
List<MediaType> list = new ArrayList<MediaType>();
list.add(MediaType.MULTIPART_FORM_DATA);
return list;
}
@Override
public QuestionPostDto read(Class<? extends QuestionPostDto> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
InputStream istream = inputMessage.getBody();
String requestString = IOUtils.toString(istream, "UTF-8");
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(requestString, QuestionPostDto.class);
}
@Override
public void write(QuestionPostDto t, MediaType contentType, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
}
}
2)我为这种类型的HttpMessageConverter创建bean(我们在项目中使用Spring Boot):
@Configuration
public class HttpConfiguration {
...
@Bean
public QuestionPostDtoHttpMessageConverter commonsMultipartResolver() {
return new QuestionPostDtoHttpMessageConverter();
}
}
3)现在我在RestController中的代码工作正常:
@RestController
@RequestMapping("/api/question")
@ConfigurationProperties(prefix = "question")
@RequiredArgsConstructor
@Slf4j
public class QuestionController {
...
@PostMapping
ResponseEntity<String> addQuestion(@RequestPart("dtoObject") QuestionPostDto dtoObject, @RequestPart("file") MultipartFile file) { ... }
感谢所有人,特别是Chi Dov