文件上传Restful API:Spring-boot

时间:2016-02-05 04:36:16

标签: spring spring-mvc file-upload spring-boot media-type

我正在尝试上传图片,但是我收到了这个错误,我不知道为什么。我已经尝试了很多东西,但我仍然遇到错误。

首先,这个:

{
"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传递或到达身体呢?

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

1 个答案:

答案 0 :(得分:1)

行。那就是问题所在。

@RestController("/usarios") 

设置控制器的名称而不是urlmapping。你应该用

注释你的课程
@RestController
@RequestMapping("/usarios")

为您的服务设置正确的urlmapping。