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

429
Vistas
Spring Boot Controller does not recognize Path Variable as optional

Using Spring Boot, I have implemented a RestController like so:

@RestController
@RequestMapping("/api/v1/student/img")
@CrossOrigin("*")
public class ProfilePictureController {

    @GetMapping( "/{studentId}")
    public void getProfilePicture(@PathVariable(required = false) Long studentId, HttpServletResponse response) throws IOException {
        Optional<ProfilePicture> profilePicture;
        if (studentId != null) {
            profilePicture= studentService.getProfilePictureByStudentId(studentId);
        } else {
            profilePicture= studentService.getProfilePicture(1L);
        }
        if (profilePicture.isPresent()) {
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(profilePicture.get().getImage());
            outputStream.close();
        }
    }

My ProfilePicture-class contains a variable "image", which is of type byte[]. I am trying to retrieve this variable.

Anyways, the issue is that my controller does not seem to treat my PathVariable as optional. If I use the fetch-API to send a GET request with the following URL:

const url = "http://localhost:8080/api/v1/student/img/",

I am getting an error:

'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "img".

Does anyone know what might be the issue?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You only define the resource /api/v1/student/img/{studentId} but NOT the resource /api/v1/student/img/.

So if you just call /api/v1/student/img/ as you mentioned , it should return you 404 Not Found but not the following error that you mentioned :

'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "img".

I believe you are actually calling /api/v1/student/img/img instead. As img is not a Long and hence the error.

If you just want to call /api/v1/student/img/ without any studentId , you should define another resource for it (see below). Technically , they are different resources.

@RestController
@RequestMapping("/api/v1/student/img")
@CrossOrigin("*")
public class ProfilePictureController {

    @GetMapping( "/{studentId}")
    public void getProfilePicture(@PathVariable(required = false) Long studentId, HttpServletResponse response) throws IOException {

    }


    @GetMapping
    public void getProfilePicture(HttpServletResponse response) throws IOException {
   
    }

  }

Or defining two resource paths on the @GetMapping with Optional on the paramater :

@RestController
@RequestMapping("/api/v1/student/img")
@CrossOrigin("*")
public class ProfilePictureController {

    @GetMapping( {"/", "/{studentId}"})
    public void getProfilePicture(@PathVariable(required = false) Optional<Long> studentId, HttpServletResponse response) throws IOException {

    }
  }
about 4 years ago · Juan Pablo Isaza Denunciar

0

/api/v1/student/img/ doesn't match /api/v1/student/img/{studentId}. So your mapping will not work.

In addition to other anwser, In my opinion the best way to handle this is to add another mapping to the same method.

@GetMapping( {"/","/{studentId}"})
    public void getProfilePicture(@PathVariable(required = false) Long studentId, HttpServletResponse response) throws
        IOException {

    }

Find out more here https://medium.com/latesttechupdates/define-spring-optional-path-variables-1188fadfebde

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can't have optional path variables, but you can have two controller methods which call the same service code: but

If you are using Java 8 and above and Spring 4.1 and above you can use java.util.Optional which is supported in @RequestParam, @PathVariable, @RequestHeader and @MatrixVariable in Spring MVC

@RestController
@RequestMapping("/api/v1/student/img")
@CrossOrigin("*")
public class ProfilePictureController {

    @GetMapping( "/{studentId}")
    public void getProfilePicture(@PathVariable Optional<Long> type studentId, HttpServletResponse response) throws IOException {
        Optional<ProfilePicture> profilePicture;
        if (studentId.isPresent()) {
            profilePicture= studentService.getProfilePictureByStudentId(studentId.get());
        } else {
            profilePicture= studentService.getProfilePicture(1L);
        }
        if (profilePicture.isPresent()) {
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(profilePicture.get().getImage());
            outputStream.close();
        }
    }
about 4 years ago · Juan Pablo Isaza 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