vendredi 22 octobre 2021

Should controller take "id" as PathVariable or should it take "page-id"

We are trying to create SEO friendly roads. In this context, we decided to make changes to the url. E.g: /141(id) --> /example-page-141

I argue that we should get the "id" value as PathVariable on the back-end side. Another solution is to take "/example-page-141" as @PathVariable and find 141 in it. Which is the right solution?

  1. Solution

      @GetMapping("/get/{id}")
    public ResponseEntity<?> getProductDetail(@PathVariable Long id) 
    
        Product product = productService.getProductDetail(id);
    
        return new ResponseEntity<>(product, HttpStatus.OK);
    }
    
  2. Solution

     @GetMapping("/get/{id}")
     public ResponseEntity<?> getProductDetail(@PathVariable String id) {
    
    String[] bits = id.split("-");
    
    Long idLong = Long.valueOf(bits[bits.length-1]);
    
    Product product = productService.getProductDetail(idLong);
    
    return new ResponseEntity<>(product, HttpStatus.OK);
     }
    

Which should do the split operation on the front-end or the back-end?

Aucun commentaire:

Enregistrer un commentaire