【发布时间】:2016-02-05 04:36:15
【问题描述】:
我正在尝试上传图片,但出现此错误,我不知道为什么。我已经尝试了很多方法,但仍然出现错误。
首先,这个:
{
"timestamp": 1454645660390
"status": 405
"error": "Method Not Allowed"
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException"
"message": "Request method 'POST' not supported"
"path": "/usuarios/update"
}
这是我的控制器:
注意:返回null 进行测试。
@RequestMapping(value = "/update", method = RequestMethod.POST, headers = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Usuarios> updateUsuario(
OAuth2Authentication authentication,
HttpServletRequest req,
@RequestBody Usuarios usuarios,
@RequestParam("file") MultipartFile file) {
req.getHeaderNames();
file.getName();
return null;
}
这是我的MultipartResolver:
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setMaxUploadSize(1000000);
return resolver;
}
任何建议我做错了什么?非常感谢!
更新
我已经更新了我的@Bean:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class TierraApplication {
public static void main(String[] args) {
SpringApplication.run(TierraApplication.class, args);
}
@Bean
public MultipartConfigElement multipartConfigElement() {
return new MultipartConfigElement("");
}
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setMaxUploadSize(1000000);
return resolver;
}
}
还有我@RestController上的方法:
@RestController
@RequestMapping("/usuarios")
public class UsuariosController implements Serializable {
@RequestMapping(value = "/update", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
public ResponseEntity<Usuarios> updateUsuario(
@RequestBody Usuarios usuarios,
@RequestParam("file") MultipartFile file) {
file.getName();
return null;
}
}
但现在我收到了这个错误:
{
"timestamp": 1454683574928
"status": 415
"error": "Unsupported Media Type"
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException"
"message": "Content type 'multipart/form-data;boundary=----WebKitFormBoundary6GTTqiBmiacyW0xb;charset=UTF-8' not supported"
"path": "/usuarios/update"
}
编辑 2
好的,我已经删除了 multipartResolver 的 @Bean 和 @RequestBody 并且一切正常。
@RequestMapping(value = "/update", method = RequestMethod.POST)
public ResponseEntity<?> updateUsuario(@RequestParam("file") MultipartFile file,
OAuth2Authentication authentication,
HttpServletRequest req) {
try {
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(name, HttpStatus.OK);
}
但现在我无法在请求中到达我的身体。如果我把它再次得到所有相同的错误。那么如何使用这样的 JSON 传递或到达 body 呢?
{
"idUsuario": 1,
"roles":{"idRol": 1, "nombreRol": "ADMINISTRADOR", "fechaCreacion": "2016-01-31", "fechaModificacion": null,…},
"nombre": "User",
"apellido": "Test",
"fechaNacimiento": "1992-04-04",
"dni": 38078020,
"email": "test@hotmail.com",
"telefono": 155797919,
"domicilio": "test 972",
"provincia": "San Salvador de Jujuy",
"username": "tester",
"imagen": null,
"estado": true,
"fechaCreacion": "2016-02-03",
"fechaModificacion": null,
"idUsuarioCreacion": 1,
"idUsuarioModificacion": 0,
"passwordUsuario": "$2a$10$SGueYkRnMkL43Ns1nmA9NeONLLrqjChHtYwO8eh/LrMJlTkFHielW"
}
【问题讨论】:
-
在您的 RequestMapping-Annotation 中有“/update”,而错误表明您尝试了“/usuarios/update”。您是否在其他地方定义了“/usuarios”?
-
是的,在我的
@RestController("/usuarios")
标签: spring spring-mvc file-upload spring-boot media-type