What is the difference between @PathParam and @PathVariable

后端 未结 7 1610
天涯浪人
天涯浪人 2020-12-23 19:21

To my knowledge both serves the same purpose. Except the fact that @PathVariable is from Spring MVC and @PathParam is from JAX-RS. Any insights on

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-23 19:52

    @PathParam: it is used to inject the value of named URI path parameters that were defined in @Path expression.

    Ex:

    @GET
    @Path("/{make}/{model}/{year}")
    @Produces("image/jpeg")
    public Jpeg getPicture(@PathParam("make") String make, @PathParam("model") PathSegment car, @PathParam("year") String year) {
            String carColor = car.getMatrixParameters().getFirst("color");
    
    }
    

    @Pathvariable: This annotation is used to handle template variables in the request URI mapping ,and used them as method parameters.

    Ex:

         @GetMapping("/{id}")
         public ResponseEntity getByIdPatient(@PathVariable Integer id) {
              Patient obj =  service.getById(id);
              return new ResponseEntity(obj,HttpStatus.OK);
         }
    

提交回复
热议问题