0

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"
}
2
  • In your RequestMapping-Annotation you have "/update" while the error states you tried "/usuarios/update". Have you defined "/usuarios" elsewhere? Commented Feb 5, 2016 at 7:27
  • yeah, in my @RestController("/usuarios") Commented Feb 5, 2016 at 7:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@Andy thanks for the edit. Sometimes I'm lost without the codecompletion of my IDE. :-)
Well, the only difference I see to a working controller in my actual project are the OAuth2Authentication and HttpServletRequest parameters. What happens if you remove them?
i just put them to control what im sending, still same error without them, i'll update my question
OK, now the error is another one. Can you remove "boundary=...charset=UTF-8" from the Content-Type-header of your request and try again?
that is setted automatic, without it, got an error of bad boundary :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.