Paulo Galdo Sandoval
Paulo Galdo Sandoval

Reputation: 2203

File upload Restful API: Spring-boot

I'm trying to make image uploading but I get this error, and I don't know why. I've already tried many things but I'm still getting errors.

Firstly, this:

{
"timestamp": 1454645660390
"status": 405
"error": "Method Not Allowed"
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException"
"message": "Request method 'POST' not supported"
"path": "/usuarios/update"
}

This is my controller: Note: returns null for testing.

@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;
}

And this is my MultipartResolver:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver resolver = new CommonsMultipartResolver();
    resolver.setMaxUploadSize(1000000);
    return resolver;
}

Any suggestions what I'm doing wrong? Thank you so much!

UPDATE

I've updated my @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;
    }
}

and the method on my @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;
    }
}

but now i'm getting this error:

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

EDIT 2

Ok, I've deleted the @Bean of multipartResolver and @RequestBody and all works fine.

@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);
}

But now I can't reach my body in the request. If I put it got again all the same errors. So how can I pass or reach the body with a JSON like this?

{
"idUsuario": 1,
"roles":{"idRol": 1, "nombreRol": "ADMINISTRADOR", "fechaCreacion": "2016-01-31", "fechaModificacion": null,…},
"nombre": "User",
"apellido": "Test",
"fechaNacimiento": "1992-04-04",
"dni": 38078020,
"email": "[email protected]",
"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"
}

Upvotes: 0

Views: 5458

Answers (1)

Frank
Frank

Reputation: 2066

OK. That's the problem.

@RestController("/usarios") 

sets the name of the controller not the urlmapping. You should annotate you class with

@RestController
@RequestMapping("/usarios")

to set the correct urlmapping for your service.

Upvotes: 1

Related Questions