Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

299
Vistas
Java can't upload file with @Pathvariable optional

I try to build a todoapp with the function to upload files. Now i want to can upload files on a task or simply upload files without tasks. For that i need the @PathVariable Annotation to be optional.

This is my Controller:

@PostMapping("/upload/{taskId}")
private ResponseEntity<String> uploadFile(@CurrentUser UserPrincipal userPrincipal, @RequestParam("file") MultipartFile[] file, @PathVariable(required = false) String taskId) {
    fileService.upload(userPrincipal.getUser(), file, taskId);
    return new ResponseEntity<>("File uploaded", HttpStatus.OK);
}

If i try it to upload with a TaskId it works. But when i try it to upload without a taskId it doesnt work. I got the error:

"405 Method not allowed"

Screenshot: enter image description here

Does somebody know how to fix this?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If you want to use the @PathVariable as an optional be sure to bind both paths: @PostMapping(value = {"/upload/{taskId}", "/upload"}).

If you don't post the taskId spring will look for a controller that handles "/upload" instead of "/upload/{taskId}"

Personally i would use RequestParam instead of PathVariable for optional parameters

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda