Home > Software engineering >  empty path variable inside url get me 404 not found
empty path variable inside url get me 404 not found

Time:10-01

i have a request with empty path variable that get me a 404 response.how can i get my GET request to go into my webservice and throw exception showing that the pathvariable is empty. for now i'm having a 404 not found exception. exemple : GET
request url : hello/123/me ==> works fine
request url hello//me ===> 404 not found

    @GetMapping(value = "/hello/{uuid}/me")
public ResponseEntity<Void> hello(
        @PathVariable(name = "uuid", required = false) String uuid){

CodePudding user response:

You can achieve it with the following code.

@GetMapping(value = {"/hello/{uuid}/me", "/hello//me"})
public ResponseEntity<Void> hello(@PathVariable(name = "uuid", required = false) String uuid) {

What I have done is, just added /hello//me also to the request mapping of the same function. You can add multiple URLs just like this.

I suggest making the URL without parameters as /hello/me just to make it a standard, obviously if you have a scenario where you want to achieve it with /hello//me, you can do that.

CodePudding user response:

Maybe this the answer you looking for...., basically you want to pass the value of that uuid to be checked if its valid or not. If its not you can pass the exception into the web service using IllegalStateException. If you using getter and setter with JPA in your spring this can be done :

Optional<ClassName> validUuid = NameObjectofYourJPARepo.findByUuid(YourClassName.getUuid());

if(!validUuid.isPresent()) {
      throw new IllegalStateException("404 not found");
}  

I hope this can answer your problem

  • Related