Search code examples
javascriptjavaspring-bootfetch-api

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?


Solution

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