Home > front end >  Spring Boot Controller does not recognize Path Variable as optional
Spring Boot Controller does not recognize Path Variable as optional

Time:01-04

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?

CodePudding user response:

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

CodePudding user response:

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

  }
  • Related